Combination of list of list elements [duplicate] - python

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 4 years ago.
I want a list which is a combination of list of list elements
For example:
my input
x = [['P'], ['E', 'C'], ['E', 'P', 'C']]
The output should be
['PEE','PEP','PEC','PCE','PCP','PCC']]
Any help is highly appreciated.

Use itertools
[''.join(i) for i in itertools.product(*x)]
Note: assuming that last one should be 'PCC'

here is a solution
def comb(character_list_list):
res = ['']
for character_list in character_list_list:
res = [s+c for s in res for c in character_list]
return res
On your example, it gives, as expected
>>> comb([['P'], ['E', 'C'], ['E', 'P', 'C']])
['PEE', 'PEP', 'PEC', 'PCE', 'PCP', 'PCC']
A shorter version is possible using functools.reduce(), but the use of this function is not recommanded.

Related

Create unique cartesian product from a list [duplicate]

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 2 years ago.
How do I create a cartesian product of a list itself with unique elements?
For example, lists = ['a', 'b', 'c'], and I want to create [['a', 'b'], ['a','c'], ['b','c']].
from itertools import combinations
lists = ['a', 'b', 'c']
print(list(map(list, combinations(lists,2))))

Check for similarities in multiple lists and return match/ [duplicate]

This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 3 years ago.
I have four lists with strings:
a = ['A', 'B', 'GG', 'Z']
b = ['A', 'F', 'GG', 'Z']
c = ['A', 'P', 'E', 'Z']
d = ['A', 'T', 'R', 'Z']
and I want to return a single list that has the string that appears in all lists to get:
final_list = ['A', 'Z']
I know you can use set and intersection but these use at most one argument whilst I have four. Is there another way of doing this?
Thank you.
EDIT
I tried:
final_list = set(a).intersection(b,c,d)
Does it matter which order the lists are placed?
Using a list comprehension along with zip with an equality check:
[chars[0] for chars in zip(a,b,c,d) if len(set(chars)) <= 1]

Printing elements of a set in python [duplicate]

This question already has answers here:
Converting a list to a set changes element order
(16 answers)
Closed 4 years ago.
I want to print the elements of the set consecutively, so I wrote the following code:
s='dmfgd'
print(set(s))
However, this code shows the output as:
set(['m', 'd', 'g', 'f'])
but, I want output like:
set(['d','m','f','g'])
Any help will be appreciated.
Set is unordered. You can instead use a list of dict keys to emulate an ordered set if you're using Python 3.6+:
print(list(dict.fromkeys(s)))
This outputs:
['d', 'm', 'f', 'g']
Python set is unordered collections of unique elements
Try:
s='dmfgd'
def removeDups(s):
res = []
for i in s:
if i not in res:
res.append(i)
return res
print(removeDups(s))
Output:
['d', 'm', 'f', 'g']

How to combine every element of a list to the other list? [duplicate]

This question already has answers here:
Element-wise addition of 2 lists?
(17 answers)
Closed 5 years ago.
Suppose there are two lists:
['a', 'b', 'c'], ['d', 'e', 'f']
what I want is:
'ad','ae','af','bd','be','bf','cd','ce','cf'
How can I get this without recursion or list comprehension? I mean only use loops, using python?
The itertools module implements a lot of loop-like things:
combined = []
for pair in itertools.product(['a', 'b', 'c'], ['d', 'e', 'f']):
combined.append(''.join(pair))
While iterating through the elements in the first array, you should iterate all of the elements in the second array and push the combined result into the new list.
first_list = ['a', 'b', 'c']
second_list = ['d', 'e', 'f']
combined_list = []
for i in first_list:
for j in second_list:
combined_list.append(i + j)
print(combined_list)
This concept is called a Cartesian product, and the stdlib itertools.product will build one for you - the only problem is it will give you tuples like ('a', 'd') instead of strings, but you can just pass them through join for the result you want:
from itertools import product
print(*map(''.join, product (['a','b,'c'],['d','e','f']))

Getting Second-to-Last Element in List [duplicate]

This question already has answers here:
How do I get the last element of a list?
(25 answers)
Closed 6 years ago.
I am able to get the second-to-last element of a list with the following:
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(lst[len(lst)-2])
e
Is there a better way than using print(lst[len(lst)-2]) to achieve this same result?
There is: negative indices:
lst[-2]

Categories