| |

VerySource

 Forgot password?
 Register
Search
View: 25|Reply: 3

List is a mutable container. Why can't it modify its value? ?

[Copy link]

2

Threads

4

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-3-23 19:00:01
| Show all posts |Read mode
list = ["jessinio", "jason"]

for x in list:
   x = "non"


print (list) is still:
['jessinio', 'jason']


Puzzled! !! !! !! !! !!
Reply

Use magic Report

0

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-7-3 14:30:02
| Show all posts
for x in list:
   x = "non"

Put the values ​​in the list into X in turn, and then you are only the value X of the values ​​in the list, so the contents of the list are not modified.

The correct code should be like this
for x in range(len(list)):
    x = 0
print list

----------by a python dish.
Reply

Use magic Report

2

Threads

4

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 2020-7-4 22:00:01
| Show all posts
First of all, thank you for replying to my question.

Secondly, your reply cannot meet my request.
Reply

Use magic Report

0

Threads

7

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 3 hour(s) ago
| Show all posts
x = y
x = 1
Did you change the value of y?
x = 1
x = 2
Did you change the value of 1?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list