Get a fixed number of items from a generator [duplicate] - python

This question already has answers here:
How to get the n next values of a generator into a list
(5 answers)
Closed 5 years ago.
What is the most efficient way to get a fixed number of items from a generator?
What I currently do is using zip and range. In this example I take
chunks of size 3 from the generator.
def f():
x = 0
while x < 21:
yield x
x += 1
g = f()
while True:
x = [i for _, i in zip(range(3), g)]
if not x:
break
print x
The background is that the database I use provides a generator object for query results. Than I fill a fixed size numpy array with data and process it as one batch.

Use itertools.islice:
import itertools
for elem in itertools.islice(f(), 3):
print elem
and directly into your numpy array:
my_arr = np.array(itertools.islice(f(), 3))

Related

Changing elements of a list of lists [duplicate]

This question already has answers here:
How to add 1 to every element of a matrix / nested list in Python?
(4 answers)
Closed 1 year ago.
I need to add 0.05 to elements of coords array. But nothing happens.
Can you advise me where is the mistake? (I thought this would be easy but no)
coords = [[0.1,0.1,0.1],
[0.2,0.2,0.2],
[0.3,0.3,0.3]]
for i in coords:
for j in i:
j = j+0.05
print(coords)
Your solution doesn't actually modify the elements in the list by iterating over j. You could try a double list comprehension like this:
coords[:] = [j+0.05 for i in coords for j in i]
This has the advantage of editing the original object without creating a new instance of a list.
You current way will not store the results, as you did not provide a list to hold the results. I can show you two more straightforward examples:
a = [1,2,3,4]
for number in a:
number = number + 1
# check results
# nothing happens
print(a)
But if you do:
# you will get results
b = [number + 1 for number in a]
print(b)
The only difference is that you need to provide a list to hold the data.
Try this:
coords = [[0.1,0.1,0.1],
[0.2,0.2,0.2],
[0.3,0.3,0.3]]
for i in range(len(coords)):
for j in range(len(coords[i])):
coords[i][j] += .05
print(coords)
You can use lst comprehension
coords = [[0.1,0.1,0.1],
[0.2,0.2,0.2],
[0.3,0.3,0.3]]
newCoords = [[num+0.05 for num in lst] for lst in coords ]
print(newCoords)

How to get mean from list that is partially empty example [0,2,4,0,4,0,0,0,0,10] [duplicate]

This question already has answers here:
Computing average of non-zero values
(3 answers)
Closed 3 years ago.
How to get mean from list that is partially empty example [0,2,4,0,4,0,0,0,0,10]
So mean should be (2+4+4+10)/4 = 5 , but statistics.mean() divides by the overall amount of numbers in the list (which in this case is 10).
So in my case I need to get mean from the list of numbers which are divisible by 3 (list b)
import random
import statistics
a = []
b = []
c = [0,2,4,0,4,0,0,0,0,10]
for x in range (10):
a.append(random.randint(0,101))
for x in a:
if x%3==0:
b.append(x)
else:
b.append(0)
average = statistics.mean(b)
print('a',a)
print('b',b)
print('Srednia',average)
You can filter out the zeros from the list using a for-loop perhaps
from statistics import mean
c = [0,2,4,0,4,0,0,0,0,10]
#Filter out 0's
non_zero_c = [item for item in c if item]
#Calculate mean from non zero numbers
print(mean(non_zero_c))
The output will be 5
In alternative to creating a list of only non-zero elements you can count them
non_zero_mean = sum(x) / sum(element != 0 for element in x)
here I'm using the "trick" that from a math point of view in Python (like in C and C++) True == 1 and False == 0.
Another alternative is
non_zero_mean = sum(x) / (len(x) - x.count(0))
Very quickly, the easiest way is to use some list comprehension to select the part of the list that interest you.
Example:
"How to get mean from list that is partially empty example [0,2,4,0,4,0,0,0,0,10] "
a = [0,2,4,0,4,0,0,0,0,10]
a_selection = [elt for elt in a if elt != 0]
m = sum(a_selection)/len(a_selection)
Same for divisible by 3:
b_selected = [elt for elt in b if elt%3==0]

python printing all subsets of a list [duplicate]

