python - Python 中的多元组到两对元组?

标签 python data-structures tuples

最好的分割方式是什么:

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

进入这个:

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

假设输入总是有偶数个值。

最佳答案

zip() 是你的 friend :

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

关于python - Python 中的多元组到两对元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/756550/

相关文章:

c# - 使用什么样的数据结构?

algorithm - 给定一个数字 0-9 的数组和一个整数 n,找到可由输入数组组成且小于 n 的所有整数

python - 如何在Python中使用pandas根据两个参数对记录进行分组

python - subprocess.Popen 无法正确管道 urllib3 响应

javascript结合数组和字典

python - 从列表中的元组中删除空字符串

c++ - tuple<int, 3> t1(1), t2(2), t3(3);

python - 如何使用列名列表从旧数据帧创建新的 pandas 数据帧

python - lxml XPathposition() 不起作用

python - Python 中 tuple 和 *args 的理解困惑