英文:
Multiple duplicate values are not displayed from list
问题
我尝试从列表中获取重复的值,使用以下函数。
该函数仅显示第一个或一个重复值。
示例列表如下 -
in_list = [sam, Michael, sam, john, john]
列表中有两个重复值sam和john。
但只显示sam。
英文:
I am trying to get duplicate values from a list using the following function.
The function is only displaying the first or one duplicate value.
Example list is -
in_list = [sam, Michael, sam, john, john]
There are two duplicate values in the list sam and john.
But only sam is displayed.
def DuplicateGuideCheck(in_list, report):
unique = set(in_list)
for each in in_list:
count = in_list.count(each)
if count > 1:
print each
report.write(each)
report.write('\n')
return True
print "NONE"
report.write('NONE\n')
return False
答案1
得分: 1
你在每当第一次出现计数大于1的项目时都使用了return
。一旦它被捕捉到,你的函数就返回,不再评估其余的值。
解决这个问题的一种方法是,统计每个单词的频率,看是否有任何一个单词的频率大于1,如果找到,则将其添加到重复集合中。
def DuplicateGuideCheck(in_list, report):
freq = {}
duplicates = set()
for val in in_list:
if val not in freq:
freq[val] = 0
freq[val] += 1
if freq[val] > 1:
duplicates.add(val)
if duplicates:
for val in duplicates:
report.write(val)
report.write("\n")
else:
report.write("None\n")
return True if duplicates else False
英文:
you have used return
whenever you get the first item whose count is more than 1. Once it is caught your function return back leaving the rest of the values to be not evaluated.
one approach to solving the problem. where you count the frequency of each word, and see if any one of them is more than 1 if found then add that to duplicates set.
def DuplicateGuideCheck(in_list, report):
freq = {}
duplicates = set()
for val in in_list:
if val not in freq:
freq[val] = 0
freq[val] += 1
if freq[val] > 1:
duplicates.add(val)
if duplicates:
for val in duplicates:
report.write(val)
report.write("\n")
else:
report.write("None\n")
return True if duplicates else False
</details>
# 答案2
**得分**: 1
我已经翻译好了您的代码部分:
```python
我不知道您的代码的正确缩进... 但是我已经正确缩进了您的代码,并且这里是`john`没有显示的原因。
这是您的代码... 当您找到一个`count`大于`1`的`element`时,您会返回`True`。因此,在找到`sam`后,函数将返回`True`。`return`关键字将在返回给定值后将程序的执行移出函数。因此,在找到`sam`后,不再迭代列表元素。
使用以下代码获取所有出现多次的元素:
```python
def DuplicateGuideCheck(in_list, report):
unique = set()
for each in in_list:
count = in_list.count(each)
if count > 1 and each not in unique:
unique.add(each)
print(each)
report.write(str(each))
report.write('\n')
print("NONE")
report.write('NONE\n')
上述代码使用了一个set
数据结构,它会监视已经写入report
的元素,以便我们只写入重复的元素,并且我没有使用return
关键字,因此会迭代所有元素。谢谢!!!
注意:我的代码不是最优化的方法。我只是纠正了您的错误。
<details>
<summary>英文:</summary>
I don't know the proper indentation of your code.. but I properly indented your code and here is the reason why `john` is not displayed.
def DuplicateGuideCheck(in_list, report):
unique = set(in_list)
for each in in_list:
count = in_list.count(each)
if count > 1:
print(each)
report.write(each)
report.write('\n')
return True
print("NONE")
report.write('NONE\n')
return False
This is your code... You are returning `True` when you found a `element` whose `count` is `more than 1`. So the function will return `True` after finding `sam`. The `return` keyword will brings the execution of the program outside the function after returning the given value. So after finding `sam`, no more list elements are `iterated`.
Use the following code to get all the elements that occurs more than once:
def DuplicateGuideCheck(in_list, report):
unique = set()
for each in in_list:
count = in_list.count(each)
if count > 1 and each not in unique:
unique.add(each)
print(each)
report.write(str(each))
report.write('\n')
print("NONE")
report.write('NONE\n')
The above code has a `set` data structure which will monitors the elements which are already wrote to the `report` so that we write the repeating elements only once and I don't used `return` keyword so that all the elements are iterated. Thank you!!!
`NOTE: My code is not an optimized approach. I just corrected your mistakes.`
</details>
# 答案3
**得分**: 0
以下是您要翻译的部分:
Duplicate is defined as: having two corresponding or identical parts.
i.e., if a value occurs exactly twice in the list then it's a duplicate.
However, we can have a version of the function that handles the strict meaning (exactly twice) or the less strict meaning of "more than one".
```python
import sys
from operator import eq, gt
def DuplicateGuideCheck(in_list, report, strict=True):
b, op = (2, eq) if strict else (1, gt)
dup = False
for name in set(in_list):
if op(in_list.count(name), b):
print(name, file=report)
dup = True
if not dup:
print('NONE', file=report)
return dup
lst = ['sam', 'Michael', 'sam', 'john', 'john', 'sam']
DuplicateGuideCheck(lst, sys.stdout)
Output:
john
...because 'sam' is in triplicate whereas...
DuplicateGuideCheck(lst, sys.stdout, False)
...produces...
sam
john
英文:
Duplicate is defined as: having two corresponding or identical parts.
i.e., if a value occurs exactly twice in the list then it's a duplicate.
However, we can have a version of the function that handles the strict meaning (exactly twice) or the less strict meaning of "more than one".
import sys
from operator import eq, gt
def DuplicateGuideCheck(in_list, report, strict=True):
b, op = (2, eq) if strict else (1, gt)
dup = False
for name in set(in_list):
if op(in_list.count(name), b):
print(name, file=report)
dup = True
if not dup:
print('NONE', file=report)
return dup
lst = ['sam', 'Michael', 'sam', 'john', 'john', 'sam']
DuplicateGuideCheck(lst, sys.stdout)
Output:
john
...because 'sam' is in triplicate whereas...
DuplicateGuideCheck(lst, sys.stdout, False)
...produces...
sam
john
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论