| |

VerySource

 Forgot password?
 Register
Search
View: 2483|Reply: 6

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

[Copy link]

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 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

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 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

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 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

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-7-14 17:30:01
| 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

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-7-15 10:00:01
| Show all posts
After Python has immutable variables, it is really difficult to understand.

The master upstairs said:

x = 1
x = 2
Did you change the value of 1?

Do you want the members of the list to be immutable variables?

If this is the case, how to use a circular language to modify all members of a container? ?
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-7-16 10:45:01
| Show all posts
for x in list:
>>>x = "non"

Each time through the loop, x just points to the same thing as a position in the list, and later x points to "non". The original position in the list still points to the original object, and the list has always been used as an rvalue.

Can be understood in C:

const char* list[] = {"apple9927", "jason"};
const char* x;
int i;
for(int i=0; i<2; i++)
{
   x = list[i];
   x = "non";
}

There is no operation to modify the list (list is not used as an lvalue);


You can use the index if you want to modify the list:
w = ["int", "str"]

for i in range(len(w)):
   w[i] = "none"

print w
Reply

Use magic Report

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-7-16 16:15:01
| Show all posts
Thank you, I want to feel this direction~! ~! ~! ~
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list