06_python_字典


日期:2017-12-30 作者:秋的懵懂


# coding = utf-8

# ***********************************************************
# @brief    字典
# @author   魏文应
# @date     2017-12-27
# ***********************************************************




# ---------------------------------------------------------
# 一个简单字典
print('_______________________________________________')
print("一个简单字典:")

alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
print(alien_0['points'])
# 修改字典元素值
alien_0['color'] = 'yellow'
print(alien_0['color'])

print('_______________________________________________')
# ---------------------------------------------------------



# ---------------------------------------------------------
# 删除键值对
print('\n\n')
print('_______________________________________________')
print("删除键值对:")

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

print('_______________________________________________')
# ---------------------------------------------------------



# ---------------------------------------------------------
# 遍历字典
print('\n\n')
print('_______________________________________________')
print("遍历字典:")

user_0 = {
    'username': 'wwy',
    'last': 'wei',
    'first': 'wenying',
    }
# 方法items()返回键-值对列表
for key, value in user_0.items():
    print('\nKey: ' + key)
    print('Value: ' + value)
    
# Python不关心键—值对的存储顺序, 而只跟踪键和值之间的关联关系
favorite_language = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }
for name, language in favorite_language.items():
    print(name.title() + "'s favorite language is " + 
        language.title() + '.')

print('_______________________________________________')
# ---------------------------------------------------------




# ---------------------------------------------------------
# 遍历字典所有键
print('\n\n')
print('_______________________________________________')
print("遍历字典所有键:")

favorite_language = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }
for name in favorite_language.keys():
    print(name.title())
    
# 默认遍历所有键
for name in favorite_language:
    print(name.title())

print('_______________________________________________')
# ---------------------------------------------------------




# ---------------------------------------------------------
# 按顺序遍历字典
print('\n\n')
print('_______________________________________________')
print("按顺序遍历字典:")

favorite_language = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    }

print()
# 遍历键
for name in sorted(favorite_language.keys()):
    print(name.title() + ', thank you for talling the poll')
    
# 遍历值
print('The following languages have been mentioned:')
for language in favorite_language.values():
    print(language.title())

# set()集合,集合中元素是独一无二的
print('The following languages have been mentioned:')
for language in set(favorite_language.values()):
    print(language.title())

print('_______________________________________________')
# ---------------------------------------------------------



# ---------------------------------------------------------
# 列表中存储字典
print('\n\n')
print('_______________________________________________')
print("列表中存储字典:")

alien_0 = {'color': 'red', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'green', 'points': 15}
aliens = [alien_0, alien_1, alien_2]

for alien in aliens:
    print(alien)

# 创建存储列表
aliens = []
# 创建30个字典
for alien_number in range(0, 30):
    new_alien = {'color': 'green', 'points': 15, 'speed': 'slow'}
    aliens.append(new_alien)
# 修改列表前面三个元素
for alien in aliens[0:3]:
    if alien['color'] == 'green':
        alien['color'] = 'yellow'
        alien['points'] = 10
        alien['speed'] = 'medium'
# 显示列表前面5个字典
for alien in aliens[0:5]:
    print(alien)

print('_______________________________________________')
# ---------------------------------------------------------



# ---------------------------------------------------------
# 字典中存储列表
print('\n\n')
print('_______________________________________________')
print("字典中存储列表:")

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

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

for topping in pizza['toppings']:
    print('\t' + topping)
# 访问内部元素
print(pizza['toppings'][1])

print('_______________________________________________')
# ---------------------------------------------------------



# ---------------------------------------------------------
# 字典中存储字典
print('\n\n')
print('_______________________________________________')
print("字典中存储字典:")

user = {
    '小和尚': {
        '姓': '陈',
        '名': '长生',
        '地址': '格力一期',
        },
    
    '小尼姑': {
        '姓': '徐',
        '名': '有容',
        '地址': '格力二期',
        },
    }
for username, user_info in user.items():
    print('\nUsername: ' + username)
    full_name = user_info['姓'] + ' ' + user_info['名']
    location = user_info['地址']
    print('\tFullname: ' + full_name.title())
    print('\tLocation: ' + location.title())

print('_______________________________________________')
# ---------------------------------------------------------


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容