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

时间:2022-03-22 07:38:12

The list is:

清单是:

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)]

I want it to print:

我想要它打印:

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

But the another problem is that this code that allows it to print in this way needs to allow the list to be changed in anyway E.G. get shorter, names changing

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

3 个解决方案

#1


The most basic way is:

最基本的方法是:

for name, grade in result:
    print name, grade 

#2


If you want to edit the names and grades in the list you should use lists instead of tuples inside it:

如果要编辑列表中的名称和等级,则应使用列表而不是其中的元组:

result = [["Imelda Thomas", 74], ["Craig Parr", 90], ["Eric Salisbury", 58], ["Laurence Mann", 35], ...]

(square brackets instead of parentheses)

(方括号而不是括号)

Now you can make changes to the list elements, for example, change Thomas's grade:

现在您可以对列表元素进行更改,例如,更改Thomas的成绩:

result[0][1] = 85

#3


If you want some fancy look then it's an alternate answer

如果你想要一些奇特的外观,那么这是另一个答案

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

#1


The most basic way is:

最基本的方法是:

for name, grade in result:
    print name, grade 

#2


If you want to edit the names and grades in the list you should use lists instead of tuples inside it:

如果要编辑列表中的名称和等级,则应使用列表而不是其中的元组:

result = [["Imelda Thomas", 74], ["Craig Parr", 90], ["Eric Salisbury", 58], ["Laurence Mann", 35], ...]

(square brackets instead of parentheses)

(方括号而不是括号)

Now you can make changes to the list elements, for example, change Thomas's grade:

现在您可以对列表元素进行更改,例如,更改Thomas的成绩:

result[0][1] = 85

#3


If you want some fancy look then it's an alternate answer

如果你想要一些奇特的外观,那么这是另一个答案

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