相关疑难解决方法(0)

获得两个列表之间的区别

我在Python中有两个列表,如下所示:

temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Run Code Online (Sandbox Code Playgroud)

我需要创建第三个列表,其中包含第一个列表中不存在于第二个列表中的项目.从我必须得到的例子:

temp3 = ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)

有没有循环和检查的快速方法?

python performance list set set-difference

731
推荐指数
24
解决办法
70万
查看次数

为什么set()使这段代码运行得更快?

我为Project Euler 问题35写了一些代码:

#Project Euler: Problem 35

import time

start = time.time()

def sieve_erat(n):
    '''creates list of all primes < n'''
    x = range(2,n)
    b = 0
    while x[b] < int(n ** 0.5) + 1:
        x = filter(lambda y: y % x[b] != 0 or y == x[b], x)
        b += 1
    else:
        return x

def circularPrimes(n):
    '''returns # of circular primes below n'''
    count = 0
    primes = sieve_erat(n)
    b = set(primes)
    for prime in primes:
        inc = …
Run Code Online (Sandbox Code Playgroud)

python performance set

2
推荐指数
1
解决办法
172
查看次数

标签 统计

performance ×2

python ×2

set ×2

list ×1

set-difference ×1