python field

葫芦的运维日志

下一篇 搜索 上一篇

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

 

葫芦的运维日志

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2019 王梓 赞助联系方式 陕ICP备17005322号