List 如何调用给定范围内的列表部分
我有一个问题,需要我接受用户输入并返回该值或高于该值的所有项目,最多返回100个 我问用户他想从我的一组数据中看到什么等级 因此,我将让用户输入一个等级,我将返回该等级或更高等级的所有记录 这是我到目前为止所拥有的 我从中提取的一小部分数据如下所示List 如何调用给定范围内的列表部分,list,python-3.x,call,range,threshold,List,Python 3.x,Call,Range,Threshold,我有一个问题,需要我接受用户输入并返回该值或高于该值的所有项目,最多返回100个 我问用户他想从我的一组数据中看到什么等级 因此,我将让用户输入一个等级,我将返回该等级或更高等级的所有记录 这是我到目前为止所拥有的 我从中提取的一小部分数据如下所示 ['Bud', 'Abbott', 51, 92.3] ['Don', 'Adams', 51, 90.4] ['Mary', 'Boyd', 52, 91.4] ['Jill', 'Carney', 53, 76.3] ['Hillary
['Bud', 'Abbott', 51, 92.3]
['Don', 'Adams', 51, 90.4]
['Mary', 'Boyd', 52, 91.4]
['Jill', 'Carney', 53, 76.3]
['Hillary', 'Clinton', 50, 82.1]
['Randy', 'Newman', 50, 41.2]
def Query ():
input("enter query type (g or s):")
#checks user's input and takes user to grades
if (operator == "g"):
print("You have chosen to query Grades")
GradeThreshold=input("enter the Grade threshold:")
#checks user's input and takes user to section
elif (operator == "s"):
print("You have chosen to query Section")
SectionNumber=input("enter the section:")
elif (operator != "g") and (operator != "s"):
print("Invalid entry. Please re-enter the operation from above.")
return()
到目前为止,我的代码只是一些if和elif语句,用于确保用户输入正确的函数。
此函数将起作用,因此如果用户输入字母g,程序将要求设置等级阈值,然后返回该等级及以上的任何数据行
例如,如果我是用户,我输入g,然后输入90
我只能收回这三行
['Bud', 'Abbott', 51, 92.3]
['Don', 'Adams', 51, 90.4]
['Mary', 'Boyd', 52, 91.4]
此外,如果用户输入字母S,它将查找该部分的记录,并与该部分中的所有学生一起返回
因此,如果用户输入s,然后输入50,程序将返回
['Hillary', 'Clinton', 50, 82.1]
['Randy', 'Newman', 50, 41.2]
到目前为止,我编写的代码如下所示
['Bud', 'Abbott', 51, 92.3]
['Don', 'Adams', 51, 90.4]
['Mary', 'Boyd', 52, 91.4]
['Jill', 'Carney', 53, 76.3]
['Hillary', 'Clinton', 50, 82.1]
['Randy', 'Newman', 50, 41.2]
def Query ():
input("enter query type (g or s):")
#checks user's input and takes user to grades
if (operator == "g"):
print("You have chosen to query Grades")
GradeThreshold=input("enter the Grade threshold:")
#checks user's input and takes user to section
elif (operator == "s"):
print("You have chosen to query Section")
SectionNumber=input("enter the section:")
elif (operator != "g") and (operator != "s"):
print("Invalid entry. Please re-enter the operation from above.")
return()
我很困惑,我将如何接受用户的输入,并让它从我上面的数据列表中选择等级范围或部门编号。请帮助我。您需要反复查看这些项目。例如:
items = [['Bud', 'Abbott', 51, 92.3],
['Don', 'Adams', 51, 90.4],
['Mary', 'Boyd', 52, 91.4],
['Jill', 'Carney', 53, 76.3],
['Hillary', 'Clinton', 50, 82.1],
['Randy', 'Newman', 50, 41.2]]
for item in items:
print(item)
这将按顺序打印所有项目。要从项中获取值,您需要使用括号通过索引访问它:
for item in items:
print item[2] # Prints the 3rd element in item (because indexes start at 0)
或在迭代时解压缩项目:
for first_name, last_name, some_integer, grade in items:
print('Name:', first_name, last_name)
print('Grade:', grade)
第二种解决方案在每个项目中只有很少的项目时被视为更为惯用,它是首选方案,因为它更清楚项目的组成
假设这是作业,我想您可以从这里完成。您可以从正确缩进代码样本开始。正如您所知,缩进是Python语法的一部分。