简体   繁体   English

append() function 是否将索引和值都添加到另一个列表?

[英]Does append() function add both index and value to another list?

Heres the code I'm trying to understand:这是我试图理解的代码:

The code is the implementation of Sieve of Erastothenes.代码是 Erastothenes 筛法的实现。 If I understand it correctly, then up to line 8, the code creates a list of prime numbers up to N and the list is a boolean list where True - prime;如果我理解正确,那么直到第 8 行,代码会创建一个最多为 N 的素数列表,该列表是 boolean 列表,其中 True - prime; False - non-prime; False - 非素数; and the index number matches with the number we output.并且索引号与我们output的编号匹配。

My question is: For lines 9-13 does the script "rewrite" the primes (which are just True values) with both the index and the boolean value in a new list in order for us to print them at the end?我的问题是:对于第 9-13 行,脚本是否使用新列表中的索引和 boolean 值“重写”素数(它们只是 True 值),以便我们在最后打印它们? Also, if primes is a boolean list, why python prints the index values at the end instead of just a bunch of True arguments?此外,如果素数是 boolean 列表,为什么 python 会在末尾打印索引值而不是一堆 True arguments?

Update: I got it, thanks guys, range is a set of integers from 2 - n+1, hence i is integer as well.更新:我明白了,谢谢大家,范围是一组从 2 到 n+1 的整数,因此 i 也是 integer。 thats why I get integers in the primes string.这就是为什么我在素数字符串中得到整数。 For some reason I was thinking of range as list1 initialy出于某种原因,我最初认为 range 是 list1

def sita(N):
    list1 = [True for _ in range(N + 1)]
    list1[0:1] = [False, False]
    for start in range(2, N + 1):
        if list1[start]:
            for i in range(2 * start, N + 1, start):
                list1[i] = False
    primes = []  #create empty list
    for i in range(2, N + 1):
        if list1[i]:
            primes.append(i)
    return primes
print(sita(int(input("Dati un numar N: "))))

primes.append(i) only appends its argument, i . primes.append(i)仅附加其参数i There is no magic that would additionally append anything else to the list.没有魔法可以将 append 其他任何东西添加到列表中。

In your code, there are two separate, independent lists: list1 and primes :在您的代码中,有两个独立的列表: list1primes

  • The first contains a boolean for each non-negative integer.第一个包含一个 boolean 用于每个非负 integer。

  • The second contains those integers for which list1 contained True at the end of the first loop (this is what the if list1[i] check does).第二个包含那些在第一个循环结束时list1包含True的整数(这就是if list1[i]检查所做的)。

Your loop, for i in range(2, N + 1): is looping over the actual indices.您的循环for i in range(2, N + 1):正在遍历实际索引。 It tests the boolean value in list1[i] , but only stores the index.测试list1[i]中的 boolean 值,但只存储索引。 The boolean value ( list1[i] ) need not be written; boolean 值 ( list1[i] ) 无需写入; the mere fact that you append ed i indicates that the test passed, and i is known to correspond to a prime number.append编辑i的事实表明测试通过,并且已知i对应于素数。 Since primes is built from scratch, it contains no booleans at all, just the various i values (tested and found prime) append ed to it in the final loop.由于primes是从头开始构建的,因此它根本不包含布尔值,只有各种i值(经过测试并找到素数) append在最终循环中对其进行了编辑。

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

相关问题 将值添加/附加到列表中的特定索引 - Add/Append value to specific index in a list append 使用与另一个 ndarray 索引匹配的索引到列表的第 i 个值 - append ith value to a list using index that matches another ndarray index 在缺失值索引上附加一个列表 - Append a list on missing value index 当两个列表作为参数传递给函数时,无法将列表附加到另一个列表 - Cannot append a list to another list when both lists are passed into function as parameters 无法使用函数装饰器将值附加到来自另一个文件的列表 - Cannot append a value to a list from another file using a function decorator Python:将一个列表的索引与另一个列表进行比较,将第二个列表的值附加到第一个列表 - Python: compare one list's index to another, append second list value to first list 将列表值追加到另一个列表中的相应值 - Append list value to corresponding value in another list Python索引错误(意外)列表追加功能 - Python Index Error (unexpected) List append function 在 append 中查找项目的索引值 - find index value of item in a append an ithem to the list Python - 循环将列表 append 中的值递增到另一个列表,只需将总和的总数相加 - Python - loop to increment a value in list append to another list just add sum's total
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM