#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/7/5 14:53
# @Author  : Ting
# @Site    : 
# @File    : Chapter 6.py
# @Software: PyCharm

#6.1 一个简单的字典
alien_0={'color':'green','points':5}

print(alien_0['color'])
print(alien_0['points'])

#6.2 使用字典
#6.2.1 访问字典中的值
alien_0={'point':5}
new_points=alien_0['point']
print('You have earned '+str(new_points)+' points!')

#6.2.2 添加键-值对
#从从python3.7开始，字典按照插入顺序，实现了有序。
# 修改一个已存在的key的值，不影响顺序。
# 如果删了一个key后再添加该key，该key会被添加至末尾。标准json库的dump(s)/load(s)也是有序的
alien_0={'color':'green','point':5}
print(alien_0)

alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)

#6.2.3 先创建一个空字典，再分行添加各键-值对（有时候必须这样做）
alien_0 = {}
alien_0['color']='green'
alien_0['points'] = 5
print(alien_0)

#6.2.4 修改字典中的值
#修改颜色为黄色
alien_0['color'] = 'yellow'
print('\n')
print(alien_0)

#先明确一点,下面结果是5
x=2
x=x+3
print(x)

#跟踪以不同速度移动的外星人的位置
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
elif alien_0['speed'] == 'fast':
    x_increment = 3

alien_0['x_position'] = alien_0['x_position'] + x_increment
print('New x_position: '+str(alien_0['x_position']))

#6.2.5 删除键-值对
alien_0 = {'color':'green','points':5}
print(alien_0)

del alien_0['points']
print(alien_0)

#由类似对象组成的字典
favorite_language = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python',
    }

print("Sarah's favorite language is "+
      favorite_language['sarah'].title()+
      ".")

#6.3 遍历字典
#6.3.1遍历所有的键-值对
information = {
    'first_name':'Julian',
    'last_name':'Aaron',
    'city':'New York'
    }

for key,value in information.items():
    print(key)
    print(value)

for key,value in information.items():
    print('\nkey'+':'+key)
    print('value'+':'+value)

print('\n')

for key,value in information.items():
    if key == 'city':
        print('His '+ key +' is at '+ value +'.')
    else:
        print('His '+ key +' is '+ value + '.')

for name,language in favorite_language.items():
    print('Hi,'+name.title() + 'I see your favorite_language is '+language+'.')

#6.3.2 遍历字典中所有键
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())

firends = ['phil','sarah']

#方法1（用键）P
for name in favorite_language.keys():
    print(name.title())

    if name in firends:
        print('Hi '+ name.title() +
              ', I see your favorite language is '+
              favorite_language[name].title()+'!')

#方法2 （用键-值对）
for name,language in favorite_language.items():
    print(name.title())

    if name in firends:
        print('Hi '+ name.title() +
              ', I see your favorite language is '+
              language.title()+'!')

#6.3.3 按顺序遍历字典中的所有键
favorite_language = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python',
    }

for name in sorted(favorite_language.keys()):
    print(name.title())

print('\n')
#6.3.4 遍历字典中的所有值
favorite_language = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python',
    }
print('The following languages have been mentioned:')
for language in favorite_language.values():
    print(language.title())

#剔除重复项，并使用这些元素创建一个集合
favorite_language = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python',
    }
print('\nThe following languages have been mentioned:')
for language in set(favorite_langauage.values()):
    print(language.title())

