加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 在元组列表列表中拆分元组列表

发布时间:2020-12-20 11:34:13 所属栏目:Python 来源:网络整理
导读:我有一个元组列表,如: [(1,a),(2,b),(1,e),(3,c),(4,d),(0,(6,(8,e)] 我想把它分成每个“b”的列表列表 [[(1,b)],[(1,[(2,[(0,[(6,e)]] 有没有pythonic方式来做到这一点? 解决方法 my_list = [(1,"a"),"b"),"e"),"c"),"b")]result,temp = [],[]for item in
我有一个元组列表,如:

[(1,a),(2,b),(1,e),(3,c),(4,d),(0,(6,(8,e)]

我想把它分成每个“b”的列表列表

[[(1,b)],[(1,[(2,[(0,[(6,e)]]

有没有pythonic方式来做到这一点?

解决方法

my_list = [(1,"a"),"b"),"e"),"c"),"b")]
result,temp = [],[]

for item in my_list:
    temp.append(item)
    if item[1] == 'b':
        result.append(temp)
        temp = []

if len(temp) > 0:
    result.append(temp)

print result
# [[(1,'a'),'b')],'e'),'c'),'b')]]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读