In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "introduction to the process of passing values between two lists in python development". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the introduction of the process of passing values between two lists in python development"!
First, for a single-tier list
The assignment is as follows, as you can see, if you use = directly. One of the transformations will cause another transformation. At this time, because the two list are interpreted by python using the same address, such as the contents of id () in the output below, if you want one list to receive the value of another list and will not change because of the change of either of the values, you need to use the copy method, which can be interpreted as When the copy method is used, python separately creates an address in a memory area to put the new value.
A = [1JJ 2jue 3] b = aa [0] = a [0] + 1print (a) print (b) print (id (a [0])) print (id (b [0]) print ("*" * 20) A1 = [5Jing 6 Meng 7] b1 = a1.copy () A1 [0] = A1 [0] + 1print (A1) print (b1) print (id (A1 [0]) print (id (b1 [0)
Output
[2, 2, 3] [2, 2, 3] 14072969825921407296982592* [6,6,7] [5,6,7] 140729246982720140729246982688 for multi-tier list
This is where I was trapped today. I noticed the first layer, but not the second layer. As you can see, although we used the copy method, the inner layer [] did not.
A = [1, 2, 4], [3, 5, 5, 8] [] b = a.copy () print ("print array address:") print (id (a)) print (id (b)) print ("*" * 40) print ("print inner element address:") print (id (a [0])) print (id (b [0]) print (id (a [0] [0])) print (id (b [0] [0])) print ("*" * 40) a.append (4) a [ 1] [0] = a [1] [0] + 1print (a) print (b) print (id (a [4]))
Output
Print array address: 19388789795281938879043976 * print inner element address: 19388777312081938877731208140729246982560140729246982560 [[1,2,4], [4,5,8], [], 4] [[1,2] 4], [4, 5, 8], [], []] 140729246982656
So the assignment operation should be:
Import copya = [[1Jing 2 Jing 4], [3 Jing 5 Jing 8], [], []] # b = a.copy () b = copy.deepcopy (a)
Even if you use copy.deepcopy for assignment, b will not change after you operate on the inner elements of a.
However, it should be noted here (I have to say that this design saving memory is really subtle), if the values of the internal elements of an and b are not changed, then the values of the internal elements of an and b are still the same, and the address will change only if one party changes the value within its own address.
Import copya = [[1pr 2pr 4], [3pr 5pr 8], [] [] # b = a.copy () b = copy.deepcopy (a) print ("print array address:") print (id (a)) print (id (b)) print ("*" * 40) print ("print inner element address:") print (id (a [0])) print (id (b [0]) print ("0" * 50) print (id (a [0] [0])) print (id (b [0] [0])) ) print ("10" * 50) print ("\ n") print (a [1] [0]) print (b [1] [0]) print (id (a [1] [0]) print (id (b [1] [0])) print ("10" * 50) print ("\ n") # print ("*" 40) # a.append (4) a [1] [0] = a [1] [0] + 1 # print (a) # print (b) ) # print (id (a [4])) # print ("\ n") print ("10" * 50) print (a [1] [0]) print (b [1] [0]) print (id (a [1] [0])) print (id (b [1] [0])) print ("10" * 50) print ("\ n") b [1] [0] = b [1] [0] + 2print (b [1] [0]))
Output
打印数组地址:16962612902481696261352200****************************************打印内层元素地址:169625769204016943687072720000000000000000000000000000000000000000000000000014072913334313614072913334313610101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010103314072913334320014072913334320010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010431407291333432321407291333432001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010140729133343264到此,相信大家对"python开发中两个列表之间的传值过程介绍"有了更深的了解,不妨来实际操作一番吧! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.