Transposing Lists - Python [duplicate] - python

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 2 years ago.
I have list a = [[1,2,3],[4,5,6],[7,8,9]] and want to convert that into list b = [[1,4,7],[2,5,8],[3,6,9]]. Is it possible to do that?
i.e. take all the value in the first element spot and combine into a new list, same for 2nd, 3rd etc...
Assume the initial list has an unknown amount of elements and each element has unknown length, i.e. there could be 40 elements in a and each element contains 14 numbers. Thanks

There are a couple of ways to do it.
The first one is as follows:
print(list(map(list, zip(*a))))
another option is using numpy as follows:
print(np.array(a).T.tolist())
Another option is to use list comprehension as follows:
print([[row[i] for row in a] for i in range(len(a[0]))])

Using numpy you can try this
import numpy as np
a= [[1,2,3],[4,5,6],[7,8,9]]
ar = np.array(a)
a_transpose = at.T.tolist()

Related

Finding the number of combinations possible, given 4 dictionaries [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 3 months ago.
Given the following dictionaries:
dict_first_attempt = {'Offense': ['Jack','Jill','Tim'],
'Defense':['Robert','Kevin','Sam']}
dict_second_attempt = {'Offense': ['Jack','McKayla','Heather'],
'Defense':['Chris','Tim','Julia']}
From this dictionaries, my focus is just the offense, so if I just wanted the list of those, I would do this:
first = dict_first_attempt['Offense']
second = dict_second_attempt['Offense']
For each of those lists, I am trying to create a code that can do the following:
Tell me all the possible combinations of first attempt offense and second attempt offense.
Outputs it in a list, with lists of the combinations.
The first element within the list has to be from the first attempt offense, and the second element has to be from the second attempt offense.
An example of the type of output I want is:
[['Jack','Jack'],['Jack','McKayla'],['Jack','Heather'],
['Jill','Jack'],['Jill','McKayla'],['Jill','Heather'],
['Tim','Jack'],['Tim','McKayla'],['Tim','Heather']]
import itertools
list(itertools.product(first, second))

Average entries of lists of lists [duplicate]

This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Transpose list of lists
(14 answers)
Closed 1 year ago.
I have a list of lists
my_list = [[8,2,3], [8,3,6], [2,3,9], [7,3,6], [2,6,4], [8,5,8]]
and would like to take the average of the last n entries of the list for each of its entries. To clarify, here is an example:
The average of the entries of the last 3 lists of my_list is:
[(7+2+8)/3, (3+6+5)/3, (6+4+8)/3] = [5.67, 4.67, 6]
I did it with a bunch of loops of loops but I feel like there is a way to do it in probably a line or two of code. Any help is appreciated. Thanks
use np.mean after you slice your list
import numpy as np
my_list = [[8,2,3], [8,3,6], [2,3,9], [7,3,6], [2,6,4], [8,5,8]]
n = 3
np.mean(my_list[-n:], axis=0) # -> array([5.66666667, 4.66666667, 6. ])
Use a list comprehension and zip, with some help from statistics.mean to easily compute the mean:
my_list = [[8,2,3], [8,3,6], [2,3,9], [7,3,6], [2,6,4], [8,5,8]]
n = 3
from statistics import mean
[mean(x) for x in zip(*my_list[-n:])]
# OR using map
list(map(mean, zip(*my_list[-n:])))
output:
[5.666666666666667, 4.666666666666667, 6]

Perform an operation to each item of a list [duplicate]

This question already has answers here:
Divide elements of a list by integer with list comprehension: index out of range
(5 answers)
Closed 3 years ago.
I have a list of numbers and i need to divide them all by a specific number.
Lets say i want to divide all of the items by 2
list = [1,2,3,4,5,6,7]
wanted_list = [1/2,2/2,3/2,4/2,5/2,6/2,7/2]
I attempted a for loop that changes each but it didnt work for some reason like it didnt do the operation.
list = [1,2,3,4,5,6,7]
wanted_list = [i/2 for i in list]
print(wanted_list)
I assume san's answer is the best approach, as it utilizes lists comprehension and in that case a new list is created automatically, however considering what you wrote, you can as well use a for loop, only you need to store the result somewhere, ex:
data = [2, 4, 6]
wanted_data = []
for d in data:
wanted_data.append(int(d/2))
print(wanted_data)

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]

Mix 3 Lists, Each List 35 Elements Python3 [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 5 years ago.
I have 3 lists like
_1st = ["Qa4AJ-","Qb4AJ-","Qc4AJ-","Qd4AJ-","Qe4AJ-","Qf4AJ-","Qg4AJ-","Qh4AJ-","Qi4AJ-","QJ4AJ-","Qk4AJ-","Ql4AJ-","Qm4AJ-","Qn4AJ-","Qo4AJ-","Qp4AJ-","Qq4AJ-","Qr4AJ-","Qs4AJ-","Qt4AJ-","Qu4AJ-","Qv4AJ-","Qw4AJ-","Qx4AJ-","Qy4AJ-","Qz4AJ-","Q14AJ-","Q24AJ-","Q34AJ-","Q44AJ-","Q54AJ-","Q64AJ-","Q74AJ-","Q84AJ-","Q94AJ-"]
_2nd = ["H581A-","H582A-","H583A-","H584A-","H585A-","H586A-","H587A-","H588A-","H589A-","H58aA-","H58bA-","H58cA-","H58dA-","H58eA-","H58fA-","H58gA-","H58hA-","H58iA-","H58jA-","H58kA-","H58lA-","H58mA-","H58nA-","H58oA-","H58pA-","H58qA-","H58rA-","H58sA-","H58tA-","H58uA-","H58vA-","H58wA-","H58xA-","H58yA-","H58zA-"]
_3rd = ["KNaQ3","KNbQ3","KNcQ3","KNdQ3","KNeQ3","KNfQ3","KNgQ3","KNhQ3","KNiQ3","KNjQ3","KNkQ3","KNlQ3","KNmQ3","KNnQ3","KNoQ3","KNpQ3","KNqQ3","KNrQ3","KNsQ3","KNtQ3","KNuQ3","KNvQ3","KNwQ3","KNxQ3","KNyQ3","KNzQ3","KN1Q3","KN2Q3","KN3Q3","KN4Q3","KN5Q3","KN6Q3","KN7Q3","KN8Q3","KN9Q3"]
I want to mix them together
For example, I want Python print 1st element of _1st with 1st element of _2nd and 1st element of _3rd
and then print 1st element of _1st with 1st element of _2nd and 2nd element of _3rd.
I need it to mix every single element from every list to other elements from other lists
I have no Idea "How I can do it".
I'm not sure if I clearly said what I want, but I hope u get it.
The thing you're looking for is the 'cartesian product' of these lists
In Python 2.6+
import itertools
for element in itertools.product(_1st, _2nd, _3rd):
print(element)

Categories