简体   繁体   English

列表理解和 str.split() 返回列表中的列表而不是单个字符串

[英]List comprehension and str.split() returns lists in list instead of individual strings

Assume认为

thread_sold = ['white', 'white&blue', 'white&blue', 'white', 'white&yellow', 'purple', 'purple&yellow', 'purple&yellow']

I need all items from that list, sometimes separated by & sometimes not.我需要该列表中的所有项目,有时用 & 有时不分隔。 The below function works, but I'd like to know how to do this with list comprehension.下面的 function 有效,但我想知道如何通过列表理解来做到这一点。

def cant_list_comprehend(lst):
    thread_sold_split = []
    for i in lst:
        if "&" in i:
            for x in i.split("&"):
                thread_sold_split.append(x)
        else:
            thread_sold_split.append(i)
    return thread_sold_split

returns ['white', 'white', 'blue', 'white', 'blue', 'white', 'white', 'yellow', 'purple', 'purple', 'yellow', 'purple', 'yellow', 'blue', 'blue', 'purple', 'blue', 'white', 'white'...]

list comprehension I tried:我试过的列表理解:

thread_sold_split_bad = [
    [x for x in y.split("&")] if "&" in y else y for y in thread_sold
]

returns ['white', ['white', 'blue'], ['white', 'blue'], 'white', ['white', 'yellow'], 'purple', ['purple', 'yellow'],...]

I'd like to avoid adding named functions to my code if it can be avoided, I'm also interested in solving this with lambda functions, although I'm learning basic stuff at the moment.如果可以避免的话,我想避免在我的代码中添加命名函数,我也有兴趣用 lambda 函数解决这个问题,尽管我现在正在学习基本的东西。

thread_sold_split_bad = [
    [x for x in y.split("&")] if "&" in y else y for y in thread_sold
]

Your attempt above does not work because you are doing nested list comprehensions instead of a plain comprehension (notice the brackets inside the main brackets).您上面的尝试不起作用,因为您正在执行嵌套列表推导而不是普通推导(注意主括号内的括号)。 Here is the corrected version:这是更正后的版本:

>>> thread_sold_split = [x for y in thread_sold for x in y.split("&")]
>>> print(thread_sold_split)
['white', 'white', 'blue', 'white', 'blue', 'white', 'white', 'yellow', 'purple', 'purple', 'yellow', 'purple', 'yellow']

You could use functools.reduce :你可以使用functools.reduce

from functools import reduce

reduce(lambda x, y: x + y, [x.split("&") for x in thread_sold])

or或者

import itertools

list(itertools.chain.from_iterable(x.split("&") for x in thread_sold))

OUTPUT OUTPUT

['white', 'white', 'blue', 'white', 'blue', 'white', 'white', 'yellow', 'purple', 'purple', 'yellow', 'purple', 'yellow']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串列表的 str.split() 等价物? - Equivalent of str.split() for a list of strings? str.split()返回的Pandas排序列表 - Pandas sort list returned by str.split() Python:为什么 str.split() 返回一个列表而 str.partition() 返回一个元组? - Python: Why Does str.split() Return a list While str.partition() Returns a tuple? str=str.split('=') AttributeError: 'list' 对象没有属性 'split' - str=str.split('=') AttributeError: 'list' object has no attribute 'split' str.split(“ list,''))和str.split(list,'')有什么区别 - what's the difference between str.split(“list, ' ' ”) and str.split(list, ' ') “str.split()”方法返回“[0]”而不是拆分字符串 - “str.split()” method returns “[0]” instead of splitting the string python list comprehension:返回字符串列表而不是列表列表 - python list comprehension: return a list of strings instead of a list of lists 如何根据分隔符将列表拆分为子列表,类似于 str.split()? - How to split a list into sublists based on a separator, similar to str.split()? Python:使用str.split并使列表索引超出范围 - Python: Using str.split and getting list index out of range 使用 str.split 构建一个带有“one-line for”的字符串列表 - Building a list of string with a "one-line for" with str.split
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM