如何仅对元组的第一个变量进行迭代 [英] How can I iterate over only the first variable of a tuple

查看:91
本文介绍了如何仅对元组的第一个变量进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,当您有一个元组列表时,可以遍历它们.例如,当您有3d点时:

In python, when you have a list of tuples, you can iterate over them. For example when you have 3d points then:

for x,y,z in points:
    pass
    # do something with x y or z

如果只想使用第一个变量,或者第一个和第三个变量,该怎么办. python中是否有任何跳过符号?

What if you only want to use the first variable, or the first and the third. Is there any skipping symbol in python?

推荐答案

是否可以防止您不接触您不感兴趣的变量?在Python中,通常使用下划线来表示您不感兴趣的变量.例如:

Is something preventing you from not touching variables that you're not interested in? There is a conventional use of underscore in Python to indicate variable that you're not interested. E.g.:

for x, _,_ in points:
    print(x)

您需要了解这只是一个约定,与性能无关.

You need to understand that this is just a convention and has no bearing on performance.

这篇关于如何仅对元组的第一个变量进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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