在python中使用record, for ** in **以及定义函数

来源:互联网 发布:2016nba总决赛数据统计 编辑:程序博客网 时间:2024/05/23 19:37
#! /usr/bin/env python
#coding=utf-8
bob = [['name', 'Bob Smith'], ['age', 42], ['pay', 10000]]
sue = [['name', 'Sue Jones'], ['age', 45], ['pay', 20000]]
people = [bob, sue]
for person in people:
 print(person[0][1], person[2][1]) # name, pay

print([person[0][1] for person in people])

for person in people:
 print(person[0][1].split()[-1]) # get last names
 person[2][1] *= 1.10 # give a 10% raise


for person in people:
 print(person[2])


for person in people:
 for (name, value) in person:
  if name == 'name': print(value) # find a specific field

print("\n")

##定义函数,  record表示记录
def field(record, label):
 for (fname, fvalue) in record:
  if fname == label: # find any field by name
   return fvalue

print(field(bob, 'name'))
print(field(sue, 'pay'))

for rec in people:
 print(field(rec, 'age')) # print all ages