Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python打印少于列表中最后一个数字的所有数字_Python - Fatal编程技术网

用python打印少于列表中最后一个数字的所有数字

用python打印少于列表中最后一个数字的所有数字,python,Python,如何仅打印少于列表中最后一个数字的数字 我正在尝试使用python打印列表中小于最后一个数字的所有数字。该列表基于用户输入。用户输入的示例编号为: 5 40 50 160 300 75 100 (the last number) 我不想打印第一个或最后一个数字。第一个数字列出了列表中要检查的数字。我的代码只提供列表中的当前数字。我不知道如何只得到少于列表中最后一个数字的数字。我不想使用函数或数组。这需要是for/while/else/if/range或该领域中的其他内容 lst = [] #t

如何仅打印少于列表中最后一个数字的数字

我正在尝试使用python打印列表中小于最后一个数字的所有数字。该列表基于用户输入。用户输入的示例编号为:

5
40
50
160
300
75
100 (the last number)
我不想打印第一个或最后一个数字。第一个数字列出了列表中要检查的数字。我的代码只提供列表中的当前数字。我不知道如何只得到少于列表中最后一个数字的数字。我不想使用函数或数组。这需要是for/while/else/if/range或该领域中的其他内容

lst = [] #the list 
n = int(input()) #user input
  
for i in range(-1, n):
    ele = int(input())
    lst.append(ele) # adding the element'

print(*lst, sep = "\n")
我不明白您希望用户输入在其中起什么作用,但是如果您运行一个列表,它会将最后一个数字与列表中不包括第一个数字的所有数字进行核对。

如果您有一个列表:

List=[5, 40, 50, 160, 300, 75, 100]
列表[-1]是100

要打印列表中最后一个元素中小于100的所有元素,请执行以下操作:

n=int(input("Limit: "))) # Limiting number of comparisons with user input
smaller_numbers=[] # Creating a new list to store all the smaller values
for i in List[:n]: # Looping through the list to compare every element
    if i<List[-1]: # Seeing if the number is smaller than the last element of the list
        smaller_numbers.append(i) # if True the number will be appended to the new list

print(smaller_numbers)

我可能倾向于对项目进行排序,因为这样,当您可能点击较大的项目时,您可以停止检查。我可能会将列表排序为一个新的列表,并尝试检查效率。

您能具体说明您的问题是什么吗?用于rangen中的I,并了解您不使用Hello,TKO有什么特殊原因吗!一些帮助您解决问题的提示-您可以使用lst[-1]获取列表中的最后一个元素,使用lst[0]获取第一个元素。如果要跳过列表中的第一个元素,只需在lst[1:].1中使用for i。读。2.将n个以上的数字读入一个列表。3.读最后一个数字。使用列表理解仅打印列表中小于最后一个数字的数字,或使用另一个带有if的for循环。感谢大家的帮助!!!!非常感谢!你的回答让我明白了我错过了什么。我不知道你可以创建一个列表来存储另一个列表中的号码。还有很多学习要做。非常有用。谢谢@TKO没问题!:不过,下次再清楚一点,很难理解你想要什么。
n=int(input("Limit: "))) # Limiting number of comparisons with user input
smaller_numbers=[] # Creating a new list to store all the smaller values
for i in List[:n]: # Looping through the list to compare every element
    if i<List[-1]: # Seeing if the number is smaller than the last element of the list
        smaller_numbers.append(i) # if True the number will be appended to the new list

print(smaller_numbers)
[5, 40, 50, 75]