I've been having this problem (below).
What I want to achieve is to shift each individual element in the list to the right and create a list/dictionary of lists of the possible combinations.
This is what I would like to achieve:
{[1,2,3,4,5],
[2,1,3,4,5],
[2,3,1,4,5],
[2,3,4,1,5],
[2,3,4,5,1],
[1,3,2,4,5],
[1,3,4,2,5],
[1,3,4,5,2],
[1,2,4,3,5],
.
.
.
#until final possible combination
}
It doesn't have to be in the same order as the example above. I just want to be able to get a long list/dictionary of all possible order of combinations of [1,2,3,4,5]. Thanks in advance!
You can use permutations() from itertools:
from itertools import permutations
l = [1,2,3,4,5]
for i in permutations(l):
print(list(l))
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 5, 4]
[1, 2, 4, 3, 5]
[1, 2, 4, 5, 3]
[1, 2, 5, 3, 4]
[1, 2, 5, 4, 3]
[1, 3, 2, 4, 5]
[1, 3, 2, 5, 4]
[1, 3, 4, 2, 5]
[1, 3, 4, 5, 2]
[1, 3, 5, 2, 4]
[1, 3, 5, 4, 2]
[1, 4, 2, 3, 5]
[1, 4, 2, 5, 3]
[1, 4, 3, 2, 5]
[1, 4, 3, 5, 2]
[1, 4, 5, 2, 3]
[1, 4, 5, 3, 2]
[1, 5, 2, 3, 4]
[1, 5, 2, 4, 3]
[1, 5, 3, 2, 4]
[1, 5, 3, 4, 2]
[1, 5, 4, 2, 3]
[1, 5, 4, 3, 2]
[2, 1, 3, 4, 5]
[2, 1, 3, 5, 4]
[2, 1, 4, 3, 5]
[2, 1, 4, 5, 3]
[2, 1, 5, 3, 4]
[2, 1, 5, 4, 3]
[2, 3, 1, 4, 5]
[2, 3, 1, 5, 4]
[2, 3, 4, 1, 5]
[2, 3, 4, 5, 1]
[2, 3, 5, 1, 4]
[2, 3, 5, 4, 1]
[2, 4, 1, 3, 5]
[2, 4, 1, 5, 3]
[2, 4, 3, 1, 5]
[2, 4, 3, 5, 1]
[2, 4, 5, 1, 3]
[2, 4, 5, 3, 1]
[2, 5, 1, 3, 4]
[2, 5, 1, 4, 3]
[2, 5, 3,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 2, 4]
[5, 3, 1, 4, 2]
[5, 3, 2, 1, 4]
[5, 3, 2, 4, 1]
[5, 3, 4, 1, 2]
[5, 3, 4, 2, 1]
[5, 4, 1, 2, 3]
[5, 4, 1, 3, 2]
[5, 4, 2, 1, 3]
[5, 4, 2, 3, 1]
[5, 4, 3, 1, 2]
[5, 4, 3, 2, 1]
Related
I have a given 2d np-array and want to duplicate every e.g. 3rd row and column.
Basically, if I had an np-array
a = np.array([
[1, 2, 3, 1, 2, 3],
[2, 3, 4, 2, 3, 4],
[3, 4, 5, 3, 4, 5],
[4, 5, 6, 4, 5, 6]
])
I would want to produce:
b = np.array([
[1, 2, 3, 3, 1, 2, 3, 3],
[2, 3, 4, 4, 2, 3, 4, 4],
[3, 4, 5, 5, 3, 4, 5, 5],
[3, 4, 5, 5, 3, 4, 5, 5],
[4, 5, 6, 6, 4, 5, 6, 6]
])
How could I do that?
rows
You can identify the Nth row using arithmetic, then duplicate it with np.repeat:
N = 3
out = np.repeat(a, (np.arange(a.shape[0])%N == (N-1)) + 1, axis=0)
Output:
array([[1, 2, 3, 1, 2, 3],
[2, 3, 4, 2, 3, 4],
[3, 4, 5, 3, 4, 5],
[3, 4, 5, 3, 4, 5],
[4, 5, 6, 4, 5, 6]])
intermediate:
(np.arange(a.shape[0])%N == (N-1)) + 1
# array([1, 1, 2, 1])
rows and cols
same mechanisms on both dimensions:
N = 3
rows = (np.arange(a.shape[0])%N == (N-1)) + 1
# array([1, 1, 2, 1])
cols = (np.arange(a.shape[1])%N == (N-1)) + 1
# array([1, 1, 2, 1, 1, 2])
out = np.repeat(np.repeat(a, rows, axis=0), cols, axis=1)
output:
array([[1, 2, 3, 3, 1, 2, 3, 3],
[2, 3, 4, 4, 2, 3, 4, 4],
[3, 4, 5, 5, 3, 4, 5, 5],
[3, 4, 5, 5, 3, 4, 5, 5],
[4, 5, 6, 6, 4, 5, 6, 6]])
As an example, say I have the following six lists:
[2], [2,3], [4], [1,2], [2], [1,4]
I want to make a new list that has either 0 or 1 elements from each of these lists. One such list could be [2,2,4,1,2,1] which is made by selecting the first element from each list. Another such list could be [2,3,2,2,4] which is made by selecting the last element from each list. Another could be [2,4,1,2] which has no elements from the first and last lists. How can I form a list of lists (in python) that has all possible lists made from selecting either 0 or 1 elements from these six lists? Any help would be greatly appreciated!
You want the product of all the lists. To include the case where you select zero elements from any given list, you can add None to all the lists, and then remove None elements before you yield a selection.
This assumes you don't have any Nones in the original lists. If you do, you can replace the Nones in this logic with some dummy value that your lists won't contain.
import itertools
def optional_product(*iterables, dummy=None):
iterables = [itertools.chain(itbl, [dummy]) for itbl in iterables]
for selection in itertools.product(*iterables):
sel = [s for s in selection if s != dummy]
yield sel
Note: To support iterables other than lists, I use itertools.chain to "append" a None to each input iterable.
To run this:
for sel in optional_product([2], [2,3], [4], [1,2], [2], [1,4]):
print(sel)
which prints:
[2, 2, 4, 1, 2, 1]
[2, 2, 4, 1, 2, 4]
[2, 2, 4, 1, 2]
[2, 2, 4, 1, 1]
[2, 2, 4, 1, 4]
[2, 2, 4, 1]
[2, 2, 4, 2, 2, 1]
[2, 2, 4, 2, 2, 4]
[2, 2, 4, 2, 2]
[2, 2, 4, 2, 1]
[2, 2, 4, 2, 4]
[2, 2, 4, 2]
[2, 2, 4, 2, 1]
[2, 2, 4, 2, 4]
[2, 2, 4, 2]
[2, 2, 4, 1]
[2, 2, 4, 4]
[2, 2, 4]
[2, 2, 1, 2, 1]
[2, 2, 1, 2, 4]
[2, 2, 1, 2]
[2, 2, 1, 1]
[2, 2, 1, 4]
[2, 2, 1]
[2, 2, 2, 2, 1]
[2, 2, 2, 2, 4]
[2, 2, 2, 2]
[2, 2, 2, 1]
[2, 2, 2, 4]
[2, 2, 2]
[2, 2, 2, 1]
[2, 2, 2, 4]
[2, 2, 2]
[2, 2, 1]
[2, 2, 4]
[2, 2]
[2, 3, 4, 1, 2, 1]
[2, 3, 4, 1, 2, 4]
[2, 3, 4, 1, 2]
[2, 3, 4, 1, 1]
[2, 3, 4,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, 1]
[2, 4, 4]
[2, 4]
[2, 1, 2, 1]
[2, 1, 2, 4]
[2, 1, 2]
[2, 1, 1]
[2, 1, 4]
[2, 1]
[2, 2, 2, 1]
[2, 2, 2, 4]
[2, 2, 2]
[2, 2, 1]
[2, 2, 4]
[2, 2]
[2, 2, 1]
[2, 2, 4]
[2, 2]
[2, 1]
[2, 4]
[2]
[3, 4, 1, 2, 1]
[3, 4, 1, 2, 4]
[3, 4, 1, 2]
[3, 4, 1, 1]
[3, 4, 1, 4]
[3, 4, 1]
[3, 4, 2, 2, 1]
[3, 4, 2, 2, 4]
[3, 4, 2, 2]
[3, 4, 2, 1]
[3, 4, 2, 4]
[3, 4, 2]
[3, 4, 2, 1]
[3, 4, 2, 4]
[3, 4, 2]
[3, 4, 1]
[3, 4, 4]
[3, 4]
[3, 1, 2, 1]
[3, 1, 2, 4]
[3, 1, 2]
[3, 1, 1]
[3, 1, 4]
[3, 1]
[3, 2, 2, 1]
[3, 2, 2, 4]
[3, 2, 2]
[3, 2, 1]
[3, 2, 4]
[3, 2]
[3, 2, 1]
[3, 2, 4]
[3, 2]
[3, 1]
[3, 4]
[3]
[4, 1, 2, 1]
[4, 1, 2, 4]
[4, 1, 2]
[4, 1, 1]
[4, 1, 4]
[4, 1]
[4, 2, 2, 1]
[4, 2, 2, 4]
[4, 2, 2]
[4, 2, 1]
[4, 2, 4]
[4, 2]
[4, 2, 1]
[4, 2, 4]
[4, 2]
[4, 1]
[4, 4]
[4]
[1, 2, 1]
[1, 2, 4]
[1, 2]
[1, 1]
[1, 4]
[1]
[2, 2, 1]
[2, 2, 4]
[2, 2]
[2, 1]
[2, 4]
[2]
[2, 1]
[2, 4]
[2]
[1]
[4]
[]
The last value yielded is the case where zero elements are selected from all lists. If you don't want this, you can simply yield sel only when sel is non-empty.
To obtain a list of lists containing all selections, just do list(optional_product(...))
There are 3 subsets in Spi, each subset contains its sublists and I denoted it as Spi[d][l] (d is the number of subsets and l is the number of sublists).
I want to enter each subset to insert just one specific number which is an element of S_inter into the 3rd, 4th, and 5th (3, 4, 5 are elements of in_inter_job) position of each sublist respectively.
To be more clear, that is inserting number 4 into the first subset, next is number 2 entering to the second subset (2 will be inserted into position 3 of the first sublist, position 4 in the second sublist and 5th position in the last sublist), the do the same for number 6 and the last subset.
S_inter=[4, 2, 6]
in_inter_job=[ 3, 4, 5]
Spi=[[[1, 3, 5, 2, 6], [1, 3, 5, 2, 6], [1, 3, 5, 2, 6]], [[1, 3, 5, 4, 6], [1, 3, 5, 4, 6], [1, 3, 5, 4, 6]], [[1, 3, 5, 4, 2], [1, 3, 5, 4, 2], [1, 3, 5, 4, 2]]]
The result I wanted is like :
Spi=[[[1, 3, 5, 4, 2, 6], [1, 3, 5, 2, 4, 6], [1, 3, 5, 2, 6, 4]], [[1, 3, 5, 2, 4, 6], [1, 3, 5, 4, 2, 6], [1, 3, 5, 4, 6, 2]], [[1, 3, 5, 6, 4, 2], [1, 3, 5, 4, 6, 2], [1, 3, 5, 4, 2, 6]]]
But after I run these for loop:
for d in range(0,len(Spi)):
for k in S_inter:
for l, r in zip(range(len(Spi[d])),in_inter_job):
Spi[d][l].insert(r,k)
if len(Spi[d][l]) == 6:
break
print(" After insert ",Spi)
then I get result:
Spi=[[[1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 2, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 2, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 2, 6]], [[1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 6]], [[1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 2], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 2], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 2]]]
Please help me!
Simple nested for loop to insert into each list in Spi at corresponding position in in_inter_job with corresponding value in S_inter:
S_inter=[4, 2, 6]
in_inter_job=[ 3, 4, 5]
Spi=[[[1, 3, 5, 2, 6], [1, 3, 5, 2, 6], [1, 3, 5, 2, 6]], [[1, 3, 5, 4, 6], [1, 3, 5, 4, 6], [1, 3, 5, 4, 6]], [[1, 3, 5, 4, 2], [1, 3, 5, 4, 2], [1, 3, 5, 4, 2]]]
for i, x in enumerate(S_inter):
for j, L in enumerate(Spi[i]):
pos = in_inter_job[j]
L.insert(pos, x)
print(Spi)
Your zip (which is similar to what enumerate above is doing) doesn't work, because it doesn't match your logic. The positions of in_inter_job do not directly correspond with the indexes in S_inter, and so you end up inserting the wrong values, because you're trying to use the same index for both lists.
Given this matrix:
x = [[1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5]]
What is an efficient way to return all the possible 4 by 1 vectors and 1 by 4 matrix in this matrix. As well as any 4 diagonal spaces joined in a line.
For example:
[1,1,1,1] would appear 3 times
Diagonals also need to be addressed so [1,2,3,4] would be included as a row but also a diagonal.
Splitting the problem into two steps:
Step 1 - get all horizontal, vertical and diagonal lines
Diagonals are tackled using the fact either i+j, or respectively i-j, is constant for the indexes i, j
x = [[1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5]]
pprint.pprint(x)
# [[1, 2, 3, 4, 5],
# [1, 2, 3, 4, 5],
# [1, 2, 3, 4, 5],
# [1, 2, 3, 4, 5],
# [1, 2, 3, 4, 5],
# [1, 2, 3, 4, 5]]
all_lines = (
# Horizontal
[x[i] for i in range(len(x))] +
# Vertical
[[x[i][j] for i in range(len(x))] for j in range(len(x[0]))] +
# Diagonal k = i - j
[[x[k+j][j] for j in range(len(x[0])) if 0 <= k+j < len(x)] for k in range(-len(x[0])+1, len(x))] +
# Diagonal k = i + j
[[x[k-j][j] for j in range(len(x[0])) if 0 <= k-j < len(x)] for k in range(len(x[0])+len(x)-1)]
)
>>> pprint.pprint(all_lines)
[[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5],
[5],
[4, 5],
[3, 4, 5],
[2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4],
[1, 2, 3],
[1, 2],
[1],
[1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[2, 3, 4, 5],
[3, 4, 5],
[4, 5],
[5]]
Step 2 - for each line select each 4-length slice
ans = [a[i:i+4] for i in range(len(a)-4+1) for a in all_lines if len(a[i:i+4]) == 4]
>>> ans = [a[i:i+4] for i in range(len(a)-4+1) for a in all_lines if len(a[i:i+4]) == 4]
>>> pprint.pprint(ans)
[[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5],
[2, 3, 4, 5],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[2, 3, 4, 5],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5]]
Maybe not be the most efficient but it's at least a way of doing it.
There will likely be a way of using itertools combinations to simplify this dramatically.
for i in file:
file = [i.strip() for i in file]
return [file[i:i+1] for i in range(0, len(file),1)]
OUTPUT:
[['1,3,4,5,2'], ['4,2,5,3,1'], ['1,3,2,5,4'], ['1,2,4,3,5'], ['1,3,4,5,2'], ['2,1,3,5,4'], ['1,3,4,5,2'], ['3,5,2,4,1'], ['1,4,5,2,3'], ['5,1,4,3,2'], ['3,2,5,4,1'], ['3,1,2,5,4'], ['2,5,1,4,3'], ['3,2,1,4,5'], ['4,5,3,1,2'], ['1,5,4,3,2'], ['1,5,3,4,2'], ['2,1,4,3,5'], ['4,1,2,5,3']]
This is my List with sub-lists.
How can I take off the ' '? So ['1,3,4,5,2'] -> [1,3,4,5,2]
Thanks! (:
You can try this:
s = [['1,3,4,5,2'], ['4,2,5,3,1'], ['1,3,2,5,4'], ['1,2,4,3,5'], ['1,3,4,5,2'], ['2,1,3,5,4'], ['1,3,4,5,2'], ['3,5,2,4,1'], ['1,4,5,2,3'], ['5,1,4,3,2'], ['3,2,5,4,1'], ['3,1,2,5,4'], ['2,5,1,4,3'], ['3,2,1,4,5'], ['4,5,3,1,2'], ['1,5,4,3,2'], ['1,5,3,4,2'], ['2,1,4,3,5'], ['4,1,2,5,3']]
final_data = [map(int, i[0].split(",")) for i in s]
Output:
[[1, 3, 4, 5, 2], [4, 2, 5, 3, 1], [1, 3, 2, 5, 4], [1, 2, 4, 3, 5], [1, 3, 4, 5, 2], [2, 1, 3, 5, 4], [1, 3, 4, 5, 2], [3, 5, 2, 4, 1], [1, 4, 5, 2, 3], [5, 1, 4, 3, 2], [3, 2, 5, 4, 1], [3, 1, 2, 5, 4], [2, 5, 1, 4, 3], [3, 2, 1, 4, 5], [4, 5, 3, 1, 2], [1, 5, 4, 3, 2], [1, 5, 3, 4, 2], [2, 1, 4, 3, 5], [4, 1, 2, 5, 3]]
try this:
import ast
l = []
s = [['1,3,4,5,2'], ['4,2,5,3,1'], ['1,3,2,5,4'], ['1,2,4,3,5'], ['1,3,4,5,2'], ['2,1,3,5,4'], ['1,3,4,5,2'], ['3,5,2,4,1'], ['1,4,5,2,3'], ['5,1,4,3,2'], ['3,2,5,4,1'], ['3,1,2,5,4'], ['2,5,1,4,3'], ['3,2,1,4,5'], ['4,5,3,1,2'], ['1,5,4,3,2'], ['1,5,3,4,2'], ['2,1,4,3,5'], ['4,1,2,5,3']]
for li in s:
l.append(list(ast.literal_eval(li[0])))
note: ast is a built-in (standard) module if you don't know it, it cames with python installation
One line solution :
list_1=[['1,3,4,5,2'], ['4,2,5,3,1'], ['1,3,2,5,4'], ['1,2,4,3,5'], ['1,3,4,5,2'], ['2,1,3,5,4'], ['1,3,4,5,2'], ['3,5,2,4,1'], ['1,4,5,2,3'], ['5,1,4,3,2'], ['3,2,5,4,1'], ['3,1,2,5,4'], ['2,5,1,4,3'], ['3,2,1,4,5'], ['4,5,3,1,2'], ['1,5,4,3,2'], ['1,5,3,4,2'], ['2,1,4,3,5'], ['4,1,2,5,3']]
print([[int(item_3) for item_3 in item_1 if item_3.isdigit()] for item in list_1 for item_1 in item])
output:
[[1, 3, 4, 5, 2], [4, 2, 5, 3, 1], [1, 3, 2, 5, 4], [1, 2, 4, 3, 5], [1, 3, 4, 5, 2], [2, 1, 3, 5, 4], [1, 3, 4, 5, 2], [3, 5, 2, 4, 1], [1, 4, 5, 2, 3], [5, 1, 4, 3, 2], [3, 2, 5, 4, 1], [3, 1, 2, 5, 4], [2, 5, 1, 4, 3], [3, 2, 1, 4, 5], [4, 5, 3, 1, 2], [1, 5, 4, 3, 2], [1, 5, 3, 4, 2], [2, 1, 4, 3, 5], [4, 1, 2, 5, 3]]
in detailed :
for item in list_1:
for item_1 in item:
new=[]
for item_3 in item_1:
if item_3.isdigit():
new.append(int(item_3))
print(new)