当你要寻找的某个特定值不在列表中时,该如何表达

2024-05-12 20:54:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为学校做这个作业,几乎都完成了,但我错过了一个非常小但非常关键的部分。你知道吗

因此,完整的作业是让用户输入学生在列表中得到的分数的一些值,然后用户必须能够看到某个特定科目的平均值或所有学生在某个科目上得到的所有分数的打印版本。你知道吗

我的问题是,当用户选择查看某个主题的avg/all值时,我应该如何告诉用户没有任何特定主题的值。 我将在这里展示一个已经包含一些值的列表,这样就不需要经历将它们放入的整个过程

cijfers = [ [ 12345, 'wiskunde', 8.9], [ 12345,'elnet', 4.0], [12345, 'python', 8.9], [98761, 'wiskunde', 6.5], [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'wiskunde', 5],[20945, 'elnet', 6.9], [20945, 'python', 4.5], [65489, 'wiskunde', 3.4], [65489, 'elnet', 6.7], [65489, 'python', 10]]

第一个是学生编号,第二个是科目,第三个是学生的成绩。你知道吗

所以如果我把这个写下来就是给我所有的“wiskunde”科目的分数

cijfers = [ [ 12345, 'wiskunde', 8.9], [ 12345,'elnet', 4.0], [12345, 'python', 8.9], [98761, 'wiskunde', 6.5], [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'wiskunde', 5],[20945, 'elnet', 6.9], [20945, 'python', 4.5], [65489, 'wiskunde', 3.4], [65489, 'elnet', 6.7], [65489, 'python', 10]]

a = 0
print ('     Tentamencijfers voor: ','Wiskunde', '\n', '========================================')
print ('         studenten# | cijfer')
while (a<len(cijfers)):
    if (cijfers [a][1] == 'wiskunde'):
        print ('          ',cijfers[a][0], '     ',cijfers[a][2])
    a = a + 1

它的输出如下:

    Tentamencijfers voor:  Wiskunde             #translates to exam grades for: Math
 ========================================
         studenten# | cijfer
           12345       8.9
           98761       6.5
           20945       5
           65489       3.4

它应该是这样做的,但是假设“wiskunde”没有任何值,那么列表将如下所示:

cijfers = [  [ 12345,'elnet', 4.0], [12345, 'python', 8.9],  [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'elnet', 6.9], [20945, 'python', 4.5],  [65489, 'elnet', 6.7], [65489, 'python', 10]]

它会给我一个输出:

Tentamencijfers voor:  Wiskunde #translates to exam grades for: Math
 ========================================
         studenten# | cijfer

我知道它只显示了一点点,因为我在“while”之前写了“print”函数,但这不是我的问题。你知道吗

所以我的问题是,我怎样才能让它给我一个简单的“这个主题没有价值观”?你知道吗


Tags: 用户主题列表学生分数print科目voor
3条回答

嗯,我一点也不确定这是不是你想要的,但很管用

print ('     Tentamencijfers voor: ','Wiskunde', '\n', '========================================')

lines_studient = []
found = False
for entry in cijfers:
    if (entry[1] == 'wiskunde'):
        lines_studient.append('          '+str(entry[0])+ '     '+str(entry[2]))
        found = True

if not found:
    print ('         None studients')
else:
    print ('         studenten# | cijfer')
    for line in lines_studient:
        print(line)

它使用两个for循环:第一个用于查找是否存在一个元素,第二个用于打印行。你知道吗

为了严格回答您的问题,我将添加一个计数器并按以下方式更改循环:

count = 0
for i in cijfers:
    if (cijfers i[1] == 'wiskunde'):
        count += 1 # Same as count = count + 1
        print ('          ',i[0], '     ',i[2])

#Out of the loop:
if count == 0:
    print "There's no values put in for this particular subject"

作为数据结构,我认为字典将更好地满足您的需要,使用学号作为键;check them out!

试试这个。这里不需要使用两个for循环。您可以将理解与任何()方法一起使用:

cijfers = [ [ 12345, 'wiskunde', 8.9], [ 12345,'elnet', 4.0], [12345, 'python', 8.9], [98761, 'wiskunde', 6.5], [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'wiskunde', 5],[20945, 'elnet', 6.9], [20945, 'python', 4.5], [65489, 'wiskunde', 3.4], [65489, 'elnet', 6.7], [65489, 'python', 10]]

a = 0
print '     Tentamencijfers voor: ','Wiskunde', '\n', '========================================'
print '         studenten# | cijfer'

if any("wiskunde" in sublist for sublist in cijfers):
    while (a<len(cijfers)):
        if (cijfers [a][1] == 'wiskunde'):
            print '          ',cijfers[a][0], '     ',cijfers[a][2]
        a = a + 1
else:
    print "There's no values put in for this particular subject"

相关问题 更多 >