Update list in python - python
I'm a fresher in python. I have some troubles with list.
After creating the list, i update it. it's right when I print each element of list in loop wrong , but wrong when I return list: all elements of list are same
def shift(array,n):
for i in range(n):
array.append(array[0])
array.pop(0)
return array
def shiftKey(array):
array_shift = []
for i in range(16):
array_shift.append([])
array_shift[0] = [shift(array[0],1),shift(array[1],1)]
print (array_shift[0])
for j in range(1,16):
if j == 1 or j == 8 or j == 15:
a1 = shift(array_shift[j-1][0],1)
a2 = shift(array_shift[j-1][1],1)
array_shift[j] = [a1,a2]
print (array_shift[j])
else:
a1 = shift(array_shift[j-1][0],2)
a2 = shift(array_shift[j-1][1],2)
array_shift[j] = [a1,a2]
print (array_shift[j])
return array_shift
shiftKey([[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,15,16]])
and this's the result expected
[[[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, 15, 16, 1]] [[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, 15, 16, 1, 2]] [[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, 15, 16, 1, 2, 3, 4]] [[7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6]] [[9, 10, 11, 12, 13, 14, 15,
16, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3,
4, 5, 6, 7, 8]] [[11, 12, 13, 14, 15, 16, 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]] [[13,
14, 15, 16, 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]] [[15, 16, 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, 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, 15]] [[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, 15, 16, 1]] [[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, 15, 16,
1, 2, 3]] [[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, 15, 16, 1, 2, 3, 4, 5]] [[8, 9, 10,
11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2, 3, 4, 5, 6, 7]] [[10, 11, 12, 13, 14, 15, 16, 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]] [[12, 13, 14, 15, 16, 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]] [[13, 14, 15,
16, 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]]]
but it's print like below when i print list
[[[13, 14, 15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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,
15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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,
15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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, 15, 16, 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]]]
You need to use deepcopy functionality so you pass a copy of list instead of reference of the list.
Also, you can use the following easy function for rotation:
def shift(input_list, rotate):
return input_list[rotate:] + input_list[:rotate]
I will suggest you read about defaultlist.
I corrected my code and gave an alternate solution, please try this one.
import copy
def shift(array,n):
for i in range(n):
array.append(array[0])
array.pop(0)
return array
def shiftKey(array):
array_shift = []
array_shift.append([shift(array[0],1),shift(array[1],1)])
print (array_shift[0])
for j in range(1,16):
if j == 1 or j == 8 or j == 15:
a1 = shift(copy.deepcopy(array_shift[j-1][0]),1)
a2 = shift(copy.deepcopy(array_shift[j-1][1]),1)
array_shift.append([a1,a2])
print (array_shift[j])
else:
a1 = shift(copy.deepcopy(array_shift[j-1][0]),2)
a2 = shift(copy.deepcopy(array_shift[j-1][1]),2)
array_shift.append([a1,a2])
print (array_shift[j])
return array_shift
shiftKey([[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,15,16]])
Related
How can I generate Permutations for multiple groupings in a list in Python
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]
Matplotlib color assignment error?
I'm trying to make a colorful scatter plot based on data in an array: plt.scatter(150, 93, c=y_pred) Here, y_pred is: array([ 5, 6, 8, 16, 21, 12, 12, 13, 6, 6, 17, 11, 6, 12, 12, 23, 6, 6, 15, 6, 6, 6, 6, 6, 6, 23, 22, 6, 12, 17, 6, 20, 0, 6, 6, 12, 12, 0, 6, 6, 6, 6, 6, 6, 5, 17, 6, 6, 11, 10, 13, 6, 22, 24, 23, 6, 6, 13, 6, 6, 6, 12, 9, 15, 13, 14, 6, 18, 1, 6, 9, 6, 6, 11, 6, 5, 16, 9, 23, 2, 14, 24, 9, 5, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 6, 19, 6, 23, 3, 20, 10, 4, 8, 9, 6, 6, 9, 22, 23, 6, 6, 11, 6, 6, 6, 22, 24, 14, 4, 7, 12, 6, 19, 6, 12, 3, 22, 6, 11, 6, 21, 23, 4, 6, 6, 6, 4, 10, 22, 15, 6, 6, 18, 6, 14, 4, 5], dtype=int32) This gives me an error: ValueError: Invalid RGBA argument: 17 I don't understand why. The same solution works for others. Could you help me understand the error?
You only add one scatter point at x=150 ; y=93 but you try to assign 150 colors for this one value. plt.scatter(150, 93) It works if you pass in x and y that have the same shape as c: plt.scatter(np.random.random(150), np.random.random(150), c=y_pred)
Adding each element in an array by each element in the same array in python
I need to add each element in an array by each element in the same array and then add the results to a new array. I have tried my_list = [] for filename in [my_file]: with open(filename) as f: my_list += [int(i) for line in f for i in line.split()] answer = [] for elem in my_list: answer += [elem + elem] print answer When my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the program prints [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] which is incorrect. How can I fix this?
You can try this: my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] answer = [i+a for i in my_list for a in my_list] Output: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
swap elements of list in recursive call python
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.
How to find all possible ascending and descending order from a list?
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]]