Acquaintance python small pool of data collection

. ⼀ a ⼩ small data pool
before saying ⼩ small pool of data we look at ⼀ What a concept idea what is the code block..:

We found the prompts from the official ⽅ party files a document that argument:

A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter) is a code block. A script command (a command specified on the interpreter command line with the ‘-c‘ option) is a code block. The string argument passed to the built-in functions eval() and exec() is a code block.
A code block is executed in an execution frame. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed.

Slightly rough translation:
. Python program code block is composed of text files ⼀ a python code block as a program execution unit ⾏ trekking.
Block: ⼀ a module, a function ⼀, ⼀ a class to every even ⾄ ⼀ command is a command ⼀ a block of code. ⼀ files a document is ⼀ a block of code, eval () and exec () perform trekking when a block of code is ⼀

What is the command ⾏ trekking What?
We entered into the python is the command python in console mode CMD In individual cases from inside where you can write python program.
⽽ and look according to the official explanation ⽅ side surface of the ⾯ . ⼀ command is a command block ⼀ a python

⼆ II. Next we look at the difference between ⼩ ⼀ data pool is small and the == 1. id ()

By id () we can check ⼀ a variable scale monitor shows the value of the address in memory.

\ 2. Is and ==

== determination value are equal at both ends of the left and right. ⼀ it is not consistent.

is to determine the contents of both ends of the left and right of the memory address is ⼀ consistent. If it returns True, these two variables that can determine the amount of use Use an object is the same ⼀

s = 'alex'
print(id(s)) # 4326667072

We can think so. If the same memory address. ⼀ then the value must be equal. If the values ​​are equal. ⼀ not necessarily the same object ⼀

⼩ small data pool. ⼀ A data caching mechanism is also referred to as stay-resident mechanism. Each zoomed big programming words in your language and have similar east west ⻄ search constant volume pool ⽹ online, ⼩ refers to small data pool They are the same ⼀ a content.
⼩ small data pool only for: other types of data residing stay mechanism does not exist

Integer, string, string, Boolean

对于整数,Python官⽅方⽂文档中这么说:
The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined.

For string string:

Incomputer science, string interning is a method of storing only onecopy of each distinct string value, which must be immutable. Interning strings makes some stringprocessing tasks more time- or space-efficient at the cost of requiring moretime when the string is created or interned. The distinct

values ​​are stored ina string intern pool -. Auto cited from Wikipedia

In python integer between -5 and 256 are resident remain in memory. Set remains certain rules string string cache. In Use with time, the memory will only create a ⼀ of the data object. ⼩ data stored in the small pool. when using another use when acquiring an object directly from ⼩ small data pool memory using a lead. ⽽ ⼀ without the need to create a new data. this will save more additional memory area.

Advantages: the ability to provide high ⼀ ADVANCED some string string integer processing speed of the processing object creation province slightly drawbacks: Create a new content or insert 'pool' will take much more time.

For digital: -5 to 256 will be added to the pool each small data ⼩ Use with respect to the object are the same ⼀ string string:

  1. If the length of the string ⻓ string is 0 or 1, the cache will default into ⾏ trekking

  2. ⻓ zoomed terminated string length is greater than 1, but the string contains a word string ⺟ letters, numbers, underline not cached

  3. .1 Use string to the string of multiplication. Multiplier is 1, contain only numbers, letters ⺟ mother be cached underline. If

    Contain other characters, and ⻓ ⽽ length <= 1 will be stored in, zoomed multiplier greater than 2. 1. Contains only numeric characters, words ⺟ mother, the underline

    This time line is cached, but is not zoomed terminated string length is greater than 20 ⻓

  4. Specifies the resident to stay. We can specify the content to be hosted by the sys module to stay in the intern () function.

OK. To the destination time it is currently only for still. We have some basic understanding of the situation ⼀ python ⼩ small data pool down the road. But there !!!! last frame a question. ⼩ small pool of data and code beginning What blocks have anything to do with it? ⼀ the same piece of code ⾏ trekking in the command window-connector port and py files in the file. effect that occurs is completely ⼀ same.

Caching mechanism within the code block is not the same as ⼀. Trekking if you perform the same initialization command objects ⼀ of a code block, it checks whether the value already exists, if present, will use a weight Use In other sentence saying: when you perform the same ⼀ trekking a code block, encountered command to initialize the object, he will be initialized with the value of this variable amount ⼀ stored in a dictionary, in the face of a new variable amount, will when the first query records in the dictionary, if you have the same record then it will repeat using this value before using this dictionary, so in sub-submenus Example you give, the files document perform trekking (with ⼀ a code block) will a, b two variables ⼀ same amount to an object.

