python---数据类型(元组、字典)

元组

定义元组 tuple

列表适合于存储在程序运行期间可能变化的数据集,列表可修改。

元组用于创建一系列不可修改的元素,python不能修改的值称为不可变的,不可变的列表称为元组。

使用圆括号来标识:

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
dimensions[1] = 100 #报错

遍历元组

dimensions = (200, 50)

for dimension in dimensions:
    print(dimension)
修改元组变量

元组的元素不可修改,但是可以给存储元组的变量赋值:

dimensions = (200, 50)
print("Original dimensions:")

for dimension in dimensions:
    print(dimension)

dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

缩进:

PEP8建议每级缩进使用4个空格。千万不要制表符和空格混用

行长:

建议每行不超过80个字符

PEP8还建议注释行长不超过72字符

空行:

空行不会影响代码的运行,但会影响代码的可读性。

字典

定义

python中,字典是一系列键值对。每个键都与一个值相关联,可以用键来访问关联的值。

与键关联的可以是数字、字符串、列表乃至字典。任何python对象都可以用做字典的值。

python中字典用{}中的一系列键值对表示,键值用冒号分隔,你想存储多少个键值对都可以。最简单的字典只有一个键值对:

dict_0 = {'color':'green', 'size':20}
dict_1 = {'color':'green
访问字典中的值
alien_0 = {'color':'green'}
print(alien_0['color'])

alien_0 = {'color':'green', 'points':5}
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)

python不关心键值对的添加顺序,只关心键值之间的关联关系。

创建空的字典
mydict = {}

mydict['name'] = 'zhangsan'
mydict['age'] = 25

print(mydict)
修改字典的值
mydict = {}

mydict['name'] = 'zhangsan'
mydict['age'] = 25

print(mydict)

mydict['name'] = 'lisi'
print(mydict)
删除键值对

使用del语句将相应的键值对彻底删除。使用del语句时,必须指定字典名和要删除的键:

mydict = {}

mydict['name'] = 'zhangsan'
mydict['age'] = 25

print(mydict)

del mydict['name']
print(mydict)
由类似对象组成的字典

注意缩进,养成好习惯:

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

print("Sarah's favorite language is " +
      favorite_languages['sarah'].title() +
      ".")
遍历字典
遍历所有的键值对
favorite_language = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'java'

}
for key,value in favorite_language.items():
    print(key+" likes "+value)
遍历字典中所有的键
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'java'
    }

for name in favorite_languages.keys():
    print(name)

print("=============")

for name in favorite_languages:
    print(name)

遍历字典时,会默认遍历所有的键,因此for name in favorite_languages.keys()可以替换为for name in favorite_languages:,输出不变。

还可以使用keys()确定某个名称是否在字典的键中:

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

if 'zhangsan' not in favorite_languages.keys():
    print("zhangsan are you ok?")

if "sarah" in favorite_languages.keys():
    print("hello sarah")

方法keys()实际上返回一个列表,其中包含字典中所有键。

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

for name in sorted(favorite_languages.keys()):
    print(name.title())
遍历字典的所有值

values()方法返回一个值列表,不包含任何键:

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

for language in favorite_languages.values():
    print(language)

如果要去重,可以使用集合set,类似于列表,元素不重复:

favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'java',
    'zhangsan':'ruby'
    }

for language in set(favorite_languages.values()):
    print(language)
字典列表
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
    print(alien)
字典中可以存储列表
favorite_languages = {
    'jen': ['python', 'ruby'],
    'sarah': ['c'],
    'edward': ['ruby', 'go'],
    'phil': ['python', 'haskell'],
    }
for name, languages in favorite_languages.items():
    print("\n" + name.title() + "'s favorite languages are:")
    for language in languages:
        print("\t" + language.title())
字典中存储字典
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())

你可能感兴趣的:(python,python,开发语言)