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']
Related
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']]
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:
How do I iterate through two lists in parallel?
(8 answers)
Closed 2 years ago.
I have a dataframe with 2 columns name and age
I want to make a list of dictionaries containing their value of name and age which is like this
list = [{'a':'14'}, {'b':'21'}, {'c':'12'}]
I've tried using list = [{i:j} for i,j in df['name'],df['age']] but apparently it cannot loop from 2 for in. How can I loop the list of dictionaries?
EDIT
I have completed it with list = [{i:j} for i,j in zip(df['name'],df['age'])]
Thank you for the answers!
I don't know if it works but try this
list = [{i:j} for i,j in zip(df['name'],df['age'])]
you should be able to do that with zip. Try: [{i:j} for i,j in zip(df['name'],df['age'])]
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:
Python -Intersection of multiple lists?
(6 answers)
Closed 6 years ago.
I have a list of lists, like follow:
list = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]
I want to find the intersection of them in python 2.7, I mean
intersect_list = [3]
Thanks.
First, don't use list as a variable name - it hides the built in class.
Next, this will do it
>>> a = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]
>>> set.intersection(*map(set,a))
{3}
The map(set,a) simply converts it to a list of sets. Then you just unpack the list and find the intersection.
If you really need the result as a list, just wrap the call with list(...)