Related
I'm working with lists and I came up with a question. So basically, I have a 3D list in which I'm trying that after a for loop I maintain the second and penultimate element of the inner list, my code does the work, but it changes from a 3D list to a 2D list and I don't want that (this is just an example, mylist can have more elements) here is my code
mylist = [[[0, 4, 3, 5, 1], [0, 2, 1], [0, 2, 3, 1], [0, 3, 5, 1], [0, 2, 5, 3, 1]],
[[0, 4, 1], [0, 2, 1], [0, 4, 3, 1], [0, 4, 5, 1], [0, 4, 3, 1]]]
aux = []
for i in mylist:
for j in i :
if len(j) >= 4:
aux.append(j[1:-1])
print(aux)
# aux= [[4, 3, 5], [2, 3], [3, 5], [2, 5, 3], [4, 3], [4, 5], [4, 3]]
I got almost what I need, but I was expecting this output
aux = [[[4, 3, 5], [2, 3], [3, 5], [2, 5, 3]], [[4, 3], [4, 5], [4, 3]]] #Mantains the 3D dimension
I'm working on my list comprehension, but I'm not quite sure how to do this, this is what I try
aux = [[j[1:-1] if len(j) >= 4 for j in i] for i in mylist]
#But I get a syntax error
I don't know if there is a easier way to do this (must be, but I'm the guy who complicate things) maybe a a much easier list comprehension that I'm not seeing, so any help will be appreciated, thank you!
You got it right, only you have to move the if statement to after for in list comprehension:
aux = [[j[1:-1] for j in i if len(j)>=4] for i in mylist]
output:
[[[4, 3, 5], [2, 3], [3, 5], [2, 5, 3]], [[4, 3], [4, 5], [4, 3]]]
You were really close!
This should work. Look it over and see the logic involved
aux = [[j[1:-1] for j in i if len(j) >= 4] for i in mylist]
I'd like to remove every 4th to 6th elements from a list.
The list is of permutations of 4 numbers, it is as follows:
A = map(list, permutations([1, 2, 3, 4]))
These are the first 12 elements from the output of A[0:12], and the other list is the desired output, with the 4th-to-6th elements removed from this bit of the overall list.
[[1, 2, 3, 4],
[1, 2, 4, 3],
[1, 3, 2, 4],
[1, 3, 4, 2],
[1, 4, 2, 3],
[1, 4, 3, 2],
[2, 1, 3, 4],
[2, 1, 4, 3],
[2, 3, 1, 4],
[2, 3, 4, 1],
[2, 4, 1, 3],
[2, 4, 3, 1]],
After removal, the list should be:
[[1, 2, 3, 4],
[1, 2, 4, 3],
[1, 3, 2, 4],
[2, 1, 3, 4],
[2, 1, 4, 3],
[2, 3, 1, 4]],
But my code outputs an error.
B=A[:4]
B.extend(A[7:])
should do the trick.
TITLE CURRENT READS "removing every 3rd-6th element in a list - pythoN" yet question current reads "I'd like to remove every 4th-6th elements out of a list.", but iether way, this is done the same in every programming language, you have to lop through it, starting from the end, and remove it if the index % 3 == 0, for example, for pseudo-code
variable someList = []
for(variable index = someList.length - 1; index >= 0; index--)
if(index % 3 == 0) delete someList[index]
adapt it accordingly to any programming language, essentially
I wish to split a list into same-sized chunks using a "sliding window", but instead of truncating the list at the ends, I want to wrap around so that the final chunk can be spread across the beginning and end of the list.
For example, given a list:
l = [1, 2, 3, 4, 5, 6]
I wish to generate chunks of size n=3 as follows:
[1, 2, 3], [4, 5, 6]
[2, 3, 4], [5, 6, 1]
[3, 4, 5], [6, 1, 2]
Or the same list with chunks of size n=2 should be split as follows:
[1, 2], [3, 4], [5, 6]
[2, 3], [4, 5], [6, 1]
The list may not be divided evenly into n sublists (e.g. if the original list has length 7 and n=3 - or any value other than 7 or 1). The rounded value len(l) / n can be used to determine the split size, as in the usual case.
This post is related, although it does not wrap around as I need. I have tried but not managed anything useful. Any suggestions would be most welcome!
You can use itertools.islice over a wrap-around iterator generated from itertools.cycle:
from itertools import cycle, islice
def rolling_chunks(l, n):
return ([list(islice(cycle(l), i + j, i + j + n)) for j in range(0, len(l), n)] for i in range(n))
so that list(rolling_chunks(l, 3)) returns:
[[[1, 2, 3], [4, 5, 6]], [[2, 3, 4], [5, 6, 1]], [[3, 4, 5], [6, 1, 2]]]
and that list(rolling_chunks(l, 2)) returns:
[[[1, 2], [3, 4], [5, 6]], [[2, 3], [4, 5], [6, 1]]]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 4 years ago.
Here is a snippet of python code:
a = [[0]*3]*4
for i in range(4):
for j in range(3):
a[i][j] = i+j
print(a[i][j])
print(a)
However, the outputs of the two prints are different.
The former one prints what I want. And the second prints all the same for the 4 sublists.
It seems the problem of shallow copying. I really don't understand how and why it happens.
Update:
After I have solved this, I found another problem:
a = [[0]*3]*4
for i in range(4):
a[i] = [i, 2*i, 3*i]
The result is also what I want. I'm once again confused about this.
Who can tell me the difference?
a = [[0]*3]*4
for i in range(4):
for j in range(3):
a[i][j] = i+j
print(a)
print(a[i][j])//show the execution at every step
print(a)
At each step the list with same column is updated with the same value.
output is
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
0
[[0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0]]
1
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
2
[[1, 1, 2], [1, 1, 2], [1, 1, 2], [1, 1, 2]]
1
[[1, 2, 2], [1, 2, 2], [1, 2, 2], [1, 2, 2]]
2
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
3
[[2, 2, 3], [2, 2, 3], [2, 2, 3], [2, 2, 3]]
2
[[2, 3, 3], [2, 3, 3], [2, 3, 3], [2, 3, 3]]
3
[[2, 3, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4]]
4
[[3, 3, 4], [3, 3, 4], [3, 3, 4], [3, 3, 4]]
3
[[3, 4, 4], [3, 4, 4], [3, 4, 4], [3, 4, 4]]
4
[[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]
5
[[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]
The multiplier of operator taking a list and an int will make makes shallow copies of the elements of the list, so you're on the right track.
Initialise like this instead
a = [[0] * 3 for _ in range(4)]
My task is to calculate the k-permutations from the updated List by new element
without recalculating the k-permutations already gotten from the previous state of the list. Example:
liste = [1, 2, 3]
3-permutations are:
[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
The updated list:
liste = [1, 2, 3, 4]
I would like to obtain directly 3-permutations[1, 2, 3, 4]-3-permutations[1, 2, 3]
without recalculating 3-permutations[1, 2, 3]
Calculate directly the new permutations:
[1, 2, 4], [1, 3, 4], [1, 4, 2], [1, 4, 3], [2, 1, 4], [2, 3, 4], [2, 4, 1],
[2, 4, 3], [3, 1, 4], [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]
Thanks
Computing first the cartesian product {0,1,2,3}x{0,1,2}x{0,1} and taking the nth element of list (1,2,3,4).
r=[]
for prd in itertools.product([[0,1,2,3],[0,1,2],[0,1]]):
l=[1,2,3,4]
r0=[]
for i in prd:
r0 += l[i]
del l[i]
r += r0
EDIT: original answer gives the 3-permutations of [1,2,3,4]
following command answers specifically to question, see how it can be generalized
[list(j) for i in itertools.combinations([1,2,3],2) for j in itertools.permutations(list(i)+[4])]
next case, maybe one of ?
[list(j) for i in itertools.combinations([1,2,3],2) for j in itertools.permutations(list(i)+[4,5])]
[list(j) for i in itertools.combinations([1,2,3,4],3) for j in itertools.permutations(list(i)+[4,5])]
try saving the existing permutations to a list, then do:
if newPermutation not in listOfExistingPermutations:
listOfExistingPermutations.append(newPermutation)
or something along those lines