字典

簡單的字典:
    
  字典就是鍵值對key-value組合。
1 #字典  鍵值對組合
2 alien_0 ={'color':'green','number':5}
3 print(alien_0['color'])
4 print(alien_0['number'])

  

  添加鍵值對java

1 alien_0['first_name'] = 'mo'
2 alien_0['last_name'] = 'ak'
3 print(alien_0)

 

 
使用字典:
 1 #訪問元素
 2 favorite_languages ={
 3     'jen':'python',
 4     'sarah':'c',
 5     'edwoad':'ruby',
 6     'phil':'python',
 7 }
 8 
 9 print(favorite_languages['jen'])
10 
11 #刪除鍵值對
12 del alien_0['points']

 

遍歷字典:
1 favorite_numbers = {
2     'mx' : '6',
3     'cg' : '365',
4     'zxp': '8',
5 }
6 for key,value in favorite_numbers.items():
7     print("\nkey:"+key)
8     print("value:"+value)

 

遍歷字典中的全部鍵:
1 favorite_numbers = {
2     'mx' : '6',
3     'cg' : '365',
4     'zxp': '8',
5 }
6 
7 for name in favorite_numbers.keys():
8     print(name.title())

 

嵌套:
 1 #字典列表:
 2 alien_0 = {'color':'green','points':5}
 3 alien_1 = {'color':'red','points':6}
 4 alien_2 = {'color':'blue','points':3}
 5 
 6 aliens = [alien_0,alien_1,alien_2]
 7 for alien in aliens:
 8     print(alien)
 9     
10 
11 #字典中存儲列表:
12
13 favorite_language ={
14     'jen':['python','c'],
15     'mx':['java','python'],
16 }
17 for name,languages in favorite_language.items():
18     print("\n\t"+name+str(languages))
19     for language in languages:
20         print(language)
21 
#字典中存儲字典:
#暫略
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">python



相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息