Python中的重复数字

-4 投票
3 回答
974 浏览
提问于 2025-04-18 14:03

写一个Python程序,找出所有相邻的重复数字。比如,如果输入是 1 3 3 4 5 5 6 6 6 2,程序应该输出 3 5 6。

这是我目前写的代码:

print("Take out repeating numbers")

def main():

    a = int(input("Enter a number: "))
    b = int(input("Enter a number: "))
    c = int(input("Enter a number: "))
    d = int(input("Enter a number: "))
    e = int(input("Enter a number: "))
    f = int(input("Enter a number: "))
    g = int(input("Enter a number: "))

    if a == a: 
        print(a)
    elif a == b:
        print(a)
    elif a == c:
        print(a)
    elif a == d:
        print(a)
    elif a == e:
        print(a)
    elif a == f:
        print(a)
    else:
        print(a)

    if b == a:
        print(b)
    elif b == b:
        print(b)
    elif b == c:
        print(b)
    elif b == d:
        print(b)
    elif b == e:
        print(b)
    elif b == f:
        print(b)
    else:
        print(b)

    if c == a:
        print(c)
    elif c == b:
        print(c)
    elif c == c:
        print(c)
    elif c == d:
        print(c)
    elif c == e:
        print(c)
    elif c == f:
        print(c)
    else:
        print(c)
        '''
    if d == a or d == b  or d == c or d == d or d == e or d == f or d == g:
        print(d)
    if e == a or e == b  or e == c or e == d or e == e or e == f or e == g:
        print(e)
    if f == a or f == b  or f == f or f == d or f == e or f == f or f == g:
        print(f)
    if g == a or g == b  or g == c or g == d or g == e or g == f or g == g:
        print(g)    
    '''

    print("The repeating numbers are: " , a, b, c, d, e, f, g,)

main()

它只是把所有输入的数字都打印出来。

3 个回答

0

我的回答可能来得有点晚。不过,对于这个问题,你可以通过在布尔值和while循环中使用if语句来获取相邻的重复数字,代码如下。

首先,把布尔值的状态设置为True。

在你的while语句中,如果输入的数字大于0,就继续执行if语句。在这里你需要检查状态是否为False,以及下一个数字是否和前一个数字相同。然后获取这个相邻的数字,但要确保在和其他相邻数字拼接之前把它转换成字符串。

count = 0
previous = 0
duplicate = ""

num = int(input("\nPlease enter a number or -1 to finish : "))
previous = num
status = True

while num >= 0.0:
    # determine the adjacent duplicates
    if not status and num == previous:
        duplicate = duplicate + " " + str(num)
        print(duplicate)

    status = False
    previous = num
    count = count + 1
    num = int(input("Please enter a number or -1 to finish : "))

if count > 0:
    print(duplicate)
else:
    print("Finish")

希望我的代码能对你有所帮助。其他人也可以随意批评和改进我的代码,以供其他人参考。

0

在编程中,有时候我们需要处理一些数据,而这些数据可能会有很多不同的格式。为了让程序能够理解这些数据,我们通常需要将它们转换成一种统一的格式。

比如说,如果我们有一个包含日期的字符串,像“2023-10-01”,我们可能需要把它转换成一个日期对象,这样程序才能更方便地进行日期的计算和比较。

在这个过程中,我们可能会用到一些函数或者库,这些工具可以帮助我们快速地完成数据格式的转换。通过这些工具,我们可以节省很多时间,避免手动处理每一个数据。

总之,数据格式转换是编程中一个很常见的任务,掌握它能够让我们的程序更加高效和灵活。

input_set = []
input_num = 0

while (input_num >= 0):

    input_num = int(input("Please enter a number or -1 to finish"))
    if (input_num < 0):
        break
    input_set.append(input_num)

print(input_set)

dup = 0
new_list = []
unique_list = []

for i in range(0,len(input_set)-1):
    if input_set[i]==input_set[i+1] or input_set[i] == input_set[i-1]:
        new_list.append(input_set[i])

[unique_list.append(item) for item in new_list if item not in unique_list]
print(unique_list)
2

试试这个一行的 列表推导式,它使用了来自 itertools 模块的 groupby() 函数:

import itertools as it

lst = [1, 3, 3, 4, 5, 5, 6, 6, 6, 2]

[k for k, v in it.groupby(lst) if len(list(v)) > 1]
=> [3, 5, 6]

撰写回答