python基础:5、字典

字典

#字典
#一个简单的字典
alien_0={"color":"green","points":5}
print(alien_0["color"])
print(alien_0["points"])

#字典是一系列键—值对
#添加键值对
alien_0={"color":"green","points":5}
print(alien_0)
alien_0["x_position"]=0
alien_0["y_position"]=25
print(alien_0)

#创建一个空字典
alien_0={}
alien_0["color"]="green"
alien_0["points"]=9
print(alien_0)

#修改字典中的值
alien_0={"color":"green","points":5}
alien_0["color"]="yellow"
print(alien_0)

#对一个能够以不同速度移动的外星人的位置进行跟踪。为此,我们将存储该外星人的当前速度,并据此确定该外星人将向右移动多远。
alien_0={"x_position":0,"y_position":25,"speed":"medium"}
print("Original x-position : "+str(alien_0["x_position"]))
#向右移动外星人
#据外星人当前的速度决定移动多远
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("New x-position:"+str(alien_0["x_position"]))
#删除键值对
alien_0={"color":"green","points":5}
print(alien_0)

del alien_0["points"]#永久删除
print(alien_0)
#由类似对象组成的字典
favorite_languages={
    "jen":"python",
    "sarah":"c",
    "edward":"ruby",
}
print(favorite_languages["jen"])
#动手414
memo={"first_name":"Yan","last_name":"Hao Xiang","age":17,"city":"ChongQing"}
print(memo["last_name"])
print(memo["first_name"])
print(memo["age"])
print(memo["city"])
number={
    "mjq":1,
    "dcx":2,
    "syx":3,
    "lyw":4,
}
print(number["mjq"])
print(number["dcx"])
print(number["syx"])
print(number["lyw"])

遍历字典

#遍历字典(●'◡'●)#
#1 遍历所有的键值对
user_0={
    "username":"efermi",
    "first":"enrico",
    "last":"fermi",
}
for k,v in user_0.items():
    print("\nKey:"+k)
    print("Value:"+v)
#2 遍历字典中的所有键
user_0={
    "username":"efermi",
    "first":"enrico",
    "last":"fermi",
}
friends=["fermi","phil"]
for name in user_0.keys():
    print(name.title())
    if name in friends:
        print("Hi")
#2.5 按顺序遍历字典中的所有键
user_0={
    "username":"efermi",
    "first":"enrico",
    "last":"fermi",
}
for k in sorted(user_0.keys()):
    print(k.title()+",thank you for taking the poll.")
#3 遍历字典中的所有值
user_0={
    "username":"efermi",
    "first":"enrico",
    "last":"fermi",
}
for v in user_0.values():
    print(v.title())
#利用set()可以使每一项都是独一无二
for v in set(user_0.values()):
    print(v.title())
#动手414
river={"nile":"egypt","b":"bc","j":"jk"}
for name,place in river.items():
    print("The  "+name.title()+"runs through "+place.title()+".")
for name in river.keys():
    print(name)
for place in river.values():
    print(place)
name=["kk","ll","mm"]
name_0={
    "n1":"kk",
    "n2":"oo",
    "n3":"uu"
}
for n in name_0.values():
    if n in name:
        print("HI")
    else:
        print("bye")

嵌套

#嵌套:字典存储在列表中或者将列表作为值存储在字典中。
#1 字典列表
name_1={"color":"red","points":9}
name_2={"color":"blue","points":11}
name_3={"color":"black","points":88}
names=[name_1,name_2,name_3]
for name in names:
    print(name)
#用range生成30个相同名字
names=[]
for name_number in range(30):
    new_name={"color":"red","points":9}
    names.append(new_name)

for name in names[:5]:
    print(name)
print("...")

#修改
for name in names[0:3]:
    if name["color"]=="red":
        name["color"]="yellow"
        name["speed"]="medium"
        name["points"]=10
for name in names[:5]:
    print(name)
print("...")
#2 在字典中存储列表
pizza={
    "crust":"thick",
    "toppings":["mushrooms","extra cheese"],
}
print("You ordered a"+pizza["crust"]+"-crust pizza"+"with the following toppings:")
for topping in pizza["toppings"]:
    print("\t"+topping) 
  fav_languages={
    "aad":["a","b"],
    "bbf":["c","d","e"],
    "ccg":["f","g","h","i"],
}
for name,languages in fav_languages.items():
    print("\n"+name.title()+"favorite languages are :")
    for language in languages:
        print("\t"+language.title()) 
#3 在字典中存储字典
users={
    "aeinstein":{
        "first":"albert",
        "last":"einstein",
        "location":"princeton",
        },
    "mcurie":{
        "first":"marie",
        "last":"curie",
        "location":"paris",
        },
}
for username,user_info in users.items():
    print("\nUsername:"+username)
    full_name=user_info["first"]+" "+user_info["last"]
    location=user_info["location"]
    
    print("\tFull name:"+full_name.title())
    print("\tLocation:"+location.title())
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值