First, the dictionary
1. Create and Access dictionaries (the dictionary is the curly braces, the dictionary is the mapping type)
Syntax type: Keys: Key, Values: value, separated by colons
---
>>> Dict1 = {' Li ning ': ' Everything is possible ', ' Nike ': ' Just do it ', ' Adidas ': ' Impossible is nothing ', ' Fish C Studio ': ' Programming Changes the world '}
>>> print (' Fish C Studio ' slogan is: ', dict1[' Fish C Studio ']) #dict1 [' Fish C Studio ']): plus the key
The slogan of the Fish C Studio is: Programming changes the world
2, try to put the data (' F ': ", ' C ':", "' h:104, ' I ':", ' s ': 115) to create a dictionary and Access ' C ' corresponding value?
>>> dict1={' F ': +, ' C ': ", ' H ': 104, ' I ':", ' s ': 115}
>>> print (dict1[' C ')
67
>>>
-----------------------------------------
>>> dict2 = dict ((' I ', + +), (' s ', ' 67 '), (' H ', 104), (' C ', ' + ') ') ((' F ', ', ') ')
>>> dict2[' C ']
67
>>>
3. Create a centralized way of dictionaries:
>>> a = Dict (one=1, two=2, three=3)
>>> B = {' One ': 1, ' both ': 2, ' Three ': 3}
>>> C = dict (Zip ([' One ', ' B ', ' three '), [1, 2, 3])
>>> d = dict ([' One ', 1), (' Three ', 3)])
>>> e = dict ({' Three ': 3, ' one ': 1, ' One ': 2})
The results are printed: {' One ': 1, ' both ': 2, ' Three ': 3}
4. Combination of dictionaries and strings:
data = "1000, small turtle, male"
Mydict = {}
# Remember the way the string is split, don't learn it, forget it ^_^
(mydict[' id '], mydict[' name '], mydict[' sex ') = Data.split (', ')
Print ("ID:" + mydict[' id ')
Print ("Name:" + mydict[' name ')
Print ("Sex" + mydict[' sex ')
‘‘‘
Execution Result:
id:1000
Name: Small Turtle
Sex Guy
‘‘‘
Split () function:
There are two functions for split () and Os.path.split () in Python, as follows:
Split (): Splits the string. Slices a string by making a delimiter and returns the segmented list of strings. such as: Split (', ') through, number delimited
Os.path.split (): Separates file names and paths by path
First, function description
1. Split () function
Syntax: Str.split (str= "", Num=string.count ()) [n]
Parameter description:
STR: Represents a delimiter, the default is a space, but cannot be empty ('). If there is no delimiter in the string, the entire string is used as an element of the list
Num: Indicates the number of separators. If there is a parameter num, it is separated into only num+1 substrings, and each substring can be assigned to a new variable
[n]: Indicates the selection of the first few slices
Note: When spaces are used as separators, items that are empty in the middle are automatically ignored
2. Os.path.split () function:
Syntax: Os.path.split (' path ')
Parameter description:
1. Path refers to the full path of a file as a parameter
2, if the given is a directory and file name, then the output path and filename
3, if the given is a directory name, then the output path and an empty file name
Second, examples
1. Common examples
u = "www.yizhibo.com"
#使用默认分隔符
>>> U.split ()
[' www.yizhibo.com ']
#使用 '. ' delimiter, and separated 1 times
>>> u.split ('. ', 1)
[' www ', ' yizhibo.com ']
#分隔两次
>>> u.split ('. ', 2)
[' www ', ' yizhibo ', ' com ']
#分隔2次, and take a sequence of 1 items
>>> u.split ('. ', 2) [1]
' Yizhibo '
#分隔最多次 (actual with no num parameter)
>>> u.split ('. ',-1)
[' www ', ' yizhibo ', ' com ']
#分隔两次, and save the split three sections to three files
>>> u1,u2,u3=u.split ('. ', 2)
>>> U1
' www '
>>> U2
' Yizhibo '
>>> U3
' com '
2. Remove newline characters
>>> C = "' Say
Hello
Baby ""
>>> C
' Say\nhello\nbaby '
>>> c.split (' \ n ')
[' Say ', ' hello ', ' baby ']
3, a super good example:
>>> str= "Hello Boy<[www.doiido.com]>byebye"
>>> str.split ("[") [1].split ("]") [0]
' Www.doiido.com '
>>> str.split (' [') [1].split ('] ') [0]
' Www.doiido.com '
>>> str.split ("[") [1].split ("]") [0].split ('. ')
[' www ', ' doiido ', ' com ']
Third, hands-on questions:
1, try to use the characteristics of the dictionary to write an address book program, function
Specific code:
Print ("" "
|---Welcome to the Address Book program---|
|---1: find contact Information---|
|---2: insert a new Contact---|
|---3: delete an existing contact---|
|---4: Exit Address Book Program---| "")
Key = 5
Dict1 = {' Dusty ': ' 18088888888 '}
While key!=4:
key = Int (input (' Please enter the relevant instruction code: '))
If key = = 1:
temp = input ("Please enter the name of the contact person:")
If temp in Dict1:
Print ('%s:%s '% (temp,dict1[temp))
Else
Temp1 = input ("Please enter contact Phone:")
Dict1[temp]=temp1
Print (' Added contact Profile:%s--+%s '% (Temp,dict1[temp]))
Continue
Elif Key ==2:
temp = input ("Please enter the name of the contact person:")
If temp in Dict1:
Print (' The name you entered already exists in Address Book-->%s:%s '% (Temp,dict1[temp])
x = input ("Whether to modify user resources (yes/no)")
if x = = ' YES ':
Tel = input ("Please enter user Contact number:")
dict1.update ({Temp:tel})
Print (' Modified contact profile is-->%s:%s '% (Temp,dict1[temp]))
Else
Continue
Else
Temp1 = input ("Please enter contact Phone:")
Dict1[temp]=temp1
Print (' Added contact Profile:%s--+%s '% (Temp,dict1[temp]))
Elif Key ==3:
temp = input ("Please enter the name of the contact you want to delete:")
If temp in Dict1:
Flag = input ("OK to delete this contact?") (yes/no) ")
If flag = = ' YES ':
del (dict1[temp])
Print (' Contact%s ' deleted successfully! '%temp)
Else
Continue
Else
Print ('%s is not in Address Book! '%temp)
Elif Key ==4:
Print ("|---Thanks for using the Address Book Program---|")
Break
==================== RESTART:/users/wufq/desktop/Address Book. PY ====================
|---Welcome to the Address Book program---|
|---1: find contact Information---|
|---2: insert a new Contact---|
|---3: delete an existing contact---|
|---4: Exit Address Book Program---|
Please enter the relevant instruction code: 1
Please enter the name of the contact person: Dusty
Dusty: 18088888888
Please enter the relevant instruction code: 2
Please enter the name of the contact person: Dusty
The name you entered is already present in your contacts-dust-laden: 18088888888
Whether to modify user resources (yes/no) YES
Please enter the user contact phone: \15624963195
Modified contact information-dust-laden: 15624963195
Please enter the relevant instruction code: 3
Please enter the name of the contact you want to delete: Dusty
Are you sure you want to delete this contact? (yes/no) YES
Contact Dust Removal Success!
Please enter the relevant instruction code: 4
|---Thanks for using the Address Book program---|
>>>
==================== RESTART:/users/wufq/desktop/Address Book. PY ====================
"Python025-Dictionary"