如何在没有标点符号的情况下打印我的学生成绩列表?

时间:2015-05-10 16:41:56

标签: python

列表是:

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

但另一个问题是,允许它以这种方式打印的代码需要允许以任何方式更改列表。变短,名字改变

3 个答案:

答案 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
-------------------  --