If a different code block, he would see the two variables is whether the amount of data over EMPTY foot ⼩ small pool of data, if the data is full EMPTY foot ⼩ small pool of data will point to the same address ⼀ so:. A , b assignment statement sentences are treated as two blocks of code perform trekking, but they are dissatisfied data EMPTY foot ⼩ small pool of data it will get two different objects, and is due to ⽽ judge returns False.

\ 1. The underlying data type supplement 2. set collection
\ 3. Tony depth copy ⻉

Contents:
⼀ a basic data type supplement.

First int and str therefore especially regarding prior learning has been talked about for more than 80% of the so rest automatically from ⼰ own look ⼀ a look on it .We supplement to the basic operation ⼀ a string string

Column list: a list of elements per loop ⼀ Remove Columns

li = [ "Li Ka-shing," "Twisted Vine," "Yellow Sea ⻩ peak", "Carina"] = S "_." the Join (li)
Print (S)

li = "⻩ zoomed yellow big Gui ⼥ female" s = "_". join (li) print (s)

li = [11, 22, 33, 44]
for e in li:
li.remove (e)
print (li)

Results: [22, 44]

Analyze the reasons:
. When running a trekking for the process will be ⼀ a pointer to record the elements of the current cycle in which a ⼀, ⼀ a pointer to the beginning of the first 0

A. Then get to the 0th element. Then delete the first 0. This time. The original is a first frame element will automatically automatically becomes the 0 then backward pointer moves once again, point 1 elements. This the original 1:00 has become a 0, it will not be deleted up.

Using the Delete try to use pop:

li = [11, 22, 33, 44]
for i in range (0, len (li)):
del li [i]
print (li)

Results: error
# i = 0, 1, 2 deleted when li [0] after being deleted after a ⼀ ⾯ surface becomes the first 0.
# I = 2 so the time when the list. ⼀ only one element, but this is the first time to remove two certainly being given ah

The analysis found. ⾏ not delete cycle trekking. Use either with or del Using a remove. Can not be realized. Then pop it?

Only in this way it is possible:

Or. Using another ⼀ with a list of columns to record what you want to delete. Then delete the cycle

EL in li for:
li.pop () # POP nor does ⾏ trekking

print (li) Results: [11, 22]

for i in range (0, len (li)): # cycle len (li) times, then remove it from the forward li.pop ()

print (li)

li = [11, 22, 33, 44]
del_li = []
for and in them:
del_li.append (s)
for and in del_li:
li.remove (s)

print (li)

Note: dict in fromkey (), can help us to create a dict ⼀ by list

⼀ before ⾯ face each column item in the list as the content will be key, column list after ⾯ plane as the value ⽣ generate dict better attention..:

Since removing elements will lead to changes in the index of elements, it is easy to easy to problems. Do not recycle as much as possible the amount directly to delete

In addition to the elements. You can add another element to remove ⼀ to a set amount and then bulk delete.

dic = dict.fromkeys ([ "jay", "JJ"], [ "Jay", "twist vine"]) print (dic)

Results:
{ 'Jay': [ 'Jay', 'twist vine'], 'JJ': [ ' Jay', 'twist vine']}

dic = dict.fromkeys ([ "jay", "JJ"], [ "Jay", "twist vine"]) print (dic)

dic.get("jay").append("胡⼤大") print(dic)

Results:
{ 'Jay': [ 'Jay', 'twist vine', 'large zoomed Hu'], 'JJ': [ ' Jay', 'twist vine', 'Great zoomed Hu']}

Just change the code more jay that the column list. However, due to jay and JJ use the same ⼀ Use a column list. So before ⾯ that changed the face after

⾯ face that change to follow

dict the elements in the iterative process is not allowed to enter ⾏ trekking deleted

