Python: how to add first value in each list - python

A Posn is a list of length two [x,y], where
x and y are both Float values, corresponding to
the x and y coordinates of the point, respectively.
make_posn: float float -> Posn
def make_posn(x_coord, y_coord):
return [x_coord, y_coord]
How do I add all the x-values in a list of Posns?
Ex: [ [3.0, 4.0], [8.0, -1.0], [0.0, 2.0]] would be 11

sum them:
In [2]: sum(x[0] for x in [ [3.0, 4.0], [8.0, -1.0], [0.0, 2.0]])
Out[2]: 11.0

The following piece of code should work for your
_sum = 0.0
for sublist in [ [3.0, 4.0], [8.0, -1.0], [0.0, 2.0]]:
_sum += sublist[0]
It initializes a sum accumulator to zero and then iterates over the sublist elements of the list to add the value of the first element of each list, to the initial sum

Related

Get an array of corresponding values in a reference array from very big input array

I have the following array:
table = np.array([
[1.0, 1.0, 3.0, 5.0],
[1.0, 2.0, 5.0, 3.0],
...
[2.0, 5.0, 2.0, 1.0],
[8.0, 9.0, 7.0, 2.0]])
Let's name the different columns respectively by ['a', 'b', 'm', 'n'].
"table" is my my reference table where I want to extract 'm' and 'n' given 'a' and 'b' contained in a list we will call 'my_list'. In that list, we allow duplicate pairs (a, b).
N.B.: Here list can be referred as array (not in the python sense)
It is easier to do it with for loop. But, for my problem, my list 'my_list' can contain more than 100000 pairs (a, b) so doing it with for loop is not optimal for my work.
How can I do it with numpy functions or pandas functions in a few lines (1 to 3 lines)?
An example of what I want: Given the following list
my_list = np.array([
[1.0, 2.0],
[1.0, 2.0],
[8.0, 9.0]])
I want to have the following result:
results = np.array([
[5.0, 3.0],
[5.0, 3.0],
[7.0, 2.0]])
Thank you in advance
Edit 1: equivalence with for loop
Here is the equivalent with for loop (simplest way with for loop without dichotomous search):
result = []
for x in my_list:
for y in table:
if (x[0] == y[0]) and (x[1] == y[1]):
result.append([y[2], y[3]])
break
print(results)
One possible approach using pandas is to perform inner merge
pd.DataFrame(table).merge(pd.DataFrame(my_list))[[2, 3]].to_numpy()
array([[5., 3.],
[5., 3.],
[7., 2.]])

How to sum certain floats in a list

I wonder how to sum up only the floats in this list,
list = ['abc', 3.0, 2.0, 2.0, 0.0, 1.0, 0.0, 0.0]
I can't find out how to exclude the first string. I would like to do something with
range(1, len(list))
as it will need to work on lists with longer lengths, maybe something similar to it with the same effect? For python 3
You can use a generator in sum() and isinstance() to check if something is a float.
>>> lst = ['abc', 3.0, 2.0, 2.0, 0.0, 1.0, 0.0, 0.0]
>>> sum(x for x in lst if isinstance(x, float))
8.0
Note you should not use list as a variable name as it will overwrite the built in list class.
my_list = ['abc', 3.0, 2.0, 2.0, 0.0, 1.0, 0.0, 0.0]
sum = 0
for i in my_list:
if type(i) is float:
sum += i
print(sum)
This will result the sum to 8.0

How to find part of series in some series

The question is simple.
Suppose we have Series with this values:
srs = pd.Series([7.0, 2.0, 1.0, 2.0, 3.0, 5.0, 4.0])
How can I find place (index) of subseries 1.0, 2.0, 3.0?
Using a rolling window we can find the first occurrence of a list a.It puts a 'marker' (e.g. 0, any non-Nan value will be fine) at the end (right border) of the window. Then we use first_valid_index to find the index of this element and correct this value by the window size:
a = [1.0, 2.0, 3.0]
srs.rolling(len(a)).apply(lambda x: 0 if (x == a).all() else np.nan).first_valid_index()-len(a)+1
Output:
2
The simplest solution might be to use list comprehension:
a = srs.tolist() # [7.0, 2.0, 1.0, 2.0, 3.0, 5.0, 4.0]
b = [1.0, 2.0, 3.0]
[x for x in range(len(a)) if a[x:x+len(b)] == b]
# [2]
One naive way is to iterate over the series, subset the n elements and compare if they are equal to the given list:
Here the code:
srs = pd.Series([7.0, 2.0, 1.0, 2.0, 3.0, 5.0, 4.0])
sub_list = [1.0, 2.0, 3.0]
n = len(sub_list)
index_matching = []
for i in range(srs.shape[0] - n + 1):
sub_srs = srs.iloc[i: i+n]
if (sub_srs == sub_list).all():
index_matching.append(sub_srs.index)
print(index_matching)
# [RangeIndex(start=2, stop=5, step=1)]
Or in one line with list comprehension:
out = [srs.iloc[i:i+n].index for i in range(srs.shape[0] - n + 1) if (srs.iloc[i: i+n] == sub_list).all()]
print(out)
# [RangeIndex(start=2, stop=5, step=1)]
If you want an explicit list:
real_values = [[i for i in idx] for idx in out]
print(real_values)
# [[2, 3, 4]]

