def check(inp, chk):
if chk in inp:
print("Yes")
else:
print("No")
inp = [int(x) for x in input().split()]
chk = input("Enter a character to check: ")
check(inp, chk)
def check_repeatition(num, counter):
count = counter.get(num) # returns `None` if `num` not found
if count == 1:
print('Non repeated number')
elif count == None:
print('Number not found')
else:
print('Repeated Number')
运行示例:
>>> from collections import Counter
# v v repeated
>>> my_num_str = '1 2 3 4 5 6 1 7 8 9'
# Convert number string to `list` of `int` numbers
>>> num_counter = Counter(map(int, my_num_str.split()))
# Two occurrence of `1` in the string
>>> check_repeatition(1, num_counter)
Repeated Number
# One occurrence of `2` in the string
>>> check_repeatition(2, num_counter)
Non repeated number
# `0` not present in the string
>>> check_repeatition(0, num_counter)
Number not found
您可以通过使用^{} 来实现这一点,它返回一个dict
object
,其中包含迭代器中每个元素的计数。你知道吗下面是检查
Counter
对象重复的示例函数:运行示例:
代码的问题是您正在使用
in
运算符检查列表中是否存在数字。您的代码没有检查数字是否重复。此外,您还需要通过在python3.x中使用chk = int(chk)
作为input(...)
返回str
来将chk
类型转换为int
相关问题 更多 >
编程相关推荐