Find all combinations of a list in python - python-3.x

The list that I have:
a = [1,2,3]
The output that I want:
combinations = [11, 12, 13, 21, 22, 23, 31, 32, 33]
I have tried:
a = [1,2,3]
all_combinations = []
list1_permutations = itertools.permutations(a, len(a))
for each_permutation in list1_permutations:
zipped = zip(each_permutation, a)
all_combinations.append(list(zipped))
print(all_combinations)
But I am getting the output like:
[[(1, 1), (2, 2), (3, 3)], [(1, 1), (3, 2), (2, 3)], [(2, 1), (1, 2), (3, 3)], [(2, 1), (3, 2), (1, 3)], [(3, 1), (1, 2), (2, 3)], [(3, 1), (2, 2), (1, 3)]]

This might be easiest with a nested list comprehension:
a = [1, 2, 3]
out = [int(f'{i}{j}') for i in a for j in a]
print(out)
Output:
[11, 12, 13, 21, 22, 23, 31, 32, 33]
The same result can be achieved (perhaps more efficiently) with itertools.product:
import itertools
a = [1, 2, 3]
out = [int(f"{a}{b}") for a, b in itertools.product(a, a)]

This should work.
you can use a list comprehension to make all combos because you appear to want to sample with duplicates like '33'
you can use a list gen to do this
you need to treat the items like strings to join them
you need to convert it back to integer if that is what you want as final result
a=[1,2,3]
result = [int(''.join([str(i), str(j)])) for i in a for j in a]
print(result)

Related

How to iterate python windowed() to last element?

According to the more_itertools.windowed specification, you can do:
list(windowed(seq=[1, 2, 3, 4], n=2, step=1))
>>> [(1, 2), (2, 3), (3, 4)]
But what if I want to run it all to the end? Is it possible to get:
>>> [(1, 2), (2, 3), (3, 4), (4, None)]
A workaround but not the best solution is to append None with the sequence.
list(windowed(seq=[1, 2, 3, 4,None], n=2, step=1))
I believe you can do this programmatically based on the step= value which I refer to as win_step in the following code. I also removed hardcoding where possible to make it easier to test various sequence_list, win_width, and win_step data sets:
sequence_list = [1, 2, 3, 4]
win_width = 2
win_step = 1
none_list = []
for i in range(win_step):
none_list.append(None)
sequence_list.extend(none_list)
tuple_list = list(windowed(seq=sequence_list, n=win_width, step=win_step))
print('tuple_list:', tuple_list)
Here are my results based on your original question's data set, and on the current data set:
For original, where:
sequence_list = [1, 2, 3, 4, 5, 6]
win_width = 3
win_step = 2
The result is:
tuple_list: [(1, 2, 3), (3, 4, 5), (5, 6, None), (None, None, None)]
And for the present data set, where:
sequence_list = [1, 2, 3, 4]
win_width = 2
win_step = 1
The result is:
tuple_list: [(1, 2), (2, 3), (3, 4), (4, None)]

How to make 3 pairs of sub-lists from a Python list

In Python is there a way to get all 3 pairs or n pairs of a list from a list?
For example list = [1,2,3,4]
result : [[1,2,3],[2,3,4],[1,2,4]]
I want to find all possible n pairs of lists from a Python list. I do not want to import any other functions like itertools.
You can use the module itertools. It comes inside Python by default (you don't need to install it throught third party modules):
>>> import itertools
>>> print(itertools.permutations([1,2,3,4], 3))
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
This itertools.permutations(iterable, r=None) produces all the possible permutations of a given iterable element (like a list). If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.
If you are looking only for n pair of permutations instead of all the possible permutations just delete the rest of them:
>>> print(list(itertools.permutations([1,2,3,4], 3))[:3])
[(1, 2, 3), (1, 2, 4), (1, 3, 2)]
As you asked in comments you can do that without importing any module. itertools.permutations is just a function, which you can make by yourself:
def permutations(iterable, r=None):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
But I strongly advise your importing it. If you don't want to import the whole module, just this function simply do from itertools import permutations.

Tuple list comprehension

I have a list of tuples containing numbers
list_numbers = [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
How do I use list comprehension to get a list of the sum of each item in the tuple?
expected_result = [7, 9, 11, 13, 15]
You can just loop through the list and call the sum() function on each tuple.
sums = [sum(t) for t in list_numbers]
> [7, 9, 11, 13, 15]

How to group a list of tuples by nearest values for the n-th element?

I have a list of tuples which looks like this:
d = [(1,1,1),
(2,1,1),
(1,2,1),
(1,12,1),
(13,50,4),
(1,13,32),
(4,48,100),
(0,121,5)
]
And group this list by nearest values for the first element of each tuple, like this:
d_ordered = [
[(1,1,1),(2,1,1),(1,2,1)],
[(1,12,1),(1,13,32)],
[(4,48,100),(13,50,4)],
[(0,121,5)]
]
I found this:https://stackoverflow.com/a/10017017/9071615
which does it for a simple list. I tried to use that as a base but have no idea how to extend the solution to a list of tuples.
Any idea how to have the most efficient sorting for list of tuples?
import numpy as np
from itertools import groupby
n = 1
a = np.array([i[n] for i in d])# or np.array(d)[:,n] if all the elements of d have the same shape
b,c=np.where(np.abs(a-a[:,None]) < 5)# I used a maximum distance of 5, you did not specify exactly the allowable distance
e=set(tuple(k[1] for k in j) for i,j in groupby(zip(b,c),key=lambda x:x[0]))
[[d[j] for j in i] for i in e]
[[(1, 1, 1), (2, 1, 1), (1, 2, 1)],
[(0, 121, 5)],
[(13, 50, 4), (4, 48, 100)],
[(1, 12, 1), (1, 13, 32)]]
Here is a solution using sorted() and lambda:
d = [(1,1,1),
(2,1,1),
(1,2,1),
(1,12,1),
(13,50,4),
(1,13,32),
(4,48,100),
(0,121,5)
]
d_ordered = sorted(d, key = lambda x: (x[1]))
print(d_ordered)
Output:
[(1, 1, 1), (2, 1, 1), (1, 2, 1), (1, 12, 1), (1, 13, 32), (4, 48, 100), (13, 50, 4), (0, 121, 5)]

Combine two lists by appending the element of one list to the element of the other list in Python

I have two same length lists like [[-3, -2, 1],[2,3,5],[1,2,3]...[7,8,9]] and [-1,1,1,...1]. I would like to combine them as: [(-3,-2,1,-1), (2,3,5,1), (1,2,3,1)...(7,8,9,1)] in Python.
Appreciate if any comment.
>>> a = [(-3, -2, 1),(2,3,5),(1,2,3), (7,8,9)]
>>> b = [-1,1,1, 1]
>>> [i+(j,) for i, j in zip(a, b)]
[(-3, -2, 1, -1), (2, 3, 5, 1), (1, 2, 3, 1), (7, 8, 9, 1)]

Resources