0

糟糕的标题,请酌情重命名或概括。

设想:

inputList = [[1, "Data x"], [2, "Data z"], [3, "Data x"]]

我想找到索引 [1] 的重复项。

DesiredOutput = [[1, "Data x"], [3, "Data x"]]

理想情况下, [1] 将是另一个列表,而不是字符串:

[[1, ["Data x1", "Data x2"], [2,  ["Data x1", "Data x2"]]
4

4 回答 4

2

怎么样(只是一种选择)

inputList = [[1, "Data x"], [2, "Data z"], [3, "Data x"]]

from operator import itemgetter
from collections import defaultdict

dd = defaultdict(list)
for i, v in enumerate(inputList):
    dd[v[1]].append(i)

dupes = [itemgetter(*v)(inputList) for v in dd.itervalues() if len(v) > 1]
# [([1, 'Data x'], [3, 'Data x'])]
于 2013-08-13T10:53:53.997 回答
1
inputList = [[1, "Data x"], [2, "Data z"], [3, "Data x"]]

DesiredOutput = [] # Create empty list

for x in inputList:  
    if x[1] == inputList[0][1]: #compare reference value with each item in inputList 
        DesiredOutput.append(x)  
print(DesiredOutput)

#[[1, "Data x"], [3, "Data x"]]
于 2013-08-13T11:08:15.490 回答
1

在这里使用字典:

>>> inputList = [[1, "Data x"], [2, "Data z"], [3, "Data x"]]
>>> from collections import defaultdict
>>> dic = defaultdict(list)
>>> for v, k in inputList:
...     dic[k].append(v)
...     
>>> dic
defaultdict(<type 'list'>, {'Data z': [2], 'Data x': [1, 3]})

由于第二个示例中的项目是列表,因此您需要先将它们转换为元组:

for v, k in inputList:
   dic[tuple(k)].append(v)
于 2013-08-13T10:21:42.030 回答
0
input_list = [[1, "Data x"], [2, "Data z"], [3, "Data x"]]
desired_output = [x for x in input_list if x[1] == input_list[0][1]]
print(desired_output)
于 2013-08-13T11:19:07.827 回答