简体   繁体   English

复制枚举函数的功能而不使用它

[英]Replicating the functions of the enumerate function without using it

I have been asked to replicate the enumerate function. 我被要求复制枚举函数。 That is, when a list of int or string in given, the output should be a list of tuples for each element with its index. 也就是说,当给定int或string的列表时,输出应该是每个元素及其索引的元组列表。

list entered = [1, 2, 3, 4] 输入的列表= [1、2、3、4]

expected outcome = [(0, 1), (1, 2), (2, 3), (3, 4)] 预期结果= [(0,1),(1,2),(2,3),(3,4)]

It's doable for the most part. 在大多数情况下这是可行的。 The problem occurs when an element is repeated. 当重复一个元素时,会发生此问题。

This is my code: 这是我的代码:

def my_enumerate(items):
    """ return tuples with index and number"""
    tuple_list = []
    for i in items:
        tuple_list.append((items.index(i), i))
    return tuple_list

When a list containing repeated elements in entered, 输入包含重复元素的列表时,

ans = my_enumerate(['x', 'x', 'x'])
print(ans)

expected outcome : [ (0, 'x'), (1, 'x'), (2, 'x') ] 预期结果:[(0,'x'),(1,'x'),(2,'x')]

actual outcome : [ (0, 'x'), (0, 'x'), (0, 'x') ] 实际结果:[(0,'x'),(0,'x'),(0,'x')]

What changes should I do to my code? 我应该对我的代码做哪些更改?

Thank you 谢谢

Your problem is that elements can repeat but indices are unique. 您的问题是元素可以重复,但索引是唯一的。 Also, looking for an index according to the element is generally O(n) while the whole point of lists is that acquiring an element according to its index, is O(1) . 同样,根据元素查找索引通常为O(n)而列表的全部要点是根据元素索引获取元素为O(1)

So, instead of iterating elements, iterate indices and get the elements: 因此,代替迭代元素,迭代索引并获取元素:

def my_enumerate(items):
    tuple_list = []
    for i in range(len(items)):
        tuple_list.append((i, items[i]))
    return tuple_list

Or more neatly: 或更整洁地:

def my_enumerate(items):
    return [(i, items[i]) for i in range(len(items))]

And both gives: 两者都给出:

>>> my_enumerate([1, 2, 3, 4])
[(0, 1), (1, 2), (2, 3), (3, 4)]

>>> my_enumerate(['x', 'x', 'x'])
[(0, 'x'), (1, 'x'), (2, 'x')]

To make it more inline with the generator-y spirit of enumerate we could respectively change the function to: 为了使其更符合enumerate的生成器精神,我们可以分别将函数更改为:

def my_enumerate(items):
    for i in range(len(items)):
        yield (i, items[i])

And: 和:

def my_enumerate(items):
    yield from ((i, items[i]) for i in range(len(items)))

Now you can iterate over it just as regular enumerate , or if you want it as a list simply do: list(my_enumerate(items)) 现在,您可以像常规enumerate一样对其进行迭代,或者如果您希望将其作为列表进行访问,则只需执行以下操作: list(my_enumerate(items))

You could use zip if you want: 如果需要,可以使用zip

def my_enumerate(items):
    return list(zip(range(len(items)),items))

though perhaps if you are reproducing (some of) the functionality of enumerate in terms of more primitive elements, using zip might be against the spirit of the problem (in which case Tomerikoo's solution is preferable). 尽管也许如果您要使用更原始的元素来重现enumerate的功能(某些功能),则使用zip可能与问题的实质背道而驰(在这种情况下,最好使用Tomerikoo的解决方案)。 Nevertheless, it doesn't hurt to know multiple ways of solving a problem. 但是,知道解决问题的多种方法并没有什么坏处。

Your problem is index is always finding the index of the first occurrence of 'x' . 您的问题是index始终在查找首次出现的'x'的索引。 An idea would be to use an int index instead of the index method. 一个想法是使用int索引而不是index方法。

for i in range(len(items)):
    tuple_list.append((i, items[i]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM