Python 从用户输入检查列表中的重复项
所以我写了一段代码,生成一个随机列表,其中包含随机数目的值。然后询问用户要查找的号码,如果该号码在列表中,它将告诉用户该号码在列表中的位置Python 从用户输入检查列表中的重复项,python,python-3.x,list,input,Python,Python 3.x,List,Input,所以我写了一段代码,生成一个随机列表,其中包含随机数目的值。然后询问用户要查找的号码,如果该号码在列表中,它将告诉用户该号码在列表中的位置 import random a = [random.randint(1, 20) for i in range(random.randint(8, 30))] a.sort() print(a) def askUser(): n = input("What number are you looking for?") while no
import random
a = [random.randint(1, 20) for i in range(random.randint(8, 30))]
a.sort()
print(a)
def askUser():
n = input("What number are you looking for?")
while not n.isdigit():
n = input("What number are you looking for?")
n = int(n)
s = 0
for numbers in a:
if numbers == n:
s += 1
print("Number", n, "is located in the list and the position is:", (a.index(n)+1))
# Something here to skip this index next time it goes through the loop
else:
pass
if s == 0:
print("Your number could not be found")
askUser()
我想添加一些东西,将跳过它第一次找到的索引,然后寻找重复的索引(如果有)
当前结果
[2, 4, 8, 9, 10, 10, 16, 19, 20, 20]
What number are you looking for?20
Number 20 is located in the list and the position is: 9
Number 20 is located in the list and the position is: 9
期望结果
[2, 4, 8, 9, 10, 10, 16, 19, 20, 20]
What number are you looking for?20
Number 20 is located in the list and the position is: 9
Number 20 is located in the list and the position is: 10
更改此行:
for numbers in a:
致:
然后更改打印索引的方式:
print("Number", n, "is located in the list and the position is:", (i+1))
样本输出:
[1, 2, 2, 5, 5, 5, 6, 7, 8, 8, 8, 10, 10, 10, 10, 10, 11, 11, 16, 17, 17, 19, 19]
What number are you looking for? 8
Number 8 is located in the list and the position is: 9
Number 8 is located in the list and the position is: 10
Number 8 is located in the list and the position is: 11
您可以使用
numpy
简化此代码以删除循环
a = np.array([random.randint(1,20) for i in range(random.randint(8,30))])
然后,您可以使用np。其中
确定用户是否在随机值数组a
中选择了一个值:
idx_selections = np.where(a == n)[0]
然后,您可以处理用户是否匹配答案:
if len(idx_selections) == 0:
print("Your number could not be found")
else:
for i in idx_selections:
print("Number", n, "is located in the list and the position is:", i)
如果您喜欢,可以将一些循环转换为列表理解:
def askUser():
n = input("What number are you looking for?")
while not n.isdigit():
n = input("What number are you looking for?")
n = int(n)
# get a list of all indexes that match the number
foundAt = [p+1 for p,num in enumerate(a) if num == n]
if foundAt:
# print text by creating a list of texts to print and decompose them
# printing with a seperator of linefeed
print( *[f"Number {n} is located in the list and the position is: {q}" for
q in foundAt], sep="\n")
else:
print("Your number could not be found")
编辑:正如Chrisz指出的那样,f”“
格式字符串是为Python3.6提供的(不知道:o/)-因此对于早期的3.x,Python必须使用
print( *["Number {} is located in the list and the position is: {}".format(n,q) for
q in foundAt], sep="\n")
感谢它工作得很完美,这么小的一件事却产生了这么大的差异很高兴能提供帮助,通过使用这里的
enumerate
,您可以在O(n)时间内检查所有索引,而不是使用arr.index
,它只找到第一个匹配项。a=random.choices(范围(1,20),k=random.randint(8,30))
可能比您的列表组件更好。这是一个python 2.x答案-问题带有标记-并且您没有完全生成所需的输出;)@chrisz PEP498新增:)
def askUser():
n = input("What number are you looking for?")
while not n.isdigit():
n = input("What number are you looking for?")
n = int(n)
# get a list of all indexes that match the number
foundAt = [p+1 for p,num in enumerate(a) if num == n]
if foundAt:
# print text by creating a list of texts to print and decompose them
# printing with a seperator of linefeed
print( *[f"Number {n} is located in the list and the position is: {q}" for
q in foundAt], sep="\n")
else:
print("Your number could not be found")
print( *["Number {} is located in the list and the position is: {}".format(n,q) for
q in foundAt], sep="\n")