我的代码如下所示:
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)时,我得到这样的东西:
[(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 ...... 但是,我需要这样的东西:
[(a,b,c),(d,e,f),(g,h,i),(j,k,l)]
我已经通过c尝试了for循环:
for item in c:
list1 = []
data = list1.append(item)
但这没有帮助,导致:
None
None
基于此链接: https://mail.python.org/pipermail/tutor/2008-February/060321.html
我看起来很容易,但我是python的新手,尽管阅读量很大,但还没有找到解决方案。 感谢您的帮助!
答案 0 :(得分:5)
使用itertools.chain()
和list unpacking:
>>> items = [[('a','b','c'),('d','e','f')], [('g','h','i'),('j','k','l')]]
>>>
>>> list(chain(*items))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'l')]
答案 1 :(得分:0)
试试这个
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)