浏览量 3828
2018/09/09 07:26
>>> NAME,AGE,PAY=range(3)
>>> bob = ['Bob Smith',42,30000]
>>> bob[NAME]
'Bob Smith'
>>> PAY,bob[PAY]
(2, 30000)
>>> bob = [['name','Bob Smith'],['age',42],['pay',10000]]
>>> sue = [['name','Sue Jones'],['age',45],['pay',30000]]
>>> people = [bob,sue]
>>> for person in people:
... print(person[0][1],person[2][1])
...
('Bob Smith', 10000)
('Sue Jones', 30000)
>>> [person[0][1] for person in people]
['Bob Smith', 'Sue Jones']
>>> for person in people:
... print(person[0][1].split()[-1])
... person[2][1]*=1.80
...
Smith
Jones
>>> for person in people: print(person[2])
...
['pay', 18000.0]
['pay', 54000.0]
>>> for person in people:
... for (name,value) in person:
... if name=='name':print (value)
...
Bob Smith
Sue Jones
>>> def field(record,lable):
... for (fname,fvalue) in record:
... if fname==lable:
... return fvalue
...
>>> field(bob,'name')
'Bob Smith'
>>> field(sue,'pay')
54000.0
>>> for rec in people:
... print (field(rec,'age'))
...
42
45
上一篇 搜索 下一篇