About list slicing in Python [duplicate] - python

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?

Related

Can anyone explain the function of last line in the following code [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 months ago.
str_1= input("Enter:")
for i in range(len(str_1)+1):
print(str_1[:i])
It means get all the characters starting from the i'th position for every iteration

what tricks are there to slice this sentence into my desired format? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 years ago.
How do I slice this
'Python is an interesting and useful language for numerical computing!'
to get
'Pto sa neetn n sfllnug o ueia optn!?
in Python?
List indexing magic (rather no magic)
data = 'Python is an interesting and useful language for numerical computing!'
print(data)
print(data[0::2])

Cant multiply sequence by Float for Python [duplicate]

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.

Python += with a list and a tuple [duplicate]

This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why can't I add a tuple to a list with the '+' operator in Python?
(3 answers)
Closed 7 years ago.
I saw someone wrote an interesting python line online, but couldn't understand why it works. So we can try the following lines in python interpreter:
s=[1]
s=s+(1,-1)
This will result in an error "TypeError: can only concatenate list (not "tuple") to list". But if done in another way:
s=[1]
s+=(1,-1)
will result in s = [1,1,-1]
So I used to thought x=x+y is equivalent to x+=y, can someone tell me how they are different and why the second way works? Thanks in advance.
Instead of += use list.extend:
s = [1]
s.extend((1,-1))

incrementing elements of 2d list in python [duplicate]

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.

Categories

Resources