Create unique cartesian product from a list [duplicate] - python

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

Related

Function that takes list input, and returns a new list containing only the elements in that are repeated [duplicate]

This question already has answers here:
How do I find the duplicates in a list and create another list with them?
(42 answers)
Closed 5 months ago.
>>> list_a = ['a', 'b', 'c', 'c']
>>> get_repeated(list_a)
['c']
What would be the most pythonic way to do function get_repeated()?
this should work
from collections import Counter
list_a = ['a', 'b', 'c', 'c']
count = Counter(list_a)
output = [key for key, val in count.items() if val > 1]
print(output)
>>> ['c']

Append list to itself in python [duplicate]

This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 2 years ago.
When I have a list, say mylist = ['a', 'b', 'c'] and I try to append it to its self
mylist.append(mylist)
I get
['a', 'b', 'c', [...]]
Which is a recursive list, for example, mylist[3][3][1] outputs 'b'
I expected to get
['a', 'b', 'c', ['a', 'b', 'c']]
My first question is how can I get what I expected (if there are many methods I would prefer the most performant).
My second question, is what's the reason for this "unexpected" behaviour.
Thanks.
I use python3.8.2
You can use list comprehensions:
mylist.append([x for x in mylist])

Combination of list of list elements [duplicate]

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.

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