程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python day 5

編輯:Python

One 、dict and list in copy Method drawing and code explanation

1、dict Shallow copy

dict Shallow copy ,dict_data1 and dict_data2 The two are independent , But the elements inside point to the same ( It's like a person working in two places ), So whether it's 1 still 2, As long as there is one that changes the element , The other will change with it .( It is similar to this person who was injured at the first place of work , I can only come to the second place of work with injuries ).

2、dict Deep copy

dict Deep copy, full copy ( Similar to two twins , Looks exactly the same , But two individuals ), They are completely independent , How to change one of them , The other will not change ( Even if one had an accident , Less organs , The other is still a perfect person without change ).

 

3、list Shallow copy

list In a shallow copy list_data and list_data2 Is two independent bodies , The outermost layer of the list changes , The other doesn't change ; The innermost layer of the list changes , The other will change accordingly .

 4、list Deep copy

list Deep copy list_data and list_data2 It's two separate individuals , No matter how the elements inside change , The other doesn't change .

 

  Two 、dict Extended dictionary length in ( many-ways )

1、setdefault

Insert key with a value of default if key is not in the dictionary.

      If it's not in the dictionary , Then insert the key with the default value .

2、update

    update([E, ]**F)-> None. Update D from dict/iterable E and F.

      From the dictionary / Can the iteration E and F to update D. 

3、[key] = value

 ps: This warning is to remind me that these two can be combined into a new dictionary

3、 ... and 、dict Get all the key, Get all value, Get all item.

1. obtain key

 2. obtain value

 3. obtain item

Four 、dict Through a nonexistent key To get the value ( No error is required )

get and .setdefault Can pass through nonexistent key Get value , Don't complain ( The output value is None) 

  5、 ... and 、 Define empty sets

6、 ... and 、set in , Difference set , Ask for a collection , Find the intersection ( Requires operation on the original set )

1、 Difference set

 

2、 Collection

 

3、 intersection

 

 4、 Add an element

add

Add elements to the collection

5、 Delete an element ( Using a variety of methods ) 

pop

Delete and return the first element

remove

Remove elements from collection ; It must be a member

 

 discard

If the element is a member , Delete the element from the collection  

difference_update

Remove all elements of another group from this collection .

 

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved