Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the list statement of python interview questions

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report