Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中迭代元组_Python_Loops_Tuples - Fatal编程技术网

在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中,您不应该(几乎)迭代索引。