This question already has answers here:
How to get all subsets of a set? (powerset)
(32 answers)
Closed 4 years ago.
[1,2,3]
I have this list and want to print all subsets of this list using recursion. I am getting the output using the following code but is there any better/easier way? Without typecasting it to a set.
def recur_subset(arr):
if len(arr)==0:
print(arr)
else:
for i in range(0, len(arr)):
print(arr[0:i+1])
for j in range(2, len(arr)):
print([arr[0], arr[j]])
return recur_subset(arr[1:len(arr)])
You could use itertools to do the heavy lifting:
import itertools
def recur_subset( s, l=None ):
if l == None:
l = len(s)
if l > 0:
for x in itertools.combinations( s, l ):
print(list(x))
recur_subset( s, l-1 )
Note: This only prints non-empty subsets; add else: print([]) at the end to include the empty subset.

How to calculate numbers in a list [duplicate]

This question already has answers here:
Python: Finding differences between elements of a list
(12 answers)
Difference between consecutive elements in list [duplicate]
(3 answers)
Closed 4 years ago.
here is my code.
A = [86.14803712, 85.25496701, 86.50334271, 86.0266668, 86.61455594, 86.90445213, 86.65519315, 87.10116762, 87.08173861]
B = []
i = 0
for i in range(len(A)):
c = A[i]-A[i-1]
B.append(c)
print(c)
I want to get the differences between two continuous numbers in this list, eg,(85.25496701-86.14803712). So in the results, I should have eight numbers as results.
But the results I get are:
-0.9337014900000042
-0.8930701099999965
1.2483756999999969
-0.4766759099999973
0.5878891400000015
0.2898961899999932
-0.24925897999999336
0.4459744699999959
-0.019429009999996083
I don't need -0.9337014900000042 since it comes from the first number subtract the last number in the list. What should I do the fix it? Thanks
That's the strength and the weakness of python: index -1 is always valid when the list isn't empty, which can lead to programs not crashing but not doing what you want.
For those operations, it's better to use zip to interleave the list with a sliced version of itself without the first number:
A = [86.14803712, 85.25496701, 86.50334271, 86.0266668, 86.61455594, 86.90445213, 86.65519315, 87.10116762, 87.08173861]
diffs = [ac-ap for ac,ap in zip(A[1:],A)]
or with itertools.islice to avoid creating a new list to iterate on it:
import itertools
diffs = [ac-ap for ac,ap in zip(itertools.islice(A,1,None),A)]
result (8 values):
[-0.8930701099999965, 1.2483756999999969, -0.4766759099999973, 0.5878891400000015, 0.2898961899999932, -0.24925897999999336, 0.4459744699999959, -0.019429009999996083]
It's possible to do this in base Python, but you might like the semantic clarity of Pandas:
import pandas as pd
pd.Series(A).diff().values[1:]
array([-0.89307011, 1.2483757 , -0.47667591, 0.58788914, 0.28989619,
-0.24925898, 0.44597447, -0.01942901])
You can just do:
B = [x-y for x, y in zip(A[1:], A)]
print(B) # -> [-0.8930701099999965, 1.2483756999999969, -0.4766759099999973, 0.5878891400000015, 0.2898961899999932, -0.24925897999999336, 0.4459744699999959, -0.019429009999996083]
You need to make sure you star from a correct index. In your current code, in the first iteration of the loop you will be computing A[0] - A[-1]. Hence, you need to start i from 1 to ensure in the first iteration you compute the value of A[1] - A[0]. The corrected version of your code is here:
A = [86.14803712, 85.25496701, 86.50334271, 86.0266668, 86.61455594, 86.90445213, 86.65519315, 87.10116762, 87.08173861]
B = []
i = 0
for i in range(1, len(A)):
c = A[i]-A[i-1]
B.append(c)
print(c)
I think the problem is that the loop subtracts the first element to the last because the loop starts at index 0 and subtracts it from index -1 (python takes -1 as the last index of a list). A better solution imo would be:
A = [86.14803712, 85.25496701, 86.50334271, 86.0266668, 86.61455594,
86.90445213, 86.65519315, 87.10116762, 87.08173861]
B = []
i = 0
for i in range(len(A)-1):
c = -(A[i]-A[i+1])
B.append(c)
print(c)
The easiest would be:
result = [x-y for x,y in zip(A[1:], A[:-1])]

Given a list [9,2,11].Find the biggest combination like 9211 in python [duplicate]

This question already has answers here:
Sort a list to form the largest possible number
(9 answers)
Closed 6 years ago.
x = [9,2,1]
l=[]
for i in range(len(x)):
p = max(x)
l.append(p)
x.remove(p)
print(l)
b = int(''.join(str(n) for n in l))
print(b)
The answer I got is 921
But if the list is [9,2,11].This won't work.
x = [9,2,11]
from itertools import combinations
l=combinations(x, len(x))
print max(map(lambda k: int("".join(map(str,k))),l))
You can use combinations here.
Output:
9211

Categories

Resources