dic = { 'k1': ' alex', 'k2': 'wusir', 's1': ' ⾦ Gold ⽼ old boss' #} with the delete key 'k' of the element
for k in dic:

IF 'K' in K:
del DIC [K]
Generation not allowed when trekking into the delete operation ⾏

print(dic)

# Dictionary changed size during iteration, the loop Diego

How to do it? The elements to be deleted before being saved in ⼀ a list, and then the cycle list, then deleted

dic = { 'k1': ' alex', 'k2': 'wusir', 's1': ' ⾦ Gold ⽼ old boss'} dic_del_list = []
elements # Delete key with the 'k' is
for k in dic:

​ if 'k' in k:
​ dic_del_list.append(k)
for el in dic_del_list:
​ del dic[el]

print(dic)

Conversion Type:
tuple => Columns list list (tuple) = column list> tuple tuple (list)

list=>str str.join(list) str=>list str.split()

False data is converted into the: 0, '', None, [], (), {}, set () ==> False

set collection

set is a collection of basic data types pnthon. .set in general is not very common elements are not repeated. disorder. the elements must be inside the hash (in str tuple bool), we can come to mind. dict types of data is set without saving value, save only the key, set also expressed by {}

Note: set of elements must be hash, but the set itself is not hash, the set is variable - the elements are not made

set2 = { '1', 'alex', 2, True, [1,2,3]} ------- ------- given inside []

set2 = { '1', 'alex', 2, True, {1: 2}} --------------- ------- given inside {}}

set3 = { '1', 'alex', 2, True, (1,2, [2,3,4])} ------- inside () []

set of elements is not repeated and disordered

s = { "Jay", "Chou", "circumferential stars"} print (s)

Results:
{ 'Week Star', 'Jay'} will not be repeated disorder -------

Using this feature, we can use the set to remove duplicate

lst = [45,5, "Hua Hua," 45, "China and China", 50]

lst = list (set (lst)) ------- switched to the list and then set exchange List

print(lst)

set set CRUD

1. Increase

s = { "Carina", "Kwan", "Cheng Li", "rear Little Green", "JOURNAL ON", "JiangYang"}

s.add ( "China Bear")

print(s)

s = add. ( "China Bear") ------- duplicate content is not added to the set collection

print(s)

s = { "Carina", "Kwan", "Cheng Li", "rear Little Green", "JOURNAL ON", "JiangYang"}

s.update ( "dragon pool") -------------- iterative added

