加入元组列表 [英] Join a list of tuples

查看:93
本文介绍了加入元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

from itertools import groupby

for key, group in groupby(warnstufe2, lambda x: x[0]):

    for element in group:
        a = element[1:4]
        b = element[4:12]
        c = [a,b]
        print(c)

当我打印(c)时,我得到这样的信息:

When I print (c) I get something like this:

[(a,b,c),(d,e,f)] 
[(g,h,i),(j,k,l)]

其中a1 =(a,b,c)和b1 =(d,e,f)和a2 =(g,h,i)和b2 =(j,k,l). 当然有a3 ...和b3 ... 但是,我需要这样的东西:

where a1=(a,b,c) and b1=(d,e,f) and a2=(g,h,i) and b2 = (j,k,l). Of course there is a3... and b3... However, I need something like this:

[(a,b,c),(d,e,f),(g,h,i),(j,k,l)]

我已经尝试过通过c进行for循环:

I already tried a for loop through c:

for item in c:
    list1 = []
    data = list1.append(item)

但这没有帮助,并导致:

But this did not help and resulted in:

None
None

基于此链接: https://mail.python.org/pipermail/tutor/2008-February /060321.html

我似乎很简单,但是我是python的新手,尽管有很多读物,但还没有找到解决方案. 感谢您的帮助!

I appears to be easy, but I am new to python and did not find a solution yet, despite a lot of reading. I appreciate your help!

推荐答案

尝试一下

from itertools import groupby

result = []
for key, group in groupby(warnstufe2, lambda x: x[0]):
    for element in group:
        a = element[1:4]
        b = element[4:12]
        c = [a,b]
        result.append(c)

print (result)

这篇关于加入元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