重拾python Day 2

昨天加班到深夜,所以隔了一天。

基于列表表示数据

bob = [['name', 'Bob Smith'], ['age', 42], ['pay', 10000]]
sue = [['name', 'Sue Jones'], ['age', 45], ['pay', 20000]]
people = [bob, sue]
print(people)
for person in people:
    print(person[0][1], person[2][1])
print([person[0][1] for person in people])
for person in people:
    print(person[0][1].split()[-1])
    person[2][1] *= 1.10
for person in people: 
    print(person[2])
for person in people:
    for (name, value) in person:
        if name == 'name':
            print(value)


# 定义一个函数还实现
def field(record, label):
    for (fname, fvalue) in record:
        if fname == label:
            return fvalue


print(field(bob, 'name'))

运行结果:

[[['name', 'Bob Smith'], ['age', 42], ['pay', 10000]], [['name', 'Sue Jones'], ['age', 45], ['pay', 20000]]]
Bob Smith 10000
Sue Jones 20000
['Bob Smith', 'Sue Jones']
Smith
Jones
['pay', 11000.0]
['pay', 22000.0]
Bob Smith
Sue Jones
Bob Smith

使用字典

字典为内建对象

import pprint
# 使用字典表示记录
bob = {'name': 'Bob Smith', 'age': 42, 'pay': 30000, 'job': 'dev'}
sue = {'name': 'Sue Jones', 'age': 33, 'pay': 80000, 'job': 'pm'}
print(bob['name'], sue['age'])
# 其他建立字典的方法
bob2 = dict(name='Bob Smith', age=42)
print(bob2)
# 再换一种方法,一次一个字段的填写字典
bob3 = {}
bob3['name'] = 'Bob Three'
print(bob3)
# 使用Zip函数将name和value链接一起
names = ['name', 'age', 'pay']
values = ['Sue', 45, 20000]
print(list(zip(names, values)))
sue2 = dict(zip(names, values))
print(sue2)
# 初始化空字典
fields = ('name', 'age', 'job')
record = dict.fromkeys(fields, '?')
print(record)
# 使用字典
people = [bob, sue]
for person in people:
    print(person['name'], person['age'], sep=',')
names = [person['name'] for person in people]
print('name:', names)
print('list:', list(map((lambda x: x['name']), people)))
print(sum(person['pay'] for person in people))
# 还可以使用类似SQL一样的操作
print([rec['name'] for rec in people if rec['age'] >= 40])
print(3 ** 2)
# 嵌套结构
bob4 = {
    'name': {'first': 'Bob', 'last': 'Smith'},
    'age': 42,
    'job': ['software', 'writing'],
    'pay': (4000, 50000)
}
print(bob4['name'])
print(bob4['name']['last'])
print(bob4['pay'][1])
for job in bob4['job']:
    print(job)
bob4['job'].append('janitor')
print('new job:', bob4['job'])
db = {}
db['bob'] = bob
db['sue'] = sue
print(db['bob']['name'])
# 顶部引入了包
pprint.pprint(db)
# 访问所有对象,使用键索引
for key in db:
    print(key, '=>', db[key]['name'])
# 使用迭代字典的值的集合
for rec in db.values():
    print(rec['pay'])
x = [db[key]['name'] for key in db]
print(x)
y = [rec['name'] for rec in db.values()]
print(y)
db['tom'] = dict(name='Tom', age=50, job=None, pay=0)
print(db['tom'])
print("all keys", db.keys())
print('length of db', len(db))

运行结果

new job: ['software', 'writing', 'janitor']
Bob Smith
{'bob': {'age': 42, 'job': 'dev', 'name': 'Bob Smith', 'pay': 30000},
 'sue': {'age': 33, 'job': 'pm', 'name': 'Sue Jones', 'pay': 80000}}
bob => Bob Smith
sue => Sue Jones
30000
80000
['Bob Smith', 'Sue Jones']
['Bob Smith', 'Sue Jones']
{'name': 'Tom', 'age': 50, 'job': None, 'pay': 0}
all keys dict_keys(['bob', 'sue', 'tom'])
length of db 3

所有的这些操作其实都在操作内存,虽然感觉和SQL很像

你可能感兴趣的:(Python,学习笔记)