多元组到Python中的两对元组? [英] Multiple Tuple to Two-Pair Tuple in Python?

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

问题描述

  tuple =('a','b','c' ,'d','e','f','g','h')

在这里:

  tuples = [('a','b'),('c','d'), ('e','f'),('g','h')] 

输入总是具有偶数数值。

解决方案

zip()是你的朋友:

  t =('a','b','c','d' e','f','g','h')
zip(t [:: 2],t [1 :: 2])


What is the nicest way of splitting this:

tuple = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')

into this:

tuples = [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]

Assuming that the input always has an even number of values.

解决方案

zip() is your friend:

t = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
zip(t[::2], t[1::2])

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

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