This question already has answers here:
Interleave different length lists, elimating duplicates, and preserve order
(7 answers)
Closed 1 year ago.
I'd like to merge to lists with many similar elements preserving the order as much as possible.
So say for example we have this:
list1=[1,2,3,4,6,7,8,9]
list2=[1,2,3,4,5,8,10]
list1+list2=[1,2,3,4,6,5,7,8,9,10]
or maybe:
list1+list2=[1,2,3,4,5,6,7,8,10,9]
The idea is that 1,2,3,4 all match. Then we encounter 5 in list1 and 6 in list2 and we insert them next to each other in the merge list. If there are more elements in one list than the other then the extra rows are just appended to the end.
For unsorted output-
list(set(list1+list2))
For sorted output-
list(sorted(set(list1+list2)))
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.
This question already has answers here:
Iterating over two lists one after another
(4 answers)
Closed 4 years ago.
I would like to create a list from elements of 2 different lists using list comprehensions.
For instance, assuming my 2 lists are men and women, I want a single list with all names:
men_names = [man.name for man in men]
women_names = [woman.name for woman in women]
all_names = men_names + women_names
Is there a one-line solution to obtain this list? Thanks!
EDIT: Using a list comprehension is a requirement because in my particular case it should be very much faster than building the list in a for loop, and performance is an issue.
Using itertools.chain is a way to achieve this without creating an intermediate list.
from itertools import chain
all_names = [person.name for person in chain(men, women)]
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)
This question already has answers here:
Selecting a random list element of length n in Python
(4 answers)
How do you pick "x" number of unique numbers from a list in Python?
(7 answers)
Closed 5 years ago.
Let's say i have a list. I want to iterate over that list and add 2 random strings from it to an empty list. However, because this is a random choice, there is a possibility that the same string will be picked twice (As it is crucial not to pop or delete from that list the selected item). something like this:
import random
emptylist = []
somelist = ["a","b","c","d","e",]
for item in somelist:
emptylist.append(random.choice(somelist))
emptylist.append(random.choice(somelist))
How do i make sure that it won't pick, for example, "a" twice?
I know it is possible in many ways but im looking for the most efficient one.