在python中迭代元组
我在遍历由元组组成的列表时遇到一些问题 问题似乎在于在python中迭代元组,python,loops,tuples,Python,Loops,Tuples,我在遍历由元组组成的列表时遇到一些问题 问题似乎在于for循环中的索引 每当我运行程序时,唯一的结果是: ('joao', 300, 20) 谁能跟我解释一下这件事 tuplo = [('joao', 300, 20), ('ana', 80, 15), ('patricia', 17, 90)] def coordenadas(tuplo, name): for index in range(len(tuplo)): if tuplo[index][0
for
循环中的索引
每当我运行程序时,唯一的结果是:
('joao', 300, 20)
谁能跟我解释一下这件事
tuplo = [('joao', 300, 20), ('ana', 80, 15), ('patricia', 17, 90)]
def coordenadas(tuplo, name):
for index in range(len(tuplo)):
if tuplo[index][0] == name:
print(tuplo[index][0:])
else:
return None
coordenadas(tuplo,'joao')
coordenadas(tuplo,'ana')
coordenadas(tuplo,'patricia')
首先,我确信你的意思是在你的片中使用
0
而不是o
print(tuplo[indice][o:])
=>print(tuplo[indice][0:])
您的问题是您正在使用return
这将退出您的函数。相反,您应该使用continue
tuplo = [('joao',300,20),('ana',80,15),('patricia',17,90)]
def coordenadas(tuplo,nome):
for indice in range(len(tuplo)):
if tuplo[indice][0] == nome:
print(tuplo[indice][0:])
else:
continue
coordenadas(tuplo,'joao')
coordenadas(tuplo,'ana')
coordenadas(tuplo,'patricia')
输出:
('joao', 300, 20)
('ana', 80, 15)
('patricia', 17, 90)
如果我要写这个函数,我会做其他事情: 首先,我不会在函数内部和外部为tuplo使用相同的名称。 其次,我将迭代列表中的项,而不是它们的索引(这是在python中迭代的正确方法) 第三,我将优化此功能:
global_tuplo = [('joao',300,20),('ana',80,15),('patricia',17,90)]
def coordenadas(tuplo,nome):
for tup in tuplo:
if tup[0] == nome:
print(tup)
coordenadas(global_tuplo, 'joao')
coordenadas(global_tuplo, 'ana')
coordenadas(global_tuplo, 'patricia')
删除
else
并将[o:][/code>更改为[0]
可以使代码正常工作
tuplo = [('joao',300,20),('ana',80,15),('patricia',17,90)]
def coordenadas(tuplo,nome):
for indice in range(len(tuplo)):
if tuplo[indice][0] == nome:
print(tuplo[indice][0])
coordenadas(tuplo,'joao')
coordenadas(tuplo,'ana')
coordenadas(tuplo,'patricia')
如果您想要名称详细信息,请将tuplo[indice][0]
更改为tuplo[indice]
此代码
else:
return None
如果if
测试未成功,则中断for
循环,因此如果名称与列表中的第一个元组不匹配,则不会测试其他名称。你不会想要的
而且,你不需要这样做
tuplo[index][0:]
你可以这么做
tuplo[index]
这是您的代码的修复版本
tuplo = [
('joao', 300, 20),
('ana', 80, 15),
('patricia', 17, 90),
]
def coordenadas(tuplo, nome):
for indice in range(len(tuplo)):
if tuplo[indice][0] == nome:
print(tuplo[indice])
coordenadas(tuplo, 'joao')
coordenadas(tuplo, 'ana')
coordenadas(tuplo, 'patricia')
输出
('joao', 300, 20)
('ana', 80, 15)
('patricia', 17, 90)
joao ('joao', 300, 20)
ana ('ana', 80, 15)
patricia ('patricia', 17, 90)
tom None
顺便说一句,如果您只想找到第一个匹配的元组,可以在if
块的末尾添加break
或return
语句,如下所示:
def coordenadas(tuplo, nome):
for indice in range(len(tuplo)):
if tuplo[indice][0] == nome:
print(tuplo[indice])
break
然而,有更好的方法来完成这项任务。在Python中,最好直接迭代集合中的项,而不是通过索引间接迭代:
def coordenadas(tuplo, nome):
for t in tuplo:
if t[0] == nome:
print(t)
break
一种更有效的方法是将列表转换为字典,特别是当您有许多元组时。例如:
tuplo = [
('joao', 300, 20),
('ana', 80, 15),
('patricia', 17, 90),
]
tuplo_dict = {t[0]: t for t in tuplo}
def coordenadas(data, nome):
print(nome, data.get(nome))
coordenadas(tuplo_dict, 'joao')
coordenadas(tuplo_dict, 'ana')
coordenadas(tuplo_dict, 'patricia')
coordenadas(tuplo_dict, 'tom')
输出
('joao', 300, 20)
('ana', 80, 15)
('patricia', 17, 90)
joao ('joao', 300, 20)
ana ('ana', 80, 15)
patricia ('patricia', 17, 90)
tom None
这使用了更多的RAM,但它比以前的版本效率更高,因为在字典中搜索条目的速度非常快。尝试:|您期望发生什么?在[o:
中的切片和字母o
应该做什么?您提供的代码未运行(name错误:未定义名称“o”)。如果可能的话,我会给你另一个+1来解释更长的解释谢谢你的帮助!我没有返回None,因为这是老师给我的练习中指定的:/相同的名称只是为了让跑步更容易。。。迭代也是我的老师认为我们迭代元组的过程:/@MiguelLuís所有函数默认返回None
。而且,在Python中,您不应该(几乎)迭代索引。