Python小辣鸡的逆袭第六章之字典

第六章 字典

6.1 简单字典示例

    字典格式 eg:alien_={'color':'green','points':'5'}

    花括号括起来,由键-值对构成,键与值之间用冒号隔开,键-值对之间用逗号隔开

    与键相关联的可以是任何不可变的python对象,需要注意的是List是可变的,不可用

    一一对应的关系

6.2 使用字典
6.2.1 访问字典中的值
6.2.2 添加键-值对

    动态结构,可随时添加   eg:alien_['x']=0 #赋值时键要用方括号[]括起来

                                                    alien_['y']=20

                                                    print(alien_)

                                                    {'color':'green',points:'5','x':'0','y':20} #不关心添加顺序

6.2.3 修改字典中的值

    直接覆盖

6.2.4删除键-值对

    del alien['XX']

    alien.pop('XX')

扫描二维码关注公众号,回复: 2314683 查看本文章
6.2.5 由类似对象组成的字典

  favorite_color={

          'jen':'green',

          'judy':'grey',#最后一个键值对后加逗号,为下一次添加做准备

          }#最后的花括号应该与最后一个键值对的键相对齐

6.3 遍历字典
6.3.1 遍历所有键值对

 eg:

    for name,color in favorite.color.items()

6.3.2 遍历所有键

eg:

    for name in favorite.keys()

6.3.3 遍历所有值

eg:

    for color in favorite.values()

6.3.4 按顺序遍历所有键/值

eg:

    for name in sorted(favorite.keys())

set():调用它可以避免重复 eg:

                                                        for name in set(favorite_color.values())

添加用add(),删除用remove() eg:s.add(4)                                         

set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作

eg:            s1 & s2

                  s1 | s2

6.4 嵌套
6.4.1 字典列表

eg:

alien_0={'color':'green','speed':'fast'}
alien_1={'color':'yellow','speed':'mideum'}
alien_2={'color':'grey','speed':'slow'}

aliens=[alien_0,alien_1,alien_2]
for alien in aliens[:]:
    print(alien)
print('\n')

结果:
{'color': 'green', 'speed': 'fast'}
{'color': 'yellow', 'speed': 'mideum'}
{'color': 'grey', 'speed': 'slow'}

每个外星人都是独立的,可以独立的修改每个信息

eg_2:

#创建30个信息相同的外星人
aliens=[]

for alien_number in range(30):
    new_alien={'color':'green','points':5}
    aliens.append(new_alien)

for alien in aliens:
    print(alien)
print('...')

print('\n'+'Total number of aliens:'+str(len(aliens)))

结果:
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
...

Total number of aliens:30
6.4.2 在字典中存储列表

eg:

 
 
pizza={
    'crust':'thick',
    'toppings':['mushrooms','extra cheese'],
    }

print("You ordered a"+pizza['crust']+'-crust piazza'+
      "with the following toppings:")

for topping in pizza['toppings']:
    print("\t"+topping)


结果:
You ordered athick-crust piazzawith the following toppings:
    mushrooms
    extra cheese
6.4.3 在字典中存储字典

eg:

#字典中嵌套字典
users={
    'aeinstein':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton'
        },#注意此分号位置
    'mcurie':{
        'first':'marie',
        'last':'curie',
        'location':'paris',
        },#注意此分号位置
}
for username,user_info in users.items():
    print("\nusersname:"+username)
    full_name=user_info['first']+' '+user_info['last']
    location=user_info['location']


猜你喜欢

转载自blog.csdn.net/weixin_42404145/article/details/80613100