简体   繁体   English

迭代器和数组有什么区别?

[英]what's the difference between iterator and array?

I am trying to write a function that takes a sequence of numbers and determines if all the numbers are different from each other.我正在尝试编写一个函数,该函数采用一系列数字并确定所有数字是否彼此不同。

This was my first attempt这是我的第一次尝试

def differ(data):
    for i in data:
        print(i)
        for j in data:
            print(j)
            if i==j:
                return False
    return True

print(differ([1,23,4,1,2,3,1,2]))
print(differ([1,2,3,4]))
1
1
False
1
1
False

Apparently, the for loop didn't loop over all the numbers in the data.显然,for 循环没有遍历数据中的所有数字。 Why did this happen?为什么会这样?

I wrote another function using range().我使用 range() 编写了另一个函数。

def differ(data):
    for i in range(1,len(data)):
        print("i:",i)
        for j in range(i):
            print("j:",j)
            if data[i]==data[j]:
                return False
    return True
print(differ([1,2,2,4,53]))
i: 1
j: 0
i: 2
j: 0
j: 1
False

It works, however, I don't understand why my first attempt didn't work.它有效,但是,我不明白为什么我的第一次尝试无效。

I think Tim Roberts answer explains perfectly why your code is not working as you expect.我认为 Tim Roberts 的回答完美地解释了为什么您的代码没有按预期工作。 I would like to complement the answer:我想补充答案:

Answering your question回答你的问题

Arrays are similar to lists, one of the differences is that the former consists only of elements with the same data type.数组类似于列表,区别之一是前者仅由具有相同数据类型的元素组成。 In the other hand, iterators are objects which you can iterate through, these objects must implement at least the following 2 methods on their respective classes: __iter__() and __next__()另一方面,迭代器是可以迭代的对象,这些对象必须在各自的类上至少实现以下 2 个方法: __iter__()__next__()

Cool way to do what you want in 1 line在 1 行中做你想做的很酷的方式

You can achieve what you want with this:您可以通过以下方式实现您想要的:

def differ(data):
    return len(set(data)) == len(data)


print(differ([1, 2, 3, 4]))  # True
print(differ([1, 23, 4, 1, 2, 3, 1, 2])) # False

So basically, the magic here happens when you use set() .所以基本上,当你使用set()时,这里的魔法就会发生。 Sets are similar to lists, but one of the main differences is that they can't have repeated elements. 集合类似于列表,但主要区别之一是它们不能有重复的元素。 By transforming the list to a set you are removing every duplicated element, so if there is a difference in the length after casting to a set , it means there was at least one duplicated element.通过将list转换为set您将删除每个重复的元素,因此如果转换为set后长度存在差异,则意味着至少有一个重复的元素。

The problem with your first attempt is that it will eventually compare the same number with itself.您第一次尝试的问题在于它最终会与自己比较相同的数字。

In the sequence [1,2,3,4] the first function will start off with i=1 and j=1 .在序列[1,2,3,4] ,第一个函数将从i=1j=1 They are both looking at the first number in the list causing it to fail.他们都在查看列表中的第一个数字,导致它失败。

The second attempt avoids this by only looking at the numbers before it.第二次尝试通过只查看它之前的数字来避免这种情况。 range(i) doesn't actually include i , so j can only ever be less than i meaning they will never point to the same value in the list. range(i)实际上并不包含i ,因此j只能小于i这意味着它们永远不会指向列表中的相同值。

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

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