迭代python中的元组

时间:2016-12-28 13:49:36

标签: 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] == name:
            print(tuplo[index][0:])
        else:
            return None

coordenadas(tuplo,'joao')
coordenadas(tuplo,'ana')
coordenadas(tuplo,'patricia')

3 个答案:

答案 0 :(得分:4)

首先,我确定您的意思是在切片中使用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)

如果我要写这个函数,我会做其他的事情:

首先,我不会在函数内部和外部使用相同的名称。 其次,我将迭代列表中的项而不是它们的索引(这是在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')

答案 1 :(得分:2)

删除else并将[o:]更改为[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]

答案 2 :(得分:0)

此代码

else:
    return None
如果for测试没有成功,

会突破if循环,因此如果名称与列表中的第一个元组不匹配,则不会测试其他名称。你不想要那个!

另外,你不需要做

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)

顺便说一句,如果您只想找到第一个匹配的元组,可以在break块的末尾添加returnif语句,如下所示:

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 ('joao', 300, 20)
ana ('ana', 80, 15)
patricia ('patricia', 17, 90)
tom None

这使用了更多的RAM,但它比以前的版本更有效,因为在字典中搜索项目非常快。

相关问题