This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python List Index
result=[range(3)]*2
for i in range(len(result)):
result[i][2]=4*i
print result
I would expected [[0, 1, 0], [0, 1, 4]]
Why do I get [[0, 1, 4], [0, 1, 4]]
Thank you!
When you do [range(3)] * 2, it makes a list with two references to the same list inside, so modifying result[0] and result[1] each modify both.
Use [range(3) for i in range(2)] to make a list with two different results of range(3) in it.
List "result" is: [[0, 1, 2], [0, 1, 2]]
Your iteration is: "for i in range(len(result))"
len(result) is: 2
range(2) is: [0,1]
meaning:
first time:
result[0][2]=4*0
second time:
result[1][2]=4*1
which gives you the result [[0, 1, 4], [0, 1, 4]]
This is what is doing step by step.
If you add a "break" to the iteration you see the result is [[0, 1, 0], [0, 1, 0]]
The "result" list works by reference. When it is called, it is pointing to the same object.
Related
I want to find 2D array's index and make it array.
for example:
data_pre=[[1,1,1,0,0,0],[1,0,1,0,0,0],[1,0,0,0,1,0],[1,0,0,0,0,0]]
i wanna find index that have one and wanna make it like this
b=[[0,1,2],[0,2],[0,4],[0]]
Code:
result = []
for i in range(len(data_pre)):
arr=data_pre[i]
currentArrResult=[]
for j in range(len(arr)):
if arr[j]==1:
currentArrResult.append(j)
result.append(currentArrResult)
I tried like that but output is wrong.
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 2], [0, 2], [0, 4], [0, 4], [0]]
I don't know which part is wrong...
you should not collect output inside the inner loop. that may get a result like this :
[[0],[0, 1],[0, 1, 2],
[0],[0, 2],
[0],[0, 4],
[0]]
you can check that by printing currentArrResult after append finish.
but you got different outcome because of data reference.
you should collect result after inner loop finish its work.
like:
result = []
for i in range(len(data_pre)):
arr=data_pre[i]
currentArrResult=[]
for j in range(len(arr)):
if arr[j]==1:
currentArrResult.append(j)
#print(currentArrResult)
result.append(currentArrResult)
This is probably a very basic question but I dont know what I have to search for to find the answer for it:
I have this code:
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
list.append(list[0])
for i in list:
i.append(0)
print(list)
This List will later be used as coordinates for a curve. I need to duplicate the first coordinate at the end to get a closed curve.
If I then want to add a third value to each coordinate in the list the first and last item in list will be iterated over twice:
[[0, 1, 0, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0, 0]]
I am guessing they have the same memory address and thereby the append-function is applied to the same object at this address once for the first index and once for the last.
What is this phenomenon called ? what is the easiest way to get the list like this:
[[0, 1, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0]]
Thank you for your help
You can do a list comprehension:
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
list.append(list[0])
list = [x + [0] for x in list]
print(list)
# [[0, 1, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0]]
EDIT: The trick here is, using x + [0] within the list comprehension. This way new lists are created, thus you do not append 0 to the same list twice (Hattip to #dx_over_dt)
The problem you have with your approach is, that the first and last element of your list refers to the very same object. You can see this, when you print i and list for every iteration:
for i in list:
i.append(0)
print(i)
print(list)
So for the first and last i in your loop, you will append a 0 to the very same list.
You could stick to your approach appending a copy of the first element:
list.append(list[0].copy())
The simplest answer is to add the 0's before appending the closing point.
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
for i in list:
i.append(0)
list.append(list[0])
print(list)
It's the tiniest bit more efficient than a list comprehension because it's not making copies of the elements.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
Let's say that I make a 3D list
list = [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
and I run
list[0][0][0] = 1 #set the first element of the first list of the first list to 1
print(list)
I'd expect to get
[[[1, 0], [0, 0]], [[0, 0], [0, 0]]]
but instead, I get
[[[1, 0], [1, 0]], [[1, 0], [1, 0]]]
Can someone figure out how to make it assign a variable to ONLY ONE element of a 3D list, instead of every first element? Thanks!
If it matters, I'm using Python 3.7 32-bit.
I have reproduced your results by making an assumption about how you actually defined your list. I assume that you defined some variable such as ab below and used that to create your list. However, the new list is still a bunch of references to your ab variable, so changing one actually changes ab which will affect your whole list.
ab = [0,0]
mylist = [[ab,ab],[ab,ab]]
mylist[0][0][0] = 1
print(mylist," ",ab)
OUTPUT
[[[1, 0], [1, 0]], [[1, 0], [1, 0]]] [1, 0]
To resolve this, simple initialize your lists with 0 instead of some variable:
mylist = [[[0,0],[0,0]],[[0,0],[0,0]]]
or
mylist = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(2)]
I have to create a list of lists that is summarized below:
list_of_lists = [[0,1],[0,2],[0,3]....[0,N]]
Basically just need the first element of each sub-list to be 0, and the second element to be 1 more than the prior value of the second element.
The value for N is about 2000, so obviously I do not want to type out the whole thing. Is there a simple way to automate with Python?
Thank You
You can use simple list comprehension with range:
>>> N = 5
>>> [[0, i] for i in range(1, N + 1)]
[[0, 1], [0, 2], [0, 3], [0, 4], [0, 5]]
I have a list[5][5] to populate... it looks like a table with 5 columns and 5 rows.
Each cell can be either one or zero.
I want to find different 2^25 possibility that can exist. Each possiblity is a combination of either 0 or 1 in a 5*5 table/list
How can I do that? With nested loop or something?
I suggest you start small... with a 1x1 list first and check that you can display both of the available combinations:
[[0]]
[[1]]
Next up, try a 2x2 list. There are 16 different lists to display:
[[0, 0], [0, 0]]
[[0, 0], [0, 1]]
[[0, 0], [1, 0]]
[[0, 0], [1, 1]]
[[0, 1], [0, 0]]
[[0, 1], [0, 1]]
[[0, 1], [1, 0]]
[[0, 1], [1, 1]]
[[1, 0], [0, 0]]
[[1, 0], [0, 1]]
[[1, 0], [1, 0]]
[[1, 0], [1, 1]]
[[1, 1], [0, 0]]
[[1, 1], [0, 1]]
[[1, 1], [1, 0]]
[[1, 1], [1, 1]]
If you've got the algorithm right for 1x1 and 2x2, then you should be able to generalise it to print your 5x5.
Good luck!
Update
Since you appear to be still struggling, here's a little extra help.
Break this problem into smaller problems. I'd start with generating the values. If you ignore the list notation in my examples above, you'll see that the sequence of values is one that is recognisable to every computer scientist on the planet. It's also pretty easy to generate in Python using bin() and str.zfill().
The second problem is putting them into lists. This isn't too hard either. Supposing the first value in your sequence is '0000'. You know that your lists are two rows by two columns. You can put the first two characters into a list and put that list into a list. Then put the next two characters into a list and append that list to the previous one. Done. Repeat for each value in the sequence.
Hope this helps.
You could try:
import itertools
gen = itertools.product((0,1),repeat=25)
To create a generator to get all of the combinations in 1d and then reshape the data as needed.