【问题标题】:Checking duplicates in list from user input从用户输入检查列表中的重复项
【发布时间】:2018-06-01 19:56:06
【问题描述】:

所以我编写了一个代码,它生成一个随机值的随机列表。然后询问用户用户正在寻找什么号码,如果它在列表中,它会告诉用户该号码在列表中的哪个位置。

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

【问题讨论】:

  • a = random.choices(range(1, 20), k = random.randint(8, 30)) 可能比您的列表组合更好。

标签: python python-3.x list input


【解决方案1】:

改变这一行:

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

【讨论】:

  • 感谢它完美运行,这么小的东西却有如此大的不同
  • 很乐意提供帮助,在这里使用enumerate,您可以在 O(n) 时间内检查所有索引,而不是使用仅找到第一个匹配项的 arr.index
【解决方案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"" 格式字符串随附于 Python 3.6 的 PEP-498(不知道 :o/ ) - 所以对于早期的 3.x,Python 必须使用

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

【讨论】:

  • @chrisz PEP498 已添加 :)
【解决方案3】:

您可以使用numpy 简化此代码以删除您的循环。

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)

【讨论】:

    【解决方案4】:
    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]
    

    【讨论】:

    • 这是一个 python 2.x 的答案——这个问题被标记为python3——你并没有完全产生想要的输出;)
    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2018-02-12
    • 1970-01-01
    • 2017-03-06
    • 2020-04-30
    相关资源
    最近更新 更多