Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/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_Loops_Duplicates - Fatal编程技术网

Python 如何在列表中查找重复项,并对找到的每个重复项实例执行不同的计算?

Python 如何在列表中查找重复项,并对找到的每个重复项实例执行不同的计算?,python,list,loops,duplicates,Python,List,Loops,Duplicates,我想在列表上迭代并找到重复项。如果发现重复项,我想对该重复项进行计算。如果发现同一实例的另一个副本,则应执行不同的计算 例如: 开始迭代列表: a_list = [1, 2, 3, 1, 2, 3, 1, 2, 3] 继续迭代 1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance] 1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on ins

我想在
列表上迭代并找到重复项。如果发现重复项,我想对该重复项进行计算。如果发现同一实例的另一个副本,则应执行不同的计算

例如:

开始迭代列表

a_list = [1, 2, 3, 1, 2, 3, 1, 2, 3]
继续迭代

1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance]
1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on instance]
1, 2, 3, 1, 2, 3 [Instance of duplicate 3 found - Perform 3+3 on instance]
1, 2, 3, 1, 2, 3, 1 [Second instance of duplicate 1 found - Perform 1+1+1 on instance]
1, 2, 3, 1, 2, 3, 1, 2 [Second instance of duplicate 2 found - Perform 2+2+2 on instance]
继续迭代

1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance]
1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on instance]
1, 2, 3, 1, 2, 3 [Instance of duplicate 3 found - Perform 3+3 on instance]
1, 2, 3, 1, 2, 3, 1 [Second instance of duplicate 1 found - Perform 1+1+1 on instance]
1, 2, 3, 1, 2, 3, 1, 2 [Second instance of duplicate 2 found - Perform 2+2+2 on instance]
继续迭代

1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance]
1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on instance]
1, 2, 3, 1, 2, 3 [Instance of duplicate 3 found - Perform 3+3 on instance]
1, 2, 3, 1, 2, 3, 1 [Second instance of duplicate 1 found - Perform 1+1+1 on instance]
1, 2, 3, 1, 2, 3, 1, 2 [Second instance of duplicate 2 found - Perform 2+2+2 on instance]
继续迭代

1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance]
1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on instance]
1, 2, 3, 1, 2, 3 [Instance of duplicate 3 found - Perform 3+3 on instance]
1, 2, 3, 1, 2, 3, 1 [Second instance of duplicate 1 found - Perform 1+1+1 on instance]
1, 2, 3, 1, 2, 3, 1, 2 [Second instance of duplicate 2 found - Perform 2+2+2 on instance]
继续迭代

1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance]
1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on instance]
1, 2, 3, 1, 2, 3 [Instance of duplicate 3 found - Perform 3+3 on instance]
1, 2, 3, 1, 2, 3, 1 [Second instance of duplicate 1 found - Perform 1+1+1 on instance]
1, 2, 3, 1, 2, 3, 1, 2 [Second instance of duplicate 2 found - Perform 2+2+2 on instance]
完成后,将创建一个包含所有计算的新列表:

1, 2, 3, 1, 2, 3, 1, 2, 3 [Second instance of duplicate 3 found - Perform 3+3+3 on instance]

有人能给我解释一下如何找到重复项以及计算这些重复项的实例,以便我可以对每个新的重复项实例执行不同的计算吗

我会使用字典来记录使用了哪些值以及使用次数

new_list = [1, 2, 3, 2, 4, 6, 3, 6, 9]

希望这有帮助

这是一个经过调整的版本,适合粉丝
收藏。defauldict

将集合导入为coll
a_list=[1,2,3,1,2,3,1,2,3]
d=colls.defaultdict(int)
结果=[]
对于列表中的val:
d[val]+=val
结果.追加(d[val])
打印(结果)
输出:

[1,2,3,2,4,6,3,6,9]

您可以创建一个dict作为计数器,在循环列表时检查每个数字的计数并执行操作。使用集合中的计数器。这里可以找到一个例子。问题是什么?你试过什么,做过什么研究吗?这能回答你的问题吗?我建议你读一下dict方法。它可以用来减少你的压力conditions@Tomerikoo或者
collections.defaultdict
,类似的想法。