如何解压列表?

发布于 2024-10-04 20:21:51 字数 351 浏览 1 评论 0原文

当以这种方式从列表中提取数据时

line[0:3], line[3][:2], line[3][2:]

,我收到一个数组和后面的两个变量,正如预期的那样:

(['a', 'b', 'c'], 'd', 'e')

我需要操作列表,所以最终结果是

('a', 'b', 'c', 'd', 'e')

如何?谢谢。

PS 是的,我知道我可以将第一个元素写为 line[0], line[1], line[2],但我认为这是一个非常尴尬的解决方案。

When extracting data from a list this way

line[0:3], line[3][:2], line[3][2:]

I receive an array and two variables after it, as should be expected:

(['a', 'b', 'c'], 'd', 'e')

I need to manipulate the list so the end result is

('a', 'b', 'c', 'd', 'e')

How? Thank you.

P.S. Yes, I know that I can write down the first element as line[0], line[1], line[2], but I think that's a pretty awkward solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

§对你不离不弃 2024-10-11 20:21:51
from itertools import chain
print tuple(chain(['a', 'b', 'c'], 'd', 'e'))

输出:

('a', 'b', 'c', 'd','e')
from itertools import chain
print tuple(chain(['a', 'b', 'c'], 'd', 'e'))

Output:

('a', 'b', 'c', 'd','e')
软的没边 2024-10-11 20:21:51

试试这个。

line = ['a', 'b', 'c', 'de']
tuple(line[0:3] + [line[3][:1]] + [line[3][1:]])
('a', 'b', 'c', 'd', 'e')

笔记:
我认为你的切片逻辑中有一些有趣的事情。
如果 [2:] 返回任何字符,[:2] 必须返回 2 个字符。
请提供您的输入线。

Try this.

line = ['a', 'b', 'c', 'de']
tuple(line[0:3] + [line[3][:1]] + [line[3][1:]])
('a', 'b', 'c', 'd', 'e')

NOTE:
I think there is some funny business in your slicing logic.
If [2:] returns any characters, [:2] must return 2 characters.
Please provide your input line.

情深如许 2024-10-11 20:21:51

明显的答案:而不是你的第一行,做:

line[0:3] + [line[3][:2], line[3][2:]]

假设 line[0:3] 是一个列表。否则,您可能需要进行一些细微的调整。

Obvious answer: Instead of your first line, do:

line[0:3] + [line[3][:2], line[3][2:]]

That works assuming that line[0:3] is a list. Otherwise, you may need to make some minor adjustments.

っ左 2024-10-11 20:21:51

该函数

def merge(seq):
    merged = []
    for s in seq:
        for x in s:
            merged.append(x)
    return merged 

来源:http://www.testingreflections.com/node/view/4930

This function

def merge(seq):
    merged = []
    for s in seq:
        for x in s:
            merged.append(x)
    return merged 

source: http://www.testingreflections.com/node/view/4930

绅刃 2024-10-11 20:21:51
def is_iterable(i):
    return hasattr(i,'__iter__')

def iterative_flatten(List):
    for item in List:
        if is_iterable(item):
            for sub_item in iterative_flatten(item):
                yield sub_item
        else:
            yield item

def flatten_iterable(to_flatten):
    return tuple(iterative_flatten(to_flatten))

这应该适用于任何级别的嵌套

def is_iterable(i):
    return hasattr(i,'__iter__')

def iterative_flatten(List):
    for item in List:
        if is_iterable(item):
            for sub_item in iterative_flatten(item):
                yield sub_item
        else:
            yield item

def flatten_iterable(to_flatten):
    return tuple(iterative_flatten(to_flatten))

this should work for any level of nesting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文