Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中返回值字典?_Python_List_Dictionary_Edit - Fatal编程技术网

Python 如何从列表中返回值字典?

Python 如何从列表中返回值字典?,python,list,dictionary,edit,Python,List,Dictionary,Edit,如何编辑注释掉的代码段,使其返回重复数字的字典,而不是返回“{}” 这里有一个解决方案 如果列表中有多个元素,listofeems.count(x)>1,并且我们的重复列表不包含该元素,duplicates.count(x)==0,那么我们可以将该元素添加到重复列表中 listOfElems = ["a", "c", "c"] def checkIfDuplicates_3(listOfElems): duplicates

如何编辑注释掉的代码段,使其返回重复数字的字典,而不是返回“{}”

这里有一个解决方案

如果列表中有多个元素,
listofeems.count(x)>1
,并且我们的重复列表不包含该元素,
duplicates.count(x)==0
,那么我们可以将该元素添加到重复列表中

listOfElems = ["a", "c", "c"]


def checkIfDuplicates_3(listOfElems):
    duplicates = []
    for x in listOfElems:
        if listOfElems.count(x) > 1 and duplicates.count(x) == 0:
            duplicates.append(x)
    return duplicates


# test
test = [listOfElems]

for t in test:
    output = checkIfDuplicates_3(t)
    print("The duplicate in", t, "is", output)

我不明白你为什么需要在这里用字典。您没有键或值。您所要做的就是返回一封在列表中重复的信

下面的代码使用列表理解来解决您的问题。 首先,我们调用此函数传入
listofeem
。 然后,我们将遍历给定的列表并检查每个元素的出现情况。我们可以使用
count
进行此操作。看来你也用过。如果一封信出现不止一次,我们会将该信添加到一个新的列表中

在您提供的示例中,我们将得到类似的结果,
['c','c']
。如果
c
只出现一次就好了。为了解决这个问题,我们可以使用
set
,这将给我们提供
{'c'}
,这非常好。 为了完成这一切,我们将此集合作为列表返回

listOfElems = ["a", "c", "c"]

def checkIfDuplicates_3(listOfElems):
    dups = [x for x in listOfElems if listOfElems.count(x) > 1]
    return list(set(dups))
print("The duplicates in", listOfElems, "are", checkIfDuplicates_3(listOfElems))

使用
设置
和列表理解,一行函数可以执行以下操作:

listOfElems = ["a", "c", "c","e","c","e","f"]

def checkIfDuplicates_3(listOfElems): return set([x for x in listOfElems if listOfElems.count(x)>1])

#test
test = [listOfElems]

for t in test:
    output = checkIfDuplicates_3(t)
    print("The duplicate in", t, "is", sorted(output))
请注意,集合不能保留插入顺序。要获得有序输出,请使用排序后的
(如上面的示例所示)

要获取统计发生次数>1的词典,您可以使用:

def checkIfDuplicates_3(listOfElems):
    return {x:listOfElems.count(x) for x in set(listOfElems) if listOfElems.count(x)>1}

e、 g.返回
checkIfDuplicates_3(列表元素)
结果
{e':2,'c':3}
(请参见上面的排序注释)。

这是否回答了您的问题@sagar1025虽然它确实解决了他的整体问题,但它没有回答他的具体问题,即确定重复项所需的条件。为什么你选择字典而不是列表?你永远不会得到字典,当您将重复项分配给空dict时,您总是会得到一个列表,当您为循环创建时,您调用了return语句,因此当它搜索计数和返回运行数时,将中断循环并返回代码的结尾,该代码将为空dict,因此,无需在此处返回dict
def checkIfDuplicates_3(listOfElems):
    return {x:listOfElems.count(x) for x in set(listOfElems) if listOfElems.count(x)>1}