两个相同长度的列表。根据其中一个获得唯一对

时间:2016-04-11 14:43:47

标签: list python-3.x dictionary

我有两个相同长度的列表。让我们说:

x = ['123', '456', '789', '123']
y = ['aaa', 'aaa', 'bbb', 'ccc']

它们基本上是组合(123-aaa,456-aaa,789-bbb,123-ccc)。

我想要完成的是在我删除列表x中具有相同值的所有对之后,获取列表y中的唯一值和 BUT 中的唯一值的计数。所以在这个例子中我需要删除x [3]和y [3]。然后将字数计入字典:

{'aaa' : '2', 'bbb' : '1'}

我希望这很清楚。几个小时以来我一直撞在墙上......

2 个答案:

答案 0 :(得分:0)

检查dictCounter。 dict()替换重复的值,因此您需要先反转列表。

In [24]: x
Out[24]: ['123', '456', '789', '123']

In [25]: y
Out[25]: ['aaa', 'aaa', 'bbb', 'ccc']

In [26]: x.reverse()

In [27]: y.reverse()

In [28]: x
Out[28]: ['123', '789', '456', '123']

In [29]: y
Out[29]: ['ccc', 'bbb', 'aaa', 'aaa']

In [30]: z=dict(zip(x,y))

In [31]: z
Out[31]: {'123': 'aaa', '456': 'aaa', '789': 'bbb'}

In [32]: from collections import Counter

In [33]: values=z.values()

In [34]: values
Out[34]: dict_values(['aaa', 'bbb', 'aaa'])

In [37]: z = Counter(values)

In [38]: z
Out[38]: Counter({'aaa': 2, 'bbb': 1})

In [39]: z['aaa']
Out[39]: 2

In [40]: z['bbb']
Out[40]: 1

In [41]: z.keys()
Out[41]: dict_keys(['bbb', 'aaa'])

如果您可以选择转换为numpy数组,请选中unique

In [39]: import numpy as np
In [40]: x = np.array(['123', '456', '789', '123'])

In [41]: y = np.array(['aaa', 'aaa', 'bbb', 'ccc'])

In [42]: x_unique, x_index = np.unique(x, return_index=True)

In [43]: x_unique
Out[43]:
array(['123', '456', '789'],
      dtype='|S3')

In [44]: x_index
Out[44]: array([0, 1, 2])

In [47]: z_unique, z_counts= np.unique(y[x_index],return_counts=True)

In [48]: z_unique
Out[48]:
array(['aaa', 'bbb'],
      dtype='|S3')

In [49]: z_counts
Out[49]: array([2, 1])

In [50]: z = dict(zip(z_unique, z_counts))

In [51]: z
Out[51]: {'aaa': 2, 'bbb': 1}

答案 1 :(得分:0)

另一种简单的方法:

from collections import Counter

x = ['123', '456', '789', '123']
y = ['aaa', 'aaa', 'bbb', 'ccc']

uniq = []
res = Counter()
for number, letter in zip(x, y):
    if number not in uniq:
        uniq.append(number)
        res.update(letter)

print dict(res)

{' aaa':2,' bbb':1}