Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从嵌套列表和元组列表获取所有unqiue字符串_Python_List_Tuples_Unique - Fatal编程技术网

Python 从嵌套列表和元组列表获取所有unqiue字符串

Python 从嵌套列表和元组列表获取所有unqiue字符串,python,list,tuples,unique,Python,List,Tuples,Unique,是否有一种快速的方法来获取唯一的元素,特别是从嵌套列表和元组的列表或元组中获取字符串。应删除“min”和“max”等字符串。列表和元组可以以任何可能的方式嵌套。唯一始终相同的元素是核心的元组,如包含字符串的“a”、0、49 与列表或元组类似: 想要的输出: 到目前为止,我尝试的是: flat_list = list(sum([item for sublist in x for item in sublist],())) 但这不会进入嵌套对象的核心,这将获得给定iterable内的任何字符串,而

是否有一种快速的方法来获取唯一的元素,特别是从嵌套列表和元组的列表或元组中获取字符串。应删除“min”和“max”等字符串。列表和元组可以以任何可能的方式嵌套。唯一始终相同的元素是核心的元组,如包含字符串的“a”、0、49

与列表或元组类似:

想要的输出:

到目前为止,我尝试的是:

flat_list = list(sum([item for sublist in x for item in sublist],()))
但这不会进入嵌套对象的核心,这将获得给定iterable内的任何字符串,而不管iterable内的位置:

# 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']
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']
这将获取给定iterable内的任何字符串,而不管iterable内的位置:

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']
您可以使用此处定义的代码展平:

您可以使用此处定义的代码展平:

检查列表中的每个元素,从中获取唯一的元素,如此处-,或者有更多其他链接->将这些链接存储到新列表->从新列表中删除重复项您要使用的列表将始终采用这种格式?@AnagnostouJohn No可能是任何可能的嵌套列表或元组,唯一保持相同形状的元素是核心元组,如'a',0,49为什么/如何使a,0,max不是有效元组?你是否只想得到两个内部列表中的那些?请更仔细地解释你想要什么,以及你如何决定一个元组是否值得放入你的结果中……不,它们是值得的。我只是说,这些元组包含我需要的字符串,并且它们通过列表中的每个元素也具有相同的形状,从中获得唯一的元素,如这里-,或者还有更多其他链接->将这些链接存储到新列表->从新列表中删除重复项您要使用的列表将始终采用这种格式?@AnagnostouJohn No可能是任何可能的嵌套列表或元组,唯一保持相同形状的元素是核心元组,如“a”,0,49为什么/如何使用a,0,max不是有效的元组吗?你是否只想得到两个内部列表中的那些?请更仔细地解释你想要什么,以及你如何决定一个元组是否值得放入你的结果中……不,它们是值得的。我只是说,这些元组包含我需要的字符串,它们也有相同的形状,但与Rezvanov Maxim方法有什么区别?isinstance方法对一个string@Valor差异在输出中是可见的。Rezvanov只使用列表切片硬编码的第三个元素链接所有元素:lst1=listflantlst1[::3]部分-这意味着他只查看粗体元素:'a',0,49,'b',0,70,'c',0,49,'e',0,70,'a',0,'max','b',0,100-其余的元素被切掉,不考虑。他在每个内部3元组的第0个位置获取所有字符串。我得到所有字符串,不管它们在元组中的什么位置[续][续]我认为这更接近于你的OPs标题,从嵌套列表和元组列表中获取所有unqiue字符串,因此需要在3、6、9个月内为用户构建一个数据库,这些用户搜索某个东西,碰巧找到了你的问题,并可以使用给定的答案来解决他们的实际问题。但是,Rezvanov Maxim方法有什么区别呢?isinstance方法对一个string@Valor差异在输出中是可见的。Rezvanov只使用列表切片硬编码的第三个元素链接所有元素:lst1=listflantlst1[::3]部分-这意味着他只查看粗体元素:'a',0,49,'b',0,70,'c',0,49,'e',0,70,'a',0,'max','b',0,100-其余的元素被切掉,不考虑。他在每个内部3元组的第0个位置获取所有字符串。我得到所有字符串,不管它们在元组中的什么位置[续][续]我认为这更接近于你的OPs标题,从嵌套列表和元组列表中获取所有unqiue字符串,因此需要在3、6、9个月内为用户构建一个数据库,这些用户搜索某些东西,碰巧找到了你的问题,并可以使用给定的答案来解决他们的实际问题。
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"]