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

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)

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

Transposing Lists - Python [duplicate]

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

count an element in a list with list inside in python [duplicate]

This question already has answers here:
Nested List and count()
(8 answers)
Closed 4 years ago.
I have a list with one list inside and I would like to count how many times is one element repeat. for example:
list = ['a','b','c',['a','d']]
find = 'a'
list.count(find)
The ouptput is 1, but I'm looking for 2.
There is any easy way to do it?
thanks
Archive with chain.from_iterable
from itertools import chain
print(list(chain.from_iterable(lst)).count('a'))
First make your list flatten and get the count.

How to get the last number from list in python [duplicate]

This question already has answers here:
How do I get the last element of a list?
(25 answers)
Closed 8 years ago.
Suppose I have the list as
a = [0.0021, 0.12, 0.1224, 0.22]
I have to extract the last number from the above list, so my answer should be 0.22 without using a[3], because the number of the elements in the list always keep changing.
You're talking about a list. Arrays in python are usually numpy.arrays. They are a completely different data structure.
You can achieve what you want like this:
>>> array = [0.0021, 0.12, 0.1224, 0.22]
>>> array[-1]
0.22
>>>
Negative indexing starts at the end of the list, thus array[-1] will always be the last element in the list, array[-2] the second last and so forth.
The appropriate name of [...] is a list. As you know, you can access to an element of a list using an index, like
some_list = [1, 2, 3]
print some_list[0] # first element
But you can also use negative indices:
print some_list[-1] # last element: 3
print some_list[-2] # one before the last element: 2
Note that this will "count" elements from right to left
Don't worry! Try them!
a[len(a)-1]
or
a[-1]

Find index of last item in a list [duplicate]

This question already has answers here:
How to find the last occurrence of an item in a Python list
(15 answers)
Closed 8 years ago.
For example, I have a list
[0,2,2,3,2,1]
I want to find the index of the last '2' that appears in this list.
Is there an easy way to do this?
You can try the following approach. First reverse the list, get the index using L.index().
Since you reversed the list, you are getting an index that corresponds to the reversed, so to "convert" it to the respective index in the original list, you will have to substract 1 and the index from the length of the list.
n = ...
print len(L) - L[::-1].index(n) - 1

Categories