可怕的标题,请在适当时重命名或概括。
情景:
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"]]
答案 0 :(得分: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'])]
答案 1 :(得分: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)
答案 2 :(得分: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"]]
答案 3 :(得分: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)