如何在python中输入的两个运行时数字之间打印列表中的数字

如何在python中输入的两个运行时数字之间打印列表中的数字,python,Python,如何在运行时之间打印列表中的数字在python中输入了两个数字 例如: 答案是[8,23] lst = [23, 5, 7, 8, 90, 67, 90, 0] first = int(input('Enter Your Number:')) second = int(input('Enter Your Number:')) for i in range(0, len(lst)): if lst[i] == first: for x in range(0, len(ls

如何在运行时之间打印列表中的数字在python中输入了两个数字

例如:

答案是[8,23]

lst = [23, 5, 7, 8, 90, 67, 90, 0]
first = int(input('Enter Your Number:'))
second = int(input('Enter Your Number:'))

for i in range(0, len(lst)):
    if lst[i] == first:
        for x in range(0, len(lst)):
            if lst[x] == second:
                if x > i:
                    ans = lst[lst[i]:lst[x]]
                    print(ans)
                else:
                    ans2 = lst[lst[x]:lst[i]]
                    print(ans2)
这只是一个过滤器:

print(list(filter(lambda x: first < x < second, lst)))

由于实际的筛选操作是O(n),而排序是O(n lg n),因此我建议对可能较短的结果进行排序,而不是对原始列表进行排序。

首先迭代列表元素,而不是其索引。然后检查是否为
first>i>second
。到底是什么问题?不清楚你想做什么,也不清楚目前的结果是什么。见:。
print(list(filter(lambda x: first < x < second, lst)))
print([x for x in lst if first < x < second])
[x for x in sorted(lst) if first < x < second]
sorted(x for x in lst if first < x < second)
list(filter(lambda x: first < x < second, sorted(lst)))
sorted(filter(lambda x: first < x < second, sorted(lst)))