如何将列表元素组合在元组中?

发布于 2025-02-13 18:29:33 字数 720 浏览 1 评论 0原文

我正在努力结合元组中的特定列表元素。会喜欢任何反馈或帮助!我是Python的新手,如果这不是一个好问题,则很抱歉。

如果我有这样的元组列表:

tuple_1 = [('A', 'B', 'C', 'D'), ('A', 'H'), ('B', 'C', 'D', 'A')]

我想从列表中的每个元组中组合元素“ b”,“ c”和“ d”:

tuple_1_new = [('A', 'BCD'), ('A', 'H'), ('BCD', 'A')]

我的代码看起来像这样:

next_insert = [(iter(x)) for x in tuple_1]
tuple_1_new = [i + next(next_insert) + next(next_insert) if i == "B" else i for i in next_insert]

但是当我打印时(tuple_1_new),它正在给予我的输出:

[<tuple_iterator object at ...>, <tuple_iterator object at ...>, <tuple_iterator object at ...>]

我觉得我的代码正确,但是我对此输出感到困惑。再次,如果这是一个愚蠢的问题,对不起。感谢任何帮助 - 谢谢!

I am struggling to combine specific list elements from a tuple. Would love any feedback or help! I am new to Python, so apologies if this is not a good question.

If I have a tuple list like this:

tuple_1 = [('A', 'B', 'C', 'D'), ('A', 'H'), ('B', 'C', 'D', 'A')]

I want to combine elements 'B', 'C', and 'D' from every tuple in the list:

tuple_1_new = [('A', 'BCD'), ('A', 'H'), ('BCD', 'A')]

My code looks like this:

next_insert = [(iter(x)) for x in tuple_1]
tuple_1_new = [i + next(next_insert) + next(next_insert) if i == "B" else i for i in next_insert]

but when I print(tuple_1_new), it is giving me an output of:

[<tuple_iterator object at ...>, <tuple_iterator object at ...>, <tuple_iterator object at ...>]

I feel like my code is correct, but I'm confused with this output. Again, sorry if this is a dumb question. Would appreciate any help - thanks!

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

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

发布评论

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

评论(4

随心而道 2025-02-20 18:29:34
def foo(arr):
    w = "".join(arr)    
    ind = w.find("BCD")
    if ind >= 0:
        ans = list(arr)
        return tuple(ans[:ind] + ["BCD"] + ans[ind + 3:])
    return arr

[foo(x) for x in tuple_1]
# [('A', 'BCD'), ('A', 'H'), ('BCD', 'A')]
def foo(arr):
    w = "".join(arr)    
    ind = w.find("BCD")
    if ind >= 0:
        ans = list(arr)
        return tuple(ans[:ind] + ["BCD"] + ans[ind + 3:])
    return arr

[foo(x) for x in tuple_1]
# [('A', 'BCD'), ('A', 'H'), ('BCD', 'A')]
自控 2025-02-20 18:29:34

另一个解决方案,使用发电机:

tuple_1 = [("A", "B", "C", "D"), ("A", "H"), ("B", "C", "D", "A")]

def fn(x):
    while x:
        if x[:3] == ("B", "C", "D"):
            yield "".join(x[:3])
            x = x[3:]
        else:
            yield x[0]
            x = x[1:]


out = [tuple(fn(t)) for t in tuple_1]
print(out)

打印:

[('A', 'BCD'), ('A', 'H'), ('BCD', 'A')]

Another solution, using generator:

tuple_1 = [("A", "B", "C", "D"), ("A", "H"), ("B", "C", "D", "A")]

def fn(x):
    while x:
        if x[:3] == ("B", "C", "D"):
            yield "".join(x[:3])
            x = x[3:]
        else:
            yield x[0]
            x = x[1:]


out = [tuple(fn(t)) for t in tuple_1]
print(out)

Prints:

[('A', 'BCD'), ('A', 'H'), ('BCD', 'A')]
年华零落成诗 2025-02-20 18:29:34

列表理解答案:

[tuple([t for t in tup if t not in ['B', 'C', 'D']] + [''.join([t for t in tup if t in ['B', 'C', 'D']])]) for tup in tuple_1]

尽管没有完全获取所需的输出,但是打印:

[('A', 'BCD'), ('A', 'H', ''), ('A', 'BCD')]

注意:在“简单”列表中,'in itobles_name中的x for x for x in itsoble_name'distecting'defressing循环使用'x'(或如果扩展元组,则名称集合或在每个循环中执行ZIP提取)作为变量。
在列表理解中执行列表理解(对于循环内部的循环)中时,每个循环将贡献一个或多个变量,显然一定不能引起名称碰撞。

A list comprehension answer:

[tuple([t for t in tup if t not in ['B', 'C', 'D']] + [''.join([t for t in tup if t in ['B', 'C', 'D']])]) for tup in tuple_1]

Although not quite getting the desired output, prints:

[('A', 'BCD'), ('A', 'H', ''), ('A', 'BCD')]

Note: In a 'simple' list comprehension, the 'for x in iterable_name' creates a processing loop using 'x' (or a collection of names if expanding a tuple or performing a zip extraction) as variable(s) in each loop.
When performing list comprehension within a list comprehension (for loop inside for loop), each loop will contribute one or more variables, which obviously must not incur a name collision.

无声静候 2025-02-20 18:29:34

假设字符串是您的示例中的单个字母:

tuple_1_new = [tuple(' '.join(t).replace('B C D', 'BCD').split())
               for t in tuple_1]

或与重新恢复:

tuple_1_new = [tuple(re.findall('BCD|.', ''.join(t)))
               for t in tuple_1]

Assuming the strings are single letters like in your example:

tuple_1_new = [tuple(' '.join(t).replace('B C D', 'BCD').split())
               for t in tuple_1]

Or with Regen:

tuple_1_new = [tuple(re.findall('BCD|.', ''.join(t)))
               for t in tuple_1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文