Creating List From Dictionary that Specifies Position by Index and Skips Some Index Positions

I am working with the following dictionary:
d = {'inds':[0, 3, 7, 3, 3, 5, 1], 'vals':[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]}
I am wanting to create a new_list that takes the values in list d['vals'] and places them in new_list by corresponding index in list d['inds']. The ultimate result should be:
[1.0, 7.0, 0.0, 11.0, 0.0, 6.0, 0.0, 3.0]
This takes the following:
d['inds'] == [0, 3, 7, 3, 3, 5, 1]
d['vals'] == [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
For any index position not included in d['inds'] the corresponding value is 0.0.
For index positions that are repeated, the True value for that position is the sum of the individual values. For example, above 3 is repeated 3 times; so, new_list[3] should == 11, which is the sum of 2.0 + 4.0 + 5.0.
First, allocate a list of the appropriate length and full of zeroes:
result = [0] * (max(d['inds']) + 1)
Then loop over the indices and values and add them to the values in the list:
for ind, value in zip(d['inds'], d['vals']):
result[ind] += value
Output:
>>> result
[1.0, 7.0, 0, 11.0, 0, 6.0, 0, 3.0]
After collaborating with a co-worker, who helped walked me through this, the following was arrived at for a more dynamic function (to allow for different lengths of the resulting list):
import numpy as np
d ={
'inds': [0,3,7,3,3,5,1],
'vals': list(range(1,8))}
## this assumes the values in the list associated with the 'vals' key
## remain in numerical order due to range function.
def newlist(dictionary, length) ##length must be at least max(d['inds'])+1
out = np.zeroes(length)
for i in range (len(dictionary['inds'])):
out[dictionary['inds'][i]] += d['vals'][i]
return(out)

how to conditionally replace values in list of lists in python

I am a python new bie.
I have two list of lists like this:
A = List[List[float,float]]
B = List[List[float,float]]
both different sizes
Ex :
A has [time, value]
B has [start_time, end_time]
A = [[0.0,10.0] , [1.0,10.0], [2.0,10.0], [3.0,10.0], [4.0,10.0], [5.0,10.0], [6.0,10.0]]
B = [[0.0,2.0], [5.0,6.0]]
What I am trying to do is :
if A has a time which is not in B, I should make the corresponding 'value' in A to zero.
So output would be :
[[0.0,10.0] , [1.0,10.0], [2.0,10.0], [3.0,0.0], [4.0,0.0], [5.0,10.0], [6.0,10.0]]
i.e if a time segment in B has no corresponding time value present in A, the value corresponding to that time should be made zero. In this case, the values between 2 and 5 are there in B , so partner values of'3' and '4' in A are made to zeroes.
Please tell me how to do it.
I have referred here: Python - How to change values in a list of lists? and List of lists into numpy array
So One idea i got was to convert B into single list and then compare values from AA and A. However I haven't made much progress.
AA = numpy.hstack(B) # for getting array of times
for i in 1: len(AA):
if (AA[i]==A[
For simple problems, a nested loop design can often get you to a quick solution without needing to worry about the specifics of list-flattening functions.
for i in range(len(A)):
time = A[i][0]
isValidTime = False
for time_segment in B:
if time_segment[0] <= time <= time_segment[1]:
isValidTime = True
break
if not isValidTime:
A[i][1] = 0.0
Edit: Just to be clear, including the 'break' statement isn't necessary to get to the solution, but it helps avoid unnecessary computation. If we've determined that an item in A does have a valid time, we can safely stop searching through the time segments in B and move on to the next item of A.
If you flatten B, then you can compare it:
A = [[0.0, 10.0], [1.0, 10.0], [2.0, 10.0], [3.0, 10.0], [4.0, 10.0], [5.0, 10.0], [6.0, 10.0]]
B = [[0.0, 2.0], [5.0, 6.0]]
Bvals = [item for sublist in B for item in sublist]
print(Bvals)
newA = [x if x[0] in Bvals else [x[0], 0.0] for x in A]
print(newA)
Outputs:
[0.0, 2.0, 5.0, 6.0]
[[0.0, 10.0], [1.0, 0.0], [2.0, 10.0], [3.0, 0.0], [4.0, 0.0], [5.0, 10.0], [6.0, 10.0]]
I suppose the B list contains time intervals, right? In that case, you can do something like the following:
updated = [x if any([start <= x[0] end <= for start, end in B]) else [x[0], 0] for x in A]
That may be a bit too compact for some people, but it essentially does the same as the following:
updated = []
for time, value in A:
for start, end in B:
if start <= time and time <= end:
updated.append([time, value])
break
updated.append([time, 0])
On a side note, if you are doing interval checking, this is probably not the most efficient way to do it. Take a look at interval trees, which is a data structure for performing various interval-related queries (yours included).
A Pythonic one-liner:
[[t,v if any(s <= t and t <= e for s,e in B) else 0.0] for t,v in A]
which gives:
[[0.0, 10.0], [1.0, 10.0], [2.0, 10.0], [3.0, 0.0], [4.0, 0.0], [5.0, 10.0], [6.0, 10.0]]

Categories

Resources