使用in运算符和更改列表

时间:2015-11-10 03:10:45

标签: python list

我试图制作一个表格,其中一行的数字增加一个,而另一行的相应复合材料的限制为100,如:

Numbers----------composites
x---------------numbers 1-100 divisible by x
x+1---------------numbers 1-100 divisible by x+1 but aren't in x
x+2---------------numbers 1-100 divisible by x+2 but aren't in x or x+1    x+3---------------numbers 1-100 divisible by x+3 but aren't in x,x+1,or x+2    etc

Numbers是一个永久列表,以2-100开始,因为我在函数中提取每个复合数字,然后它应该只包含素数。 composites是一个列表,我填写了一定数量的合成(2,3,4等),然后我希望用当前的numbers列表检查,以确保没有重复。我打印剩下的,清空列表并将当前变量增加1并重复 这是我提出的编码,我理解它非常草率但我对这个主题一无所知,我的教授喜欢我们学习火试验,这是我设法从教科书中汲取的东西。我担心的主要问题是添加和删除某些列表中的元素

def main():
    x=2
    n=2
    print("numbers""       ""composite")
    print("------------------------")
    cross_out(n,x)
def cross_out(n,x):
    composites=[]
    prime=[]
    numbers=[]
    while x<101:
        numbers.append(x)
        x=x+1
    x=2
    for x in range(2,102):
        if x==101:
            search=composites[0]
            index=0
            while index<=len(composites):
                if search in numbers:
                    search=search+1
                    index=index+1
                else:
                    if search in composites:
                        composites.remove(search)
                    else:
                        pass
            print(n,"--->",composites)
            x=2
            composites=[]
            n=n+1
            index=0
        elif x%n==0:
            composites.append(x)
            if x in numbers:
                numbers.remove(x)
            else:
                pass
            x=x+1
        else:
            x=x+1



main()
cross_out()

1 个答案:

答案 0 :(得分:0)

我认为我正确地理解了你的描述,这就是我想出来的。

我使用set来跟踪已添加到复合材料中的数字。这使得问题非常简单。此外,编写函数时的建议是不覆盖您的参数。例如,在cross_out中,您多次执行x = <value>n = <value>

def cross_out(n,x):
    composites=[]
    seen = set()
    numbers = range(x, 101)
    primes = []

    for num in numbers:
        for val in numbers:
            if val % num == 0 and val not in seen:
                composites.append(val)
                seen.add(val)
        if composites:
            primes.append(composites[0])
        print(num,'--->',composites)
        composites = []

    print(primes)

def main():
    print("numbers        composite")
    print("------------------------")
    cross_out(2, 2)


main()

样本输出

numbers        composite
------------------------
2 ---> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
3 ---> [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99]
4 ---> []
5 ---> [5, 25, 35, 55, 65, 85, 95]
6 ---> []
7 ---> [7, 49, 77, 91]
8 ---> []
9 ---> []
10 ---> []
11 ---> [11]
12 ---> []
13 ---> [13]
14 ---> []
15 ---> []
16 ---> []
17 ---> [17]
18 ---> []
19 ---> [19]
20 ---> []
21 ---> []
22 ---> []
23 ---> [23]
24 ---> []
25 ---> []

素数

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]