这段代码如何优化:
def has_duplicates(nums):
for num in nums:
if nums.count(num) > 1:
return True
return False
numbers = ['1000223453', '1000227458', '10002235539', '100022343356', '1000227458', '1000223423','1000223412','1000223498']
print(has_duplicates(numbers)) # 输出:True
c=[n for n in numbers if numbers.count(n) > 1]
list2=list(set(c))
print('单据号',list2)
输出:
True
单据号 ['1000227458']
试试下面的方法:
has_duplicates=lambda iter_var:True if len(set(iter_var))==len(iter_var) else False
def find_duplicates(arr):
seen = set()
duplicates = []
for num in arr:
if num in seen:
duplicates.append(num)
seen.add(num)
return duplicates
has_duplicates()函数没值
我的意思时能不能把c=[n for n in numbers if numbers.count(n) > 1] 写道函数里面去?
函数里面的循环与推导式时循环同一个数组
def has_duplicates(*nums):
n = []
for num in nums:
if nums.count(num) > 1:
n.appenf(num)
return True,list2
list2 = list(set(n))
return False,list2
numbers = ['1000223453', '1000227458', '10002235539', '100022343356', '1000227458','1000227458', '1000223423','1000223412','1000223498']
print(has_duplicates(numbers)) # 重复数据输出不出来