Python笔记#边学边记笔记#字典

一、使用字典

1.1 访问字典中的值

字典使用花括号;键与值之间用冒号连接;各个键-值对之间用逗号分隔。

alien_0={"color":"green","point":5}
print(alien_0["color"])
print(alien_0["point"])
green
5

1.2 添加键-值对

alien_0={"color":"green","point":5}
print(alien_0)
alien_0["x_point"]=5
alien_0["y_point"]=65
print(alien_0)
{'color': 'green', 'point': 5}
{'color': 'green', 'point': 5, 'x_point': 5, 'y_point': 65}

1.3 删除键-对值 

alien_0={"color":"green","point":5}
print(alien_0)
del alien_0["color"]
print(alien_0)
{'color': 'green', 'point': 5}
{'point': 5}

 1.4 由类似对象生成的字典

Universities={
    "gwt":"Peking University",
    "pyx":"Nanjing University&Johns Hopkins University",
    "tjz":"Beijing University Of Posts and Telecommunications",
    "qsj":"Communication University of China",
    "zzw":"McGill University",
    "sk":"Sichuan Normal University",
    "hyc":"Beijing University of Aeronautics and Astronautics",
}
print(Universities["gwt"])
Peking University

二、遍历字典

2.1 遍历字典的键-值对

for key,value in universities.items():

Universities={
    "gwt":"Peking University",
    "pyx":"Nanjing University&Johns Hopkins University",
    "tjz":"Beijing University Of Posts and Telecommunications",
    "qsj":"Communication University of China",
    "zzw":"McGill University",
    "sk":"Sichuan Normal University",
    "hyc":"Beijing University of Aeronautics and Astronautics",
}
for person,university in Universities.items():
    print(person.title()+" was graduated from "+university.title()+".")
Gwt was graduated from Peking University.
Pyx was graduated from Nanjing University&Johns Hopkins University.
Tjz was graduated from Beijing University Of Posts And Telecommunications.
Qsj was graduated from Communication University Of China.
Zzw was graduated from Mcgill University.
Sk was graduated from Sichuan Normal University.
Hyc was graduated from Beijing University Of Aeronautics And Astronautics.

2.2 遍历字典的键或值 

遍历字典的键key和值value

for person in Universities.keys():
    print(person.title())

for university in Universities.values():
    print(university.title())

2.3 创建值的集合 

languages={
    "lihua":"C",
    "xaioming":"python",
    "xiaohong":"java",
    "lingling":"python",
}
####创建值的集合,可剔除重复项
for language in set(languages.values()):
    print(language.title())
C
Java
Python

三、嵌套

3.1 字典列表

item_0={"color":"blue","point":5}
item_1={"color":"black","point":8}
item_2={"color":"green","point":10}
items=[item_0,item_1,item_2]

for item in items:
    print(item)
{'color': 'blue', 'point': 5}
{'color': 'black', 'point': 8}
{'color': 'green', 'point': 10}

        生成30个绿色外星人

#生成30个绿色外星人
aliens=[]
for number in range(30):
    new_alien={"color":"green","speed":"slow","point":5}
    aliens.append(new_alien)
#显示前五个外星人的数据
for alien in aliens[:5]:
    print(alien)
#显示创建的外星人的数量
print(str(len(aliens)))
{'color': 'green', 'speed': 'slow', 'point': 5}
{'color': 'green', 'speed': 'slow', 'point': 5}
{'color': 'green', 'speed': 'slow', 'point': 5}
{'color': 'green', 'speed': 'slow', 'point': 5}
{'color': 'green', 'speed': 'slow', 'point': 5}
30

3.2 在字典中存储列表

favorite_languages={
    "xiaoming":["python","c"],
    "lihua":["java","r"],
    "xiaohong":["c++","python"]
}
for name,languages in favorite_languages.items():
    print("\n"+name.title()+"'s favorite languge is:")
    for language in languages:
        print(language.title())
Xiaoming's favorite languge is:
Python
C

Lihua's favorite languge is:
Java
R

Xiaohong's favorite languge is:
C++
Python

3.3 在字典中存储字典

cities={
    "xc":{
        "populations":100,
        "first":"xuan",
        "last":"cheng",
    },
    "gc":{
        "populations":200,
        "first":"gu",
        "last":"cheng",
    },
    "hc":{
        "populations":600,
        "first":"huang",
        "last":"cheng",
    },
}
for city,city_item in cities.items():
    print("\nCity_short_name:"+city)
    full_city_name=city_item["first"]+city_item["last"]
    population=city_item["populations"]
    print("\tcity_full_name:"+full_city_name)
    print("\tcity_populations:"+str(population))
City_short_name:xc
	city_full_name:xuancheng
	city_populations:100

City_short_name:gc
	city_full_name:gucheng
	city_populations:200

City_short_name:hc
	city_full_name:huangcheng
	city_populations:600

你可能感兴趣的:(python,笔记)