creating multible variable lists in a loop in python - python

I'm trying to make a loop that finds distances of values of one list, to the values of another list.
The data itself is of varying dimensions in a coordinates layout. Here is an example
x = ['1.23568 1.589887', '1.989 1.689']
y = ['2.5689 1.5789', '2.898 2.656']
I would like to be able to make a separate list for each y value and its distance from each x value.
There are always more x values than y values.
This is what I have so far:
def distances_y(x,y):
for i in y:
ix = [i.split(' ',)[0] for i in y]
for z in x:
zx = [z.split('',1)[0] for z in x]
distances_1 = [zx - ix for z in x]
return distances_1
print(i +"_"+"list") = [distance_1]
But I'm stuck on how to create individual lists for each y value.
Each distance also needs to be a list in itself, a list in a list so to speak.
The largest problem is that I am unable to use packages besides tkinter for this.

Try using a dictionary instead:
def distances_y(x,y):
dct = {}
for i in y:
ix = [i.split(' ',)[0] for i in y]
for z in x:
zx = [z.split('',1)[0] for z in x]
distances_1 = [zx - ix for z in x]
return distances_1
dct[i +"_"+"list"] = [distance_1]
And to get the values, do:
print(dct)
And if you want to get a specific key name, try:
print(dct[<key name over here>])

And if you want a 2-d array:
for each x you would add
my2d.append([])
and for each y
my2d[i].append(x[i] - y[j])

Related

Select values in two arrays

