Related
how are you?
I have a distance matrix and need to perform a filter based on another list before applying some functions.
The matrix has 10 elements that represent machines and the distances between them, I need to filter this list by getting only the distances between some chosen machines.
matrix = [[0, 1, 3, 17, 24, 12, 18, 16, 17, 15],
[1, 0, 2, 2, 5, 6, 13, 11, 12, 10],
[3, 2, 0, 1, 6, 12, 18, 12, 17, 15],
[17, 2, 1, 0, 3, 12, 17, 15, 16, 14],
[24, 5, 6, 3, 0, 1, 24, 22, 23, 21],
[12, 6, 12, 12, 1, 0, 12, 10, 11, 9],
[18, 13, 18, 17, 24, 12, 0, 3, 4, 5],
[16, 11, 12, 15, 22, 10, 3, 0, 1, 2],
[17, 12, 17, 16, 23, 11, 4, 1, 0, 1],
[15, 10, 15, 14, 21, 9, 5, 2, 1, 0]]
The list used for filtering, for example, is:
filter_list = [1, 2, 7, 10]
The idea is to use this list to filter the rows and the indices of the sublists to get the final matrix:
final_matrix = [[0, 1, 18, 15],
[1, 0, 13, 10],
[18, 13, 0, 5],
[15, 10, 5, 0]]
It is worth noting that the filter list elements vary. Can someone please help me?
That's what I tried:
final_matrix = []
for i in range(0, len(filter_list)):
for j in range(0,len(filter_list[i])):
a = filter_list[i][j]
final_matrix .append(matrix[a-1])
print(final_matrix)
This is because the filter_list can have sublists. I get it:
final_matrix = [[0, 1, 3, 17, 24, 12, 18, 16, 17, 15],
[1, 0, 2, 2, 5, 6, 13, 11, 12, 10],
[18, 13, 18, 17, 24, 12, 0, 3, 4, 5],
[15, 10, 15, 14, 21, 9, 5, 2, 1, 0]]
I could not remove the spare elements.
You forgot to filter by column ids. You can do this using nested list comprehensions.
final_matrix = [[matrix[row-1][col-1] for col in filter_list] for row in filter_list]
final_matrix = []
for i in filter_list:
to_append = []
for j in filter_list:
to_append.append(matrix[i-1][j-1])
final_matrix.append(to_append)
or with list comprehension
final_matrix = [[matrix[i-1][j-1] for j in filter_list] for i in filter_list]
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
I am new to Python, is there any best way to resolve below issue list slicing
I have main list called
result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
I would like to slice above list divide 12 and expecting to have the following output:
new_result = {
'One' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'Two' : [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
'Three' : [25, 26]
}
You can do something like this:
result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
chunk_size = 12
splitted_result = [result[x:x+chunk_size]
for x in range(0, len(result), chunk_size)]
for chunk in splitted_result:
print(chunk)
Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[25, 26]
Into the last for loop you can create the object with the correct keys.
The script logic is simple:
The range function takes three arguments:
start (0)
stop (length of the list)
step (increment between each returned value)
In your case you will have:
range(0, len(result), chunk_size)=[0,12,24]
and you just have to split the initial list in chunks that goes from:
result[0:0+12]
result[12:12+12]
result[24:24+12]
You can do the following:
result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
new_list = [result[n-1:n+11] for n in result[::12]]
new_result = {}
for n in range(1,len(new_list)+1):
new_result[n] = new_list[n-1]
new_result
{1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2: [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
3: [25, 26]}
The new_list is just the list with the sliced lists. Then, I convert it into a dictionary called new_result, starting from '1' onwards
I know that we can use itertools.permutations to permutate different items in a list however, what if I have a list such that few items need to be in fixed positions, few items need to be swapped with one more and few items need to be swapped with 2 more?
For example:
test = [1, 6, 2, 12, 5, 13, 11, 14, 15]
How can I use Python itertools.permutation or another method to generate all possible combinations with the following constraints?
Update:
1 and 5 have fixed positions
In position 2, I could have either 6 or 11
In position 3, I could have either 2 or 12
In position 4, I could have 2 or 12
In position 6, I could have either 13, 14, 15 and so on
So, my list looks like this:
[1, (6, 11), (2, 12), (2,12), 5, (13, 14, 15), (6, 11), (13, 14, 15), (13, 14, 15)]
I have included the numbers in groups which represents that numbers in the same group can be swapped with each other.
Thanks.
You could do something like this:
from itertools import permutations, product, chain
test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
groups = [[1], [2, 3, 4], [5], [6, 7], [8, 9], [10], [11, 12, 13], [14], [15, 16]]
result = [list(chain.from_iterable(permutation)) for permutation in product(*map(permutations, groups))]
for e in result[:20]:
print(e)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 12, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 12, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 11, 13, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 11, 13, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 11, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 11, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 11, 12, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 11, 12, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 12, 11, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 12, 11, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 11, 12, 13, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 11, 12, 13, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 11, 13, 12, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 11, 13, 12, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 12, 11, 13, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 12, 11, 13, 14, 16, 15]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 12, 13, 11, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 7, 9, 8, 10, 12, 13, 11, 14, 16, 15]
UPDATE
Given the new constraints you can do something like this:
from functools import partial
from itertools import combinations, permutations, product, chain
choose_one = partial(lambda r, iterable: combinations(iterable, r), 1)
groups = [[[1]], combinations([6, 11], 1), permutations([2, 12]), [[5]], combinations([13, 14, 15], 1)]
for e in product(*groups, repeat=1):
print(list(chain.from_iterable(e)))
Output
[1, 6, 2, 12, 5, 13]
[1, 6, 2, 12, 5, 14]
[1, 6, 2, 12, 5, 15]
[1, 6, 12, 2, 5, 13]
[1, 6, 12, 2, 5, 14]
[1, 6, 12, 2, 5, 15]
[1, 11, 2, 12, 5, 13]
[1, 11, 2, 12, 5, 14]
[1, 11, 2, 12, 5, 15]
[1, 11, 12, 2, 5, 13]
[1, 11, 12, 2, 5, 14]
[1, 11, 12, 2, 5, 15]
I want to make simple function swap random element in list.
but it doesn't work in recursive call.
in first recursive call, element swapping work,
but nested recursive call(or nested recursive call in first recursive call) doesn't work.
I don't know why only swap in first recursive call works.
below are result.
Thank you all.
def change(lst):
if len(lst)>4:
a, b = np.random.randint(0, len(lst)), np.random.randint(0, len(lst))
print(lst)
lst[a], lst[b] = lst[b], lst[a]
print(lst)
mid = int(len(lst)/2)
change(lst[:mid])
change(lst[mid:])
k = list(range(0, 20))
change(k)
print(k)
`
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4]
[3, 0, 2, 19, 4]
[5, 6, 7, 8, 9]
[5, 6, 8, 7, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14]
[10, 14, 12, 13, 11]
[15, 16, 17, 18, 1]
[15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1] <= result.
The problem is that in your recursive calls:
change(lst[:mid])
change(lst[mid:])
you use a slicing operator. The slicing operator constructs a new list, so your changes are made on a new list and are not reflected on the original list (since it is a copy).
What you can do is use indices instead:
def change(lst,frm=0,to=None):
if to is None: # set the default to the end of the list
to = len(lst)
if to-frm > 4:
a, b = np.random.randint(frm,to), np.random.randint(frm,to)
print(lst)
lst[a], lst[b] = lst[b], lst[a]
print(lst)
mid = (frm+to)//2
change(lst,frm,mid)
change(lst,mid,to)
Then we obtain:
>>> k = list(range(0, 20))
>>> change(k)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
>>> print(k)
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
That's because you create copies of the original list by lst[:mid], lst[mid:]. A solution is to pass to change() the same list and (separately) the range to process.
I have a list of number:
number = [1,2,3,4,5,6,...,20]
I would like to generate all possible ascending and descending order of 6 total from my list.
possible_ordered:
[1,2,3,4,5,6]
[2,3,4,5,6,7]
[3,4,5,6,7,8]
...
[15,16,17,18,19,20]
[20,19,18,17,16,15]
[15,14,13,12,11,10]
[10,9,8,7,6,5]
[6,5,4,3,2,1]
This is what I tried so far.
a = [1,2,3,4,5,6,7,8,9,10]
for x in a:
b = a[a.index(x):a.index(x)+6]
if len(b) == 6:
print b
output:
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 10]
I don't know how to get the descending order and I'm not really sure this is a good code.
Try this:
a = list(range(1, 21))
b = list(range(20, 0, -1)) # the revers of a
print([a[start: start + 6] for start in range(len(a) - 5)] +
[b[start: start + 6] for start in range(len(a) - 5)])
Output:
[[1, 2, 3, 4, 5, 6],
[2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 8],
[4, 5, 6, 7, 8, 9],
[5, 6, 7, 8, 9, 10],
[6, 7, 8, 9, 10, 11],
[7, 8, 9, 10, 11, 12],
[8, 9, 10, 11, 12, 13],
[9, 10, 11, 12, 13, 14],
[10, 11, 12, 13, 14, 15],
[11, 12, 13, 14, 15, 16],
[12, 13, 14, 15, 16, 17],
[13, 14, 15, 16, 17, 18],
[14, 15, 16, 17, 18, 19],
[15, 16, 17, 18, 19, 20],
[20, 19, 18, 17, 16, 15],
[19, 18, 17, 16, 15, 14],
[18, 17, 16, 15, 14, 13],
[17, 16, 15, 14, 13, 12],
[16, 15, 14, 13, 12, 11],
[15, 14, 13, 12, 11, 10],
[14, 13, 12, 11, 10, 9],
[13, 12, 11, 10, 9, 8],
[12, 11, 10, 9, 8, 7],
[11, 10, 9, 8, 7, 6],
[10, 9, 8, 7, 6, 5],
[9, 8, 7, 6, 5, 4],
[8, 7, 6, 5, 4, 3],
[7, 6, 5, 4, 3, 2],
[6, 5, 4, 3, 2, 1]]