列表是:
result = [("Imelda Thomas",74),("Craig Parr",90),("Eric Salisbury",58),("Laurence Mann",35),("Bill Walford",82),("David Harold",27),("Pamela Langley",43),("Sarah Boat",39),("Rachel Matthews",62),("Michaela Cunningham",69)]
我想要打印:
Imelda Thomas 74
Craig Parr 90
Eric Salisbury 58
Laurence Mann 35
Bill Walford 82
David Harold 27
Pamela Langley 43
Sarah Boat 39
Rachel Matthews 62
Michaela Cunningham 69
但另一个问题是,允许它以这种方式打印的代码需要允许以任何方式更改列表。变短,名字改变
答案 0 :(得分:1)
最基本的方法是:
for name, grade in result:
print name, grade
答案 1 :(得分:0)
如果你想编辑列表中的名字和等级,你应该使用列表而不是元组中的元组:
result = [["Imelda Thomas", 74], ["Craig Parr", 90], ["Eric Salisbury", 58], ["Laurence Mann", 35], ...]
(方括号而不是括号)
现在您可以更改列表元素,例如,更改Thomas的成绩:
result[0][1] = 85
答案 2 :(得分:0)
如果你想要一些奇特的外观,那么这是另一个答案
In [45]: from tabulate import tabulate
In [46]: result = [("Imelda Thomas",74),("Craig Parr",90),("Eric Salisbury",58),("Laurence Mann",35),("Bill Walford",82),("David Harold",27),("Pamela Langley",43),("Sarah Boat",39),("Rachel Matthews",62),("Michaela Cunningham",69)]
In [47]: print tabulate(result)
------------------- --
Imelda Thomas 74
Craig Parr 90
Eric Salisbury 58
Laurence Mann 35
Bill Walford 82
David Harold 27
Pamela Langley 43
Sarah Boat 39
Rachel Matthews 62
Michaela Cunningham 69
------------------- --