This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 12 months ago.
Does anyone know how I can turn the following list comprehension into two nested for loops? When I try and do it I get errors.
data_list = [self.y[a][b] for a in range(side_1, side_2) for b in range(corner_1, corner_2)]
data_list = []
for a in range(side_1, side_2):
for b in range(corner_1, corner_2):
data_list.append(self.y[a][b])
If you need to iterate over every row / column index pair, you should use itertools.product() as a more concise solution than a nested for loop:
from itertools import product
result = []
for a, b in product(range(side_1, side_2), range(corner_1, corner_2)):
result.append(self.y[a][b])
This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]
This question already has answers here:
Count duplicates between 2 lists
(5 answers)
Closed 2 years ago.
I have those two lists:
sub_list=['person1','person2']
global_list=['person1','person2','person3','person4','person5']
I want to know how many elements from sub_list are in global_list (2), is there a one liner?
Try set:
len(set(sub_list) & set(global_list))
Using sum and list comprehension
sum([e in global_list for e in sub_list])
>>> 2
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
how can I create a new list with the following format.
The 3 arrays inside each row should be in different rows.
a= [['111,0.0,1', '111,1.27,2', '111,3.47,3'],
['222,0.0,1', '222,1.27,2', '222,3.47,3'],
['33,0.0,1', '33,1.27,2', '33,3.47,3'],
['44,0.0,1', '44,1.27,2', '4,3.47,3'],
['55,0.0,1', '55,1.27,2', '55,3.47,3']]
Final desired ouput:
b=[['111,0.0,1',
'111,1.27,2',
'111,3.47,3',
'222,0.0,1',
'222,1.27,2',
'222,3.47,3',
'33,0.0,1',
'33,1.27,2',
'33,3.47,3',
'44,0.0,1',
'44,1.27,2',
'44,3.47,3',
'55,0.0,1',
'55,1.27,2',
'55,3.47,3']]
Is this what you are looking for?
b = [[j for i in a for j in i]]
To be clear, there is no concept of rows vs. columns in Python. Your end result is just a big list of str's, within another list.
You can create the big list by chaining all of the original small lists together (a[0] + a[1] + ...), for which we may use
import itertools
big_list = list(itertools.chain(*a))
To put this inside another list,
b = [big_list]
This question already has answers here:
Zip lists in Python
(10 answers)
Closed 4 years ago.
I'm having trouble figuring out how to merge two lists so that each item in each list is merged with the other one based on what place its in. Both of the lists are of equal length. For example:
xList=[abc,zxc,qwe]
yList=[1,2,3]
I need
[abc1,zxc2,qwe3].
I'm hoping I can make a loop that will be able to handle really long lists that will do this for me.
zip is your friend:
>>> xList=['abc','zxc','qwe']
>>> yList=[1,2,3]
>>> [x+str(y) for x,y in zip(xList,yList)]
['abc1', 'zxc2', 'qwe3']
Using map + lambda:
xList=['abc','zxc','qwe']
yList=[1,2,3]
print(list(map(lambda x,y: x+str(y),xList,yList)))
Output:
['abc1', 'zxc2', 'qwe3']