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

#! /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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值