Pythonic way to group a list of lists? [duplicate] - python

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 12 months ago.
So lets say I have a hypothetical list of lists of file names in Python defined like so:
l = [["user1/stats1.csv", "user1/stats2.csv", "user1/stats3.csv"],
["user2/stats1.csv", "user2/stats2.csv", "user2/stats3.csv"]]
What would be the most pythonic way to group it by the number in statsN.csv such that the list would look like:
l = [["user1/stats1.csv", "user2/stats1.csv"],
["user1/stats2.csv", "user2/stats2.csv"],
["user1/stats3.csv", "user2/stats3.csv"],
For reference, the original list was obtained by using glob with the * wild card a la glob.glob("user1/stats*.csv") and glob.glob("user2/stats*.csv")

You could unpack the sublists and zip:
out = list(map(list, zip(*l)))
Output:
[['user1/stats1.csv', 'user2/stats1.csv'],
['user1/stats2.csv', 'user2/stats2.csv'],
['user1/stats3.csv', 'user2/stats3.csv']]

If you're using numpy, you can simply transpose:
>>> np.array(l).T.tolist()
[['user1/stats1.csv', 'user2/stats1.csv'],
['user1/stats2.csv', 'user2/stats2.csv'],
['user1/stats3.csv', 'user2/stats3.csv']]

Related

Undoing list comprehension and turning it in nested for loops [duplicate]

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])

Lists in Python - Each array in different row [duplicate]

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]

Merge all items in a list [duplicate]

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']

what is the pythonic way to create a list combinations [duplicate]

This question already has answers here:
Combining lists in python
(2 answers)
Closed 5 years ago.
Let's assume I have two lists:
list_one = [1A, 1B]
list_two = [2A]
How can I get a list containing pairs of words from both these arrays? To visualize:
answer = [1A-2A, 1B-2A]
I know I can do it using two for loops but is there a 'pythonic' way?
You can simply use product from itertools.
from itertools import product
list_one = ['1A', '1B']
list_two = ['2A']
result = ['-'.join(sub) for sub in product(list_one, list_two)]
print(result) # -> ['1A-2A', '1B-2A']
I do not think it gets any more pythonic than this.
Under the hood, product(list_one, list_two) generates tuples of the form ('1A', '2A') which we simply '-'.join() to produce the desired result.

Python list of lists to list [duplicate]

This question already has answers here:
Closed 10 years ago.
I have a fairly simple question (I think).
I have a list of lists in python, and the elements are strings. I wish to have a single list, with elements that are floats.
For example:
lst= [['0.0375'], ['-0.1652'], ['0.1841'], ['-0.0304'], ['0.0211'], ['0.1580'], ['-0.0252'], ['-0.0080'], ['-0.0915'], ['0.1208']]
And I need to have something like:
lst= [0.0375, -0.1652, 0.1841, -0.0304, 0.0211, 0.1580, -0.0252, -0.0080, -0.0915, 0.1208]
[float(x) for (x,) in your_list]

Categories