Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the list statement of python interview questions

Shulou Source: shulou.com Published: 2022-06-03 19:17:25 11月11日 Update

This article mainly introduces the python interview list statement example analysis, the article is very detailed, has a certain reference value, interested friends must read it!

The details are as follows:

What is the output of the following program?

val = [['a']*2]*2print valval[0][1]='b'print val

The answer is:

[['a', 'a'], ['a', 'a']]

[['a', 'b'], ['a', 'b']]

rather than

[['a', 'a'], ['a', 'a']]

[['a', 'b'], ['a', 'a']]

Consider the following example:

val = ['a']*2 #or simply declare val = [' a','a'] print valid (val)print id(val[0])print id(val[1])

The output is

['a', 'a']

39993928

38858384

38858384

You can see that the two 'a's in val point to the same address:

Look at the following code:

A direct statement does not change the subsequent 'a'

val = [['a','a'],['a','a']]print valprint id(val)print id(val[0][0])print id(val[1][0])val[0][0] = 'b'print valprint id(val)print id(val[0][0])print id(val[1][0])

The results are:

[['a', 'a'], ['a', 'a']]

39405512

38268560

38268560

[['b', 'a'], ['a', 'a']]

39405512

38390280

38268560

and

val = [['a','a']]*2print valprint id(val)print id(val[0][0])print id(val[1][0])val[0][0] = 'b'print valprint id(val)print id(val[0][0])print id(val[1][0])

The results are:

[['a', 'a'], ['a', 'a']]

40388552

39251600

39251600

[['b', 'a'], ['b', 'a']]

40388552

39373320

39373320

The above is "python interview list statement sample analysis" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Tags: Results examples analysis content articles face output two code value examples interests addresses guys partners directions more knowledge programs answers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Huawei Linux Shulou Tech Info