I have two arrays of temperature with different times!
x = array(['1999-03-06T12:00:00.000000000', '1999-03-07T12:00:00.000000000',
'1999-03-08T12:00:00.000000000', ..., '2021-10-09T12:00:00.000000000',
'2021-10-10T12:00:00.000000000', '2021-10-11T12:00:00.000000000'],
dtype='datetime64[ns]')
This one is daily from 1999 to 2021
y = array(['2002-07-22T09:03:54.000000000', '2004-11-03T12:36:57.000000000',
'2004-11-04T05:19:08.000000000', '2004-12-13T11:50:36.000000000',
'2005-06-07T13:16:41.000000000', '2006-07-12T12:31:53.000000000',
'2006-07-22T11:43:24.000000000', '2006-09-10T14:08:57.000000000',...]
This one is random from 2002 to 2021
I would like to know how can I select in x (daily) just the dates that contain in y
(So x and y will have the same dates)
You can try intersect1d method in numpy.
import numpy as np
array_common_dates = np.intersect1d(x, y)
For larger arrays, sorted lookup will be faster than linear lookup:
# if x is not sorted, fix it
ind = np.searchsorted(x, y)
mask = ind < x.size
mask[mask] &= y[mask] == x[ind[mask]]
The other advantage of this method is that it provides you with a two-way mapping only marked elements match:
y[mask] == x[ind[mask]]
Maybe you can do something like this
x = ['1999-03-06T12:00:00.000000000', '1999-03-07T12:00:00.000000000',
'1999-03-08T12:00:00.000000000', '2021-10-09T12:00:00.000000000',
'2021-10-10T12:00:00.000000000', '2021-10-11T12:00:00.000000000']
y = ['1999-03-06T12:00:00.000000001', '1999-03-07T12:00:00.00000004',
'1999-03-08T12:00:00.000000002', '2021-10-09T12:00:00.000001004',
'2021-10-10T12:00:00.000000003', '2021-10-11T12:00:00.000002004']
z = []
for i in x:
n = i.find('T')
z.append(i[:n])
new_list = []
for i in z:
for j in y:
if i in j:
new_list.append(j)
print(new_list)

Is it possible to use slice in Python in list of complex objects with field of the object

I am new in Python and using python3.
I have list og objects of type points
class Po:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
C = [Po(0, j) for j in range(10)]
and 2 dimensional array
m = [[j for i in range(2)] for j in range(10)]
I want to assign all fields x of C to values with index 0 from m like
C[:].x = m[:][0]
but python says list doesn't have field x.
how can I do this and why I can' access x this way
There's no specific syntax for this in Python. You'll just need to use a regular for loop.
for ci, mi in zip(C, m):
ci.x = mi[0]
Your "C" instance is itself a list, so in order to access it's x or y values, you need to call the index first. So this way you can access them:
for i in range(10):
print(C[i].x)
print(C[i].y)

Removing Numbers from Column that Don't Appear in Array

Took a large data set, removed any numbers that are not within 2 SD from a specific column and created an array, now I want to remove any numbers not in array from columns without messing up index. Would preferably like to convert any non-present numbers as nan.
Code used to remove values outside of 2 SD:
pupil_area_array = numpy.array(part_data['pupil_area'])
mean = numpy.mean(part_data['pupil_area'], axis=0)
sd = numpy.std(part_data['pupil_area'], axis=0)
final_list = [x for x in part_data['pupil_area'] if (x > mean - 2 * sd)]
final_list = [x for x in final_list if (x < mean + 2 * sd)]
print(final_list)
If you are not restricted to using a generator, you should be able to use map() https://www.geeksforgeeks.org/python-map-function/:
def filter_sd(value):
if x > mean - 2 * sd:
return x
return None #or return 'Nan'
final = map(filter_sd, part_data['pupil_area'])

how to sum adjacent tuples/list

I apologise for the terrible description and if this is a duplicated, i have no idea how to phrase this question. Let me explain what i am trying to do. I have a list consisting of 0s and 1s that is 3600 elements long (1 hour time series data). i used itertools.groupby() to get a list of consecutive keys. I need (0,1) to be counted as (1,1), and be summed with the flanking tuples.
so
[(1,8),(0,9),(1,5),(0,1),(1,3),(0,3)]
becomes
[(1,8),(0,9),(1,5),(1,1),(1,3),(0,3)]
which should become
[(1,8),(0,9),(1,9),(0,3)]
right now, what i have is
def counter(file):
list1 = list(dict[file]) #make a list of the data currently working on
graph = dict.fromkeys(list(range(0,3601))) #make a graphing dict, x = key, y = value
for i in list(range(0,3601)):
graph[i] = 0 # set all the values/ y from none to 0
for i in list1:
graph[i] +=1 #populate the values in graphing dict
x,y = zip(*graph.items()) # unpack graphing dict into list, x = 0 to 3600 and y = time where it bite
z = [(x[0], len(list(x[1]))) for x in itertools.groupby(y)] #make a new list z where consecutive y is in format (value, count)
z[:] = [list(i) for i in z]
for i in z[:]:
if i == [0,1]:
i[0]=1
return(z)
dict is a dictionary where the keys are filenames and the values are a list of numbers to be used in the function counter(). and this gives me something like this but much longer
[[1,8],[0,9],[1,5], [1,1], [1,3],[0,3]]
edits:
solved it with the help of a friend,
while (0,1) in z:
idx=z.index((0,1))
if idx == len(z)-1:
break
z[idx] = (1,1+z[idx-1][1] + z[idx+1][1])
del z[idx+1]
del z[idx-1]
Not sure what exactly is that you need. But this is my best attempt of understanding it.
def do_stuff(original_input):
new_original = []
new_original.append(original_input[0])
for el in original_input[1:]:
if el == (0, 1):
el = (1, 1)
if el[0] != new_original[-1][0]:
new_original.append(el)
else:
(a, b) = new_original[-1]
new_original[-1] = (a, b + el[1])
return new_original
# check
print (do_stuff([(1,8),(0,9),(1,5),(0,1),(1,3),(0,3)]))

'float' object is unsubscriptable

thats what I get:
TypeError: 'float' object is unsubscriptable
Thats what I did:
import numpy as N
import itertools
#I created two lists, containing large amounts of numbers, i.e. 3.465
lx = [3.625, 4.625, ...]
ly = [41.435, 42.435, ...] #The lists are not the same size!
xy = list(itertools.product(lx,ly)) #create a nice "table" of my lists
#that iterttools gives me something like
print xy
[(3.625, 41.435), (3.625, 42.435), (... , ..), ... ]
print xy[0][0]
print xy[0][1] #that works just fine, I can access the varios values of the tuple in the list
#down here is where the error occurs
#I basically try to access certain points in "lon"/"lat" with values from xy through `b` and `v`with that iteration. lon/lat are read earlier in the script
b = -1
v = 1
for l in xy:
b += 1
idx = N.where(lon==l[b][b])[0][0]
idy = N.where(lat==l[b][v])[0][0]
lan/lot are read earlier in the script. I am working with a netCDF file and this is the latitude/longitude,read into lan/lot.
Its an array, build with numpy.
Where is the mistake?
I tried to convert b and v with int() to integers, but that did not help.
The N.where is accessing through the value from xy a certain value on a grid with which I want to proceed. If you need more code or some plots, let me know please.
Your problem is that when you loop over xy, each value of l is a single element of your xy list, one of the tuples. The value of l in the first iteration of the loop is (3.625, 41.435), the second is (3.625, 42.435), and so on.
When you do l[b], you get 3.625. When you do l[b][b], you try to get the first element of 3.625, but that is a float, so it has no indexes. That gives you an error.
To put it another way, in the first iteration of the loop, l is the same as xy[0], so l[0] is the same as xy[0][0]. In the second iteration, l is the same as xy[1], so l[0] is the same as xy[1][0]. In the third iteration, l is equivalent to xy[2], and so on. So in the first iteration, l[0][0] is the same as xy[0][0][0], but there is no such thing so you get an error.
To get the first and second values of the tuple, using the indexing approach you could just do:
x = l[0]
y = l[1]
Or, in your case:
for l in xy:
idx = N.where(lon==l[0])[0][0]
idy = N.where(lat==l[1])[0][0]
However, the simplest solution would be to use what is called "tuple unpacking":
for x, y in xy:
idx = N.where(lon==x)[0][0]
idy = N.where(lat==y)[0][0]
This is equivalent to:
for l in xy:
x, y = l
idx = N.where(lon==x)[0][0]
idy = N.where(lat==y)[0][0]
which in turn is equivalent to:
for l in xy:
x = l[0]
y = l[1]
idx = N.where(lon==x)[0][0]
idy = N.where(lat==y)[0][0]

Categories

Resources