从嵌套列表和元组列表中获取所有不确定字符串

时间:2018-11-01 08:18:14

标签: python list tuples unique

有没有一种快速的方法来获取唯一元素,特别是从嵌套列表和元组的列表或元组中获取字符串。诸如“ min”和“ max”之类的字符串应删除。列表和元组可以以任何可能的方式嵌套。唯一会一直相同的元素是核心(如('a',0,49))中的元组,其中包含字符串。

像那些列表或元组:

lst1=[[(('a',0,49),('b',0,70)),(('c',0,49))],
     [(('c',0,49),('e',0,70)),(('a',0,'max'),('b',0,100))]]

tuple1=([(('a',0,49),('b',0,70)),(('c',0,49))],
     [(('c',0,49),('e',0,70)),(('a',0,'max'),('b',0,100))]) 

想要的输出:

uniquestrings = ['a','b','c','e']

到目前为止我尝试过的事情:

flat_list = list(sum([item for sublist in x for item in sublist],()))

但这不会到达嵌套对象的“核心”

3 个答案:

答案 0 :(得分:2)

# generative flatten algorithm
def flatten(lst):
    for x in lst:
        if isinstance(x, (list, tuple,)):
            for x in flatten(x):
                yield x
        else:
            yield x

# source list (or tuple)
lst1 = [[(('a', 0, 49), ('b', 0, 70)), (('c', 0, 49))],
        [(('c', 0, 49), ('e', 0, 70)), (('a', 0, 'max'), ('b', 0, 100))]]

# getting elements
lst1 = list(flatten(lst1))[::3]
# >>> ['a', 'b', 'c', 'c', 'e', 'a', 'b']

# delete non-unique elements and sorting result list
lst1 = sorted(list(set(lst1)))
# >>> ['a', 'b', 'c', 'e']

答案 1 :(得分:2)

这将在给定的可迭代对象内获取 any 字符串,而与可迭代对象内的位置无关:

def isIterable(obj):
    # cudos: https://stackoverflow.com/a/1952481/7505395
    try:
        _ = iter(obj)
        return True
    except:
        return False

# shortcut
isString = lambda x: isinstance(x,str)

def chainme(iterab):
    # strings are iterable too, so skip those from chaining
    if isIterable(iterab) and not isString(iterab):
        for a in iterab:
            yield from chainme(a)
    else: 
        yield iterab

lst1=[[(('a',0,49),('b',0,70)),(('c',0,49))],
     [(('c',0,49),('e',0,70)),(('a',0,'max'),('b',0,100))]]

tuple1=([(('a',0,49),('b',0,70)),(('c',0,49))],
     [(('c',0,49),('e',0,70)),(('a',0,'max'),('b',0,100))]) 


for k in [lst1,tuple1]:
    # use only strings
    l = [x for x in chainme(k) if isString(x)]
    print(l)
    print(sorted(set(l)))
    print()

输出:

['a', 'b', 'c', 'c', 'e', 'a', 'max', 'b'] # list
['a', 'b', 'c', 'e', 'max']                # sorted set of list

['a', 'b', 'c', 'c', 'e', 'a', 'max', 'b']
['a', 'b', 'c', 'e', 'max']

答案 2 :(得分:1)

import collections

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el

[x for x in set(list(flatten(lst1))) if str(x).isalpha() if str(x) != "max" and "min"]

您可以按照以下定义使用代码来展平: Flatten an irregular list of lists