South  ×  substituting  pregnancy M

South  ×  micro-generation signal  pregnancy M █ █: 138-0226-9370█ █ █ surrogate packet success packet healthy surrogate ██

 First, the dictionary (dict)

In the first operation of a user name and password there is a list, and the user name and password required correspondence, stored this way more strenuous, this time with a dictionary can be installed under the user name and password information user, which is a dictionary the most simple application:

accounts = {
    'Lily':'123456',
    'Lucy':'aaaaaa',
    'Emily':'hahaha'
}

 Inside the message is: in the form of 'key value', and with a key-value pair between a ',' split, this value is also convenient manner, can also be used to access a class student information, i.e., the dictionary is embedded set. Stored information at a glance, easy to read, but at the time of writing to pay attention to the format.

Copy the code
stus = {
    'a':{
        'phone':'13511111111',
        'qq':'123456',
        'addr':'new york'
    },
    'b':{
        'phone':'15677777777',
        'qq':'00000000',
        'addr':'los angelas'
    }
}
Copy the code

Above the dictionary, to take the way qq = 123456:  stus [ 'A'] [ 'QQ'] 

In addition, the dictionary are unordered, see output that can be seen when printed dictionary.

Here are some methods dictionary summarize:

increase:

accounts [ 'april'] = ' abc123' #key present when the original value changes 
accounts.setdefault ( 'Lily', '111111 ') , without changing the present values #key

delete:

accounts.pop('Lily')
del accounts['Lily']

change:

Directly through the key way to find, get the re-assignment, the first method is equivalent to "increase" in

check:

accounts [ 'abby'] # when an error can not find the key, is not recommended 
accounts.get ( 'abby') # return None can not find key 
accounts.get ( 'Abby', 'Abbie') # can not find ' abby 'when this key, returns' abbie'

Dictionary splicing:

accounts.update (stus) # stus stitching in the accounts, and the results updated in the accounts

cycle:

There are two ways, one cycle is k, a k and v is cyclic. as follows:

Copy the code
The first 1 # 
2 for A in Accounts: 
. 3 Print (A) is accessible to # Key 
. 4 Print (Accounts [A]) to the value taken by # Key 
. 5 
. 6 second # 
7 for k, v in accounts. items (): #items () method is used to take all the dictionary and key value 
. 8 Print (K, V) and # direct access to the key value, but is preferably a first performance data in this way suitable for small batch
Copy the code

In addition, lists, strings can be recycled.

A brief summary: the first time to learn the dictionary, it is easy to confuse with the list, then forget again soon. The contact made in a timely manner, yet remember, do not mix. But when the job in question to make a fat ignorant of my head, is in practice to define a nested dictionary, read, read a mess, left and right a dictionary a dictionary, which is a variable that a variable, I was still the to define a dictionary-style guessing resolved to write down in a book. Here the first to write about it when a return to practice.

The figure above two dictionaries, a users, a info, info me as a user input (key: username) of the value, so to define two dictionaries. Then users of this dictionary, key variable names unified username, value of a variable named info {}. In this way, we can put another dictionary info as the value assigned to the username.

Copy the code
= {} Users 
info = {} 

List = [ 'userA', 'pwdb']     
username = List [0] 

Users [username] = info # dictionary is equivalent to increasing the value of 
info [ 'password'] = list [1] # assignment to the dictionary in the nest
Copy the code

 

Second, the method of the string

Too many string methods ... simple order under Note that they are doing it, to facilitate future when you need to look up. It should be noted that all the methods of the string does not change the original value of the string will return a new value, because the string can not be modified.

Go to space:

Find a subscript characters:

Other (method contains the last string format, the previous description differs in that, where transmission is to own values, not variables):

Further, format and format_map:

A string of important ways:

1. string transfer list

month = 'January,February,March,April,May'

monthList = month.split(',')

print(monthList)    #结果为:monthList:[‘January’,'February','March','April','May']

2. list can also be converted to string

= monthList [ 'January', 'February', 'March', 'April', 'On May'] 

month The = ',' the Join (monthList) # written from memory actually written:. month = monthList.join ( ', ')

The reason I was wrong, because there is no clear understanding, join () method is a string method, you need to use at some later with string. When the string list turn, is to ',' as a string, using the join () method, with ',' pass behind the list of elements spaced apart and together into a string. Besides characters split (), this method is used in a mass split () (the sequence) are known to split the string, and in the divided portion of the list.

By finishing learned a lot, maybe they should not mess with method 8.

 

Triad

Tuple is a list, but with the same string can not be modified

Defined manner:  T = (1,2,3,4) 

Several methods and properties:

Suitable for storing some daily use, but the content can not be altered, such as login information database and so on.

 

Next write file operation.

Guess you like

Origin www.cnblogs.com/DINGER01/p/10942533.html