一个用于语句的多个元组 [英] Multiple tuples for one for statement

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

问题描述

我有三个相同大小的元组:tup1,tup2,tup3


我想做这样的事情:


代表a,b,c代表tup1,tup2,tup3:

打印a

打印b

打印c


当然,当你尝试运行上面的伪代码时会出现错误。

完成这项工作的正确方法是什么?


谢谢,


Harlin

I have three tuples of the same size: tup1, tup2, tup3

I''d like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c

Of course, you get an error when you try to run the pseudocode above.
What is the correct way to get this done?

Thanks,

Harlin

推荐答案

for a,b,c in zip(tup1,tup2,tup3):

打印一份

打印b

打印c

-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

洛杉矶,CA 90095

http://www.jamesstroud.com/
for a,b,c in zip(tup1, tup2, tup3):
print a
print b
print c
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/


Harlin Seritt写道:
Harlin Seritt wrote:
我有三个相同大小的元组:tup1,tup2,tup3

我会的喜欢这样做:

a,b,c in tup1,tup2,tup3:
打印
打印b
打印c

当然,当您尝试运行上面的伪代码时会出现错误。
完成此操作的正确方法是什么?
I have three tuples of the same size: tup1, tup2, tup3

I''d like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c

Of course, you get an error when you try to run the pseudocode above.
What is the correct way to get this done?




for a,b,c in zip(tup1,tup2,tup3):

打印一份

打印b

打印c


如果你的元组成为迭代器,请查看itertools.izip。


STeVe



for a, b, c in zip(tup1, tup2, tup3):
print a
print b
print c

If your tuples become iterators, look into itertools.izip.

STeVe


Harlin Seritt写道:
Harlin Seritt wrote:
我有三个相同大小的元组:tup1,tup2,tup3

我想做这样的事情:
for a,b,c in tup1,tup2,tup3:
print a
print b
print c
I have three tuples of the same size: tup1, tup2, tup3

I''d like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c




假设你希望a,b,c是三个元组中的对应条目,然后zip()是你的朋友:

for a,b,c in zip(tup1 ,tup2,tup3):

...


Kent



Presuming that you want a,b,c to be corresponding entries from the three tuples, then zip() is your
friend:
for a,b,c in zip(tup1, tup2, tup3):
...

Kent


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

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