python基础(五)
#字典,访问字典中的值 alien_0 = {'color':'green','points':5} print(alien_0['color']) print(alien_0['points'])
green 5
#将值存储在变量中new_points中 new_points = alien_0['points'] print("You just earned " + str(new_points) + " points!")
You just earned 5 points!
#添加键-值对;指定字典名,用方括号括起的键和相关联的值 alien_0 = {'color':'green','points':5} print(alien_0) alien_0['x_position'] = 0 alien_0['y_position'] = 25 print(alien_0)
{'color': 'green', 'points': 5} {'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
#先创建一个空字典 alien_0 = {} alien_0['color'] = 'green' alien_0['points'] = 5 print(alien_0)
{'color': 'green', 'points': 5}
#修改字典中的值 alien_0 = {'color':'green'} print("The alien is " + alien_0['color'] + ".")
The alien is green.
alien_0 = {'color':'yellow'} print("The alien is now " + alien_0['color'] + ".")
The alien is now yellow.
#创建一个字典 alien_0 = {'x_position':0, 'y_position':25, 'speed':'medium'}
print("Original x-position: " + str(alien_0['x_position']))
Original x-position: 0
if alien_0['speed'] == 'slow': x_increment = 1 elif alien_0['speed'] == 'medium': x_increment = 2 else: x_increment = 3
alien_0['x_position'] = alien_0['x_position'] + x_increment#通过修改外星人字典中的值,可改变外星人的行为。
print(str(alien_0['x_position']))
2
alien_0['x_position']
2
#删除键值对 alien_0 = {'color':'green','points':5} print(alien_0) del alien_0['points'] print(alien_0)
{'color': 'green', 'points': 5} {'color': 'green'}
#由类似对象组成的字典,注意这个地方的逗号! fav_flo = { 'baba':'hehua', 'mama':'meihua', 'jingjing':'xiangrikui' }
print("Jingjing's favorite flower is " + fav_flo['jingjing'].title() + ".")
Jingjing's favorite flower is Xiangrikui.
#遍历字典 fav_flo = { 'baba':'hehua', 'mama':'meihua', 'jingjing':'xiangrikui' } for key, value in fav_flo.items(): print("\nKey:" + key) print("Value:" + value)
Key:baba Value:hehua Key:mama Value:meihua Key:jingjing Value:xiangrikui
#遍历字典中所有键值对 for key, value in fav_flo.items(): print("\n" + key.title() + "'s favorite flower is " + value.title() + "!")
Baba's favorite flower is Hehua! Mama's favorite flower is Meihua! Jingjing's favorite flower is Xiangrikui!
#遍历字典中所有键 for key in fav_flo.keys(): print(key)
baba mama jingjing
ai = ['baba','mama'] for key in fav_flo.keys(): print(key.title())
Baba Mama Jingjing
favorite_languages = { '1':'python', '2':'c', '3':'ruby', '4':'python', } friends = ['1', '2'] for name in favorite_languages.keys(): print(name.title()) #bug for name in friends: print(name.title())
1 2 3 4 1 2
for name in friends: print("Hi" + name.title() + ", I see your favorite languages is " + favorite_languages[name].title() + "!")
Hi1, I see your favorite languages is Python! Hi2, I see your favorite languages is C!
favorite_languages['2'].title()
'C'
if '5' not in favorite_languages.keys(): print("ahhaha")
ahhaha
child_age = { 'yue':'12', 'zai':'10', 'lu':'19' }
for name, age in child_age.items(): print(name.title() + age.title())
Yue12 Zai10 Lu19
didi = ['zai', 'lu'] for name in didi: print("\n" + name.title() + child_age[name].title())
Zai10 Lu19
#按顺序遍历字典中的所有键 favorite_languages = { '1':'python', '2':'c', '3':'ruby', '4':'python', } for name in sorted(favorite_languages.keys()): print(name.title() + "hihi")
#遍历字典中的所有值 favorite_languages = { '1':'python', '2':'c', '3':'ruby', '4':'python', } for language in favorite_languages.values(): print(language.title())
Python C Ruby Python
#遍历唯一的值 for language in set(favorite_languages.values()): print(language.title())
C Python Ruby
#嵌套,在列表中嵌套字典,或者在字典中嵌套表 #字典列表 alien_0 = {'color':'green', 'points':5} alien_1 = {'color':'red', 'points':4} alien_2 = {'color':'blue', 'points':3}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens: print(alien)
{'color': 'green', 'points': 5} {'color': 'red', 'points': 4} {'color': 'blue', 'points': 3}
aliens = [] for alien_number in range(30): new_alien ={'color':'green', 'points':5, 'speed':'slow'} aliens.append(new_alien)
for alien in aliens[:5]: print(alien) print("...") print("Total number of aliens:" + str(len(aliens)))
{'color': 'green', 'points': 5} {'color': 'red', 'points': 4} {'color': 'blue', 'points': 3} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'} ... Total number of aliens:33
witchs = [] for witch_number in range(30): new_witch = {'color':'green', 'points':5, 'speed':'slow'} witchs.append(new_witch)
for witch in witchs[:5]: print(witch)
{'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'}
for witch in witchs[0:3]: if witch['color'] == 'green': witch['color'] = 'yellow' witch['speed'] = 'medium' witch['points'] = 10
for witch in witchs[:5]: print(witch)
{'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'yellow', 'points': 10, 'speed': 'medium'} {'color': 'green', 'points': 5, 'speed': 'slow'} {'color': 'green', 'points': 5, 'speed': 'slow'}
#在字典中存储列表 pizza = { 'crust': 'thick', 'toppings': ['mushroom', 'extra cheese'], }
print("You order a " + pizza['crust'] + "-crust pizza" + "with the following toppings") for topping in pizza['toppings']: print("\t" + topping)
You order a thick-crust pizzawith the following toppings mushroom extra cheese
aaa_111 = { 'a':['1', '2'], 'b':['3', '4'], 'c':['5', '4'], } for key, values in aaa_111.items(): print("\n" + key.title() + " hahah") for value in values: print("\t" + value.title())
A hahah 1 2 B hahah 3 4 C hahah 5 4
#在字典中存储字典 users = { 'aeinstein':{ 'aa':'aaa', 'bb':'bbb', 'cc':'ccc', }, 'mcurie':{ 'aa':'ddd', 'bb':'fff', 'cc':'ggg', }, }
for username, user_info in users.items(): print("\nUsername: " + username) full_name = user_info['aa'] + " " + user_info['bb'] location = user_info['cc'] print(full_name) print(location)
Username: aeinstein aaa bbb ccc Username: mcurie ddd fff ggg
标签:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程