This question already has answers here:
Python list problem [duplicate]
(2 answers)
Nested List Indices [duplicate]
(2 answers)
Closed 9 years ago.
I've noticed something strange when trying to implement elements of a 2d list in python. Here is my code
n_dk=2*[5*[0]]
n_dk[0][0]+=1
print n_dk
I would expect the output to be
[[1,0,0,0,0],[0,0,0,0,0]]
but the actual output is
[[1,0,0,0,0],[1,0,0,0,0]]
can anyone tell me what I am doing wrong. btw I used a numpy array instead and it worked the way I wanted it to.
Related
This question already has answers here:
Does python have a sorted list?
(9 answers)
What do I use for a max-heap implementation in Python?
(19 answers)
Closed 6 months ago.
I need a data structure like a dict where I can insert elements with a key and then efficiently take out largest element by value when I need it.
This question already has answers here:
Understanding slicing
(38 answers)
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I have learned Python for about 4 months, I thought I know every basics now. But currently I accidently tried a simple python expression, and the result is out of my expectation.
for example:
test = [1,2,3,4]
test[0:-1:-1]
and the result is an empty list([]).
However, my expected result is [3,2,1] for "test[::-1]" will output "[4,3,2,1]".
Can anyone tell me why?
This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]
This question already has answers here:
Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
(1 answer)
Why does appending to one list also append to all other lists in my list of lists? [duplicate]
(4 answers)
Python: fastest way to create a list of n lists
(5 answers)
Closed 4 years ago.
I don't know python I am working through 'Exercises in Programming Style' and translating to javascript. I can understand most of the python but this line flabbergasts me .
# Let's use the first 25 entries for the top 25 words
data = data + [[]]*(25 - len(data))
some context: the challenge is to use self imposed memory limitations represented here by the data array. So here she just cleared out data that is no longer used to make room for the 25 most frequent word. What is she doing here ?
This question already has answers here:
Python array multiply
(3 answers)
Closed 5 years ago.
I saw this questions a couple other places but all the fixes dont work for me code. If someone could help that would be great.
import random
cvalues=[]
for i in range(50):
cvalues.append(random.randrange(0,16))
float_cvalues=[float(i) for i in cvalues]
print(float_cvalues)
nvalues=[((.4*(float_cvalues)-.8))]
print(nvalues)
Multiplying a sequence in Python is interpreted as an attempt to create multiple sequences, see this post.
You can instead use another list comprehension:
nvalues=[.4*i-.8 for i in float_cvalues]
Or for instance switch to numpy arrays.