从用户输入中检查列表中的重复项

时间:2018-06-01 19:56:07

标签: 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 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

4 个答案:

答案 0 :(得分:2)

更改此行:

for numbers in a:

要:

for i, numbers in enumerate(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

答案 1 :(得分:2)

如果您觉得有点想象,可以将一些循环转换为列表推导:

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""格式字符串附带PEP-498的Python 3.6(不知道:o /) - 所以对于早期的3.x Python必须使用< / p>

print( *["Number {} is located in the list and the position is: {}".format(n,q) for 
                 q in foundAt], sep="\n")

答案 2 :(得分:1)

您可以使用a = np.array([random.randint(1,20) for i in range(random.randint(8,30))]) 来简化此代码以删除循环。

np.where

然后,您可以使用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)

然后,您可以处理用户是否匹配答案:

{{1}}

答案 3 :(得分:0)

num =20

numlist = [2, 4, 8, 9, 10, 10, 16, 19, 20, 20]

for each in numlist:
    if num is each:
        print num
        print [i for i, x in enumerate(numlist) if x == num]