Home > other >  This is a bug or feature of Python??
This is a bug or feature of Python??

Time:10-05

 
> A=[10] [0] * * 10
> A
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
> A [0] [1]='a'
> A
[[, 'a' 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0] to [0, 'a', 0, 0, 0, 0, 0, 0, 0, 0], [, 'a' 0, 0, 0, 0, 0, 0, 0, 0, 0]]
> Id (a [0])
38418120 l
> Id (a [1])
38418120 l
> Id (a [2])
38418120 l

CodePudding user response:

Didn't notice this problem before, this initialization is no problem.

 
A=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]



Python 3.7.3

CodePudding user response:

Is indeed a pit, have to do this,
> A=[[0 for _ in range (10)] for _ in range (10)]
> A
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
> A [0] [1]='a'
> A
[[, 'a' 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>

CodePudding user response:

Thank you for your answer, today the problem met in the experiment, then switch to the numpy has solved the problem, feel * 10 second like a shallow copy, no new memory allocation,

CodePudding user response:

Mainly is to want to put the problem that this discuss why so, want to see people don't do this with the later, bugs really cost time
  • Related