在嵌套的字符串列表中查找唯一元素

时间:2019-02-02 09:40:19

标签: python python-3.x

类似于在此URL上发布的查询:

https://stackoverflow.com/questions/54477996/finding-unique-elements-in-nested-list/, 

我还有另一个查询。

如果我有一个从Pandas导入的列表,则需要获取一个列表作为所有唯一元素的输出作为

[Ac, Ad, An, Bi, Co, Cr, Dr, Fa, Mu, My, Sc]

一旦我拥有所有唯一元素,我想检查整个列表中每个元素的计数。 有人可以建议我该怎么做吗?

mylist = df.Abv.str.split().tolist()
mylist
[[‘Ac,Cr,Dr’],
 [‘Ac,Ad,Sc'],
 [‘Ac,Bi,Dr’],
 [‘Ad,Dr,Sc'],
 [‘An,Dr,Fa’],
 [‘Bi,Co,Dr’],
 [‘Dr,Mu’],
 [‘Ac,Co,My’],
 [‘Co,Dr’],
 [‘Ac,Ad,Sc'],
 [‘An,Ac,Ad’],
]

我尝试了不同的方法,但似乎无法使其正常工作。

试图将其转换为字符串并在该字符串上应用拆分功能,但无济于事。

5 个答案:

答案 0 :(得分:0)

您可以创建一个字典,将键作为列表值,并将值作为其计数

您的代码可能看起来像这样

mylists = [[‘Ac,Cr,Dr’],
 [‘Ac,Ad,Sc'],
 [‘Ac,Bi,Dr’],
 [‘Ad,Dr,Sc'],
 [‘An,Dr,Fa’],
 [‘Bi,Co,Dr’],
 [‘Dr,Mu’],
 [‘Ac,Co,My’],
 [‘Co,Dr’],
 [‘Ac,Ad,Sc'],
 [‘An,Ac,Ad’],
]
unique = {}

for mylist in mylists:
    for elem in mylist:
        unique[elem] = unique[elem]+1 if elem in unique else 1

unique.keys()将提供唯一的元素数组,如果您希望对任何值进行计数,都可以从字典中获取它,例如unique ['Ad']

答案 1 :(得分:0)

您可以使用collections.Counter制作元素计数的字典。这也使您可以轻松访问所有唯一元素的列表。看起来您有一个列表列表,其中每个子列表都包含一个ingle字符串。在将它们添加到计数器之前,您需要先split

from collections import Counter
count = Counter()
mylist = [['Ac,Cr,Dr'],
 ['Ac,Ad,Sc'],
 ['Ac,Bi,Dr'],
 ['Ad,Dr,Sc'],
 ['An,Dr,Fa'],
 ['Bi,Co,Dr'],
 ['Dr,Mu'],
 ['Ac,Co,My'],
 ['Co,Dr'],
 ['Ac,Ad,Sc'],
 ['An,Ac,Ad'],
]

for arr in mylist:
    count.update(arr[0].split(','))

print(count) # dictionary of symbols: counts
print(list(count.keys())) # list of all unique elements

答案 2 :(得分:0)

您可以在Python3中以这种方式完成

    mylist = [['Ac,Cr,Dr'],
    ['Ac,Ad,Sc'],
    ['Ac,Bi,Dr'],
    ['Ad,Dr,Sc'],
    ['An,Dr,Fa'],
    ['Bi,Co,Dr'],
    ['Dr,Mu'],
    ['Ac,Co,My'],
    ['Co,Dr'],
    ['Ac,Ad,Sc'],
    ['An,Ac,Ad'],
    ]

    uniquedict = {}

    for sublist in mylist:
        for item in sublist[0].split(','):
            if item in uniquedict.keys():
                uniquedict[item] += 1
            else:
                uniquedict[item] = 1

    print(uniquedict)
    print(list(uniquedict.keys()))

{'Ac':6,'Cr':1,'Dr':7,'Ad':4,'Sc':3,'Bi':2,'An':2,'Fa': 1,'Co':3,'Mu':1,'My':1} ['Ac','Cr','Dr','Ad','Sc','Bi','An','Fa','Co','Mu','My']

答案 3 :(得分:0)

您可以利用collectionsitertoolsfunctools提供的非常强大的工具,并获得一线解决方案。

如果您的列表只包含一个元素:

from collections import Counter
from itertools import chain
from functools import partial

if __name__ == '__main__':

    mylist = [
        ['Ac,Cr,Dr'],
        ['Ac,Ad,Sc'],
        ['Ac,Bi,Dr'],
        ['Ad,Dr,Sc'],
        ['An,Dr,Fa'],
        ['Bi,Co,Dr'],
        ['Dr,Mu'],
        ['Ac,Co,My'],
        ['Co,Dr'],
        ['Ac,Ad,Sc'],
        ['An,Ac,Ad'],
     ]

    # if lists contain only one element
    occurrence_count = Counter(chain(*map(lambda x: x[0].split(','), mylist)))

    items = list(occurrence_count.keys())  # items, with no repetitions
    all_items = list(occurrence_count.elements())  # all items
    ac_occurrences = occurrence_count['Ac']  # occurrences of 'Ac'

    print(f"Unique items: {items}")
    print(f"All list elements: {all_items}")
    print(f"Occurrences of 'Ac': {ac_occurrences}")

这就是你得到的:

Unique items: ['Ac', 'Cr', 'Dr', 'Ad', 'Sc', 'Bi', 'An', 'Fa', 'Co', 'Mu', 'My']
All list elements: ['Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Cr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Ad', 'Ad', 'Ad', 'Ad', 'Sc', 'Sc', 'Sc', 'Bi', 'Bi', 'An', 'An', 'Fa', 'Co', 'Co', 'Co', 'Mu', 'My']
Occurrences of 'Ac': 6

否则,如果您的列表包含多个元素:

from collections import Counter
from itertools import chain
from functools import partial

if __name__ == '__main__':

    mylist_complex = [
        ['Ac,Cr,Dr', 'Ac,Ad,Sc'],
        ['Ac,Ad,Sc', 'Ac,Bi,Dr'],
        ['Ac,Bi,Dr', 'Ad,Dr,Sc'],
        ['Ad,Dr,Sc', 'An,Dr,Fa'],
        ['An,Dr,Fa', 'Bi,Co,Dr'],
        ['Bi,Co,Dr', 'Dr,Mu'],
        ['Dr,Mu', 'Ac,Co,My'],
        ['Ac,Co,My', 'Co,Dr'],
        ['Co,Dr', 'Ac,Ad,Sc'],
        ['Ac,Ad,Sc', 'An,Ac,Ad'],
        ['An,Ac,Ad', 'Ac,Cr,Dr'],
    ]

    # if lists contain more than one element
    occurrence_count_complex = Counter(chain(*map(lambda x: chain(*map(partial(str.split, sep=','), x)), mylist_complex)))

    items = list(occurrence_count_complex.keys())  # items, with no repetitions
    all_items = list(occurrence_count_complex.elements())  # all items
    ac_occurrences = occurrence_count_complex['Ac']  # occurrences of 'Ac'

    print(f"Unique items: {items}")
    print(f"All list elements: {all_items}")
    print(f"Occurrences of 'Ac': {ac_occurrences}")

这是在这种情况下的结果:

Unique items: ['Ac', 'Cr', 'Dr', 'Ad', 'Sc', 'Bi', 'An', 'Fa', 'Co', 'Mu', 'My']
All list elements: ['Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Ac', 'Cr', 'Cr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Dr', 'Ad', 'Ad', 'Ad', 'Ad', 'Ad', 'Ad', 'Ad', 'Ad', 'Sc', 'Sc', 'Sc', 'Sc', 'Sc', 'Sc', 'Bi', 'Bi', 'Bi', 'Bi', 'An', 'An', 'An', 'An', 'Fa', 'Fa', 'Co', 'Co', 'Co', 'Co', 'Co', 'Co', 'Mu', 'Mu', 'My', 'My']
Occurrences of 'Ac': 12

答案 4 :(得分:0)

尝试以下方法:

from itertools import chain

mylist = [['Ac,Cr,Dr'], 
          ['Ac,Ad,Sc'], 
          ['Ac,Bi,Dr'], 
          ['Ad,Dr,Sc'], 
          ['An,Dr,Fa'],   
          ['Bi,Co,Dr'], 
          ['Dr,Mu'], 
          ['Ac,Co,My'], 
          ['Co,Dr'], 
          ['Ac,Ad,Sc'], 
          ['An,Ac,Ad']
         ]

flat_list = list(chain.from_iterable(mylist))

unique_list = set(','.join(flat_list).split(','))