加入元组列表
Join a list of tuples
我的代码如下所示:
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
基于此link:
https://mail.python.org/pipermail/tutor/2008-February/060321.html
我看起来很简单,但我是 python 的新手,尽管阅读了很多书,但仍未找到解决方案。
感谢您的帮助!
使用itertools.chain()
and 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')]
试试这个
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)
我的代码如下所示:
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
基于此link: https://mail.python.org/pipermail/tutor/2008-February/060321.html
我看起来很简单,但我是 python 的新手,尽管阅读了很多书,但仍未找到解决方案。 感谢您的帮助!
使用itertools.chain()
and 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')]
试试这个
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)