print (s) ------ result is { "Carina", "Kwan", "Cheng Li", "rear Little Green", "JOURNAL ON", "JiangYang" Dragon "," pool "}

s.update ([ "Maggie", "Zhangya Zhi", "Zhangya Zhi"])

print (s) -------- result is { "Carina", "Rosamund", "Cheng Li", "after Xiaoqing," "JOURNAL ON", "Jiang Yang", "Zhang" Man, "" Jade " "Ya," "Chicago," "}

2. Delete

s = { "Carina", "Kwan", "Cheng Li", "rear Little Green", "JOURNAL ON", "Jiang Yang", "dragon pool"}

item = s.pop () ------------- up a random

print(s)

print(item)

s.remove ( "the Little Green") ---------- delete elements

s.remove ( "Aoi") ------ I do not love this woman this woman does not exist error will be deleted

print(s)

s.clear () ------- empty the collection set. Note that set if the set is empty. printout is set () and dict because to distinguish.

print()-----------set()

modify

Data set collection no index. There is no way a positioning element, so there is no way to directly edit.

We can use the delete first add the way to complete the modification operation

s = { "Carina", "Kwan", "Cheng Li", "rear Little Green", "JOURNAL ON", "Jiang Yang", "dragon pool" -----} into the longchi Timo

s.remove ( "dragon pool")

s.add ( "Timo")

print(s)

Inquire

is an iterative set object can be performed for loop

for el in s :

Print (el)

Common Operations

S1 = { "Liu can", "Zhao Si", "long ⼭ ⽪ skin ⻓ mountain"}

s2 = { "Liu Section ⻓ long", "rural ⻓ von long", "long ⼭ ⽪ skin ⻓ mountain"}

Intersection

Two sets of elements common to

Print (s1 & s2) -------- { "Long Hill Paper"}

Union

print (s1 | s2) # { 'Liu Section ⻓ length', 'long ⻓ von rural', 'Zhao Si', 'long ⼭ ⽪ skin ⻓ mountain', 'can Liu'}

Difference set

print (s1 - s2) # { 'Zhao Si', 'can Liu'} obtained in the presence of a first frame single

Anti intersection

print (s1 ^ s2) # present in two separate data set { 'von Township ⻓ length', 'Liu can', 'long ⻓ Liu Section', 'Zhao Si'}

s1 = { "Liu can", "Zhao Si"}
S2 = { "Liu can", "Zhao Si", "long ⼭ ⽪ skin ⻓ mountain"}

Child Collection

print (s1 <s2) # set1 submenus that are a subset of set2 do? True

Superset

print (s1> s2) # set1 set2 is a superset of it? False

Copy depth

lst 1 = [ "⾦ Gold Lion ⽑ hair", "paclitaxel ⻰ Dragon", "White Baimeiyingwang", "⻘ Sowosky Bat"]

lst 2 = lst 1

print(lst1) ———————————————

print (lst2) ________________-------- two identical results

lst1.append ( "Yang Xiao")

print(lst1)

print (lst2) two identical results [ '⾦ Gold ⽑ hair Lion', 'paclitaxel ⻰ dragon', 'white Baimeiyingwang', '⻘ Sowosky Bat', 'Yang Xiao']

dic1 = {"id": 123, "name": "谢逊"}

dic2 = dic1

print(dic1)
print(dic2)

dic1['name'] = "范瑶"

print(dic1) print(dic2)

For the list, set, dict, the direct assignment fact is the memory address to a variable amount. ⼀ not copy a piece of content. So.
LST1 memory points and lst2 is ⼀ same. Lst1 changed, also made lst2 ⽣ had changed

Shallow copy ⻉

lst1 = [Lion King "," Dragon paclitaxel "," eyebrow White Eagle King "," Bat ⻘ wing "]

lst2 = lst1
print(lst1)
print(lst2)

lst1.append ( "Yang Xiao")

print(lst1)

print(lst2)

Results:
[Gold ⽑ hair Lion ',' paclitaxel Dragon King ',' white eyebrow Ying Wang ',' ⻘ wing Bat ',' Yang Xiao '] [' ⾦ hair Lion ',' paclitaxel Dragon King ',' Ying Wang eyebrow white ',' ⻘ wing bat ',' Yang Xiao ']

dic1 = {"id": 123, "name": "谢逊"} dic2 = dic1
print(dic1)
print(dic2)

dic1['name'] = "范瑶"

print(dic1)

print(dic2)

结果: {'id': 123, {'id': 123, {'id': 123, {'id': 123,

'Name': 'Xie Xun'} 'name': 'Xie Xun'} 'name': 'Van Yao'} 'name': 'Van Yao'}

For the list, set, dict, the direct assignment fact is the memory address to a variable amount. ⼀ not copy a piece of content. So. Lst1 memory points and lst2 is ⼀ same. Lst1 the changes, also lst2 ⽣ had made the change

Shallow copy ⻉

lst1 = [ "Kyung Ho", "Du Haitao", "circumferential Min Yu ⺠"]

lst2 = lst1.copy () lst1.append ( "shing")

print(lst1)

print(lst2)
print(id(lst1), id(lst2))

Result:
two lst ⼀ not completely the same memory address and the content is not the same as not ⼀ found realizes a memory copy ⻉ shellfish.

lst1 = [ "Ho Kyung", "Du Haitao", "circumferential Yu ⺠ Min" [ "twist vine", "⻢ Yun Ma", "Bibi"]] lst2 = lst1.copy ()
LST1 [. 3] .append ( "⽆ invincible is more grinding lonely")
Print (LST1)

print(lst2)
print(id(lst1[3]), id(lst2[3]))

Results:
[ 'He Jiong', 'Du Haitao,' 'Zhou Yu ⺠ people', [ 'twist vine', '⻢ Ma Yun', 'Bibi', '⽆ Invincible is more grinding lonely']] [ 'He Jiong' ' Du Haitao, '' Zhou Yu ⺠ people ', [' twist vine ',' ⻢ Ma Yun ',' Bibi ',' ⽆ invincible is more grinding lonely ']] 4417248328 4417248328

Shallow copy ⻉ Tony. Tony only copy ⻉ first frame layer. Content second shot of the second floor will not copy ⻉ shell. It is called a shallow copy ⻉ Bay

Tony deep copy ⻉

import copy

lst1 = [ "Ho Kyung", "Du Haitao", "circumferential Yu ⺠ Min" [ "twist vine", "⻢ Yun Ma", "Bibi"]] lst2 = copy.deepcopy (LST1)
LST1 [. 3] .append ( "⽆ invincible is more grinding lonely")
Print (LST1)

print(lst2)
print(id(lst1[3]), id(lst2[3]))

Results:
[ 'He Jiong', 'Du Haitao,' 'Zhou Yu ⺠ people', [ 'twist vine', '⻢ Ma Yun', 'Bibi', '⽆ Invincible is more grinding lonely']] [ 'He Jiong' ' Du Haitao, '' Zhou Yu ⺠ people ', [' twist vine ',' ⻢ Ma Yun ',' Bibi ']]
4447221448 4447233800

Guess you like

Origin www.cnblogs.com/x-h-15029451788/p/11324556.html