【问题标题】:Python: How to slice a list to a certain valuePython:如何将列表切片为某个值
【发布时间】:2012-12-14 06:28:37
【问题描述】:

我想知道是否有办法将现有列表分割成某个很可能不在列表中的数字。例如,假设我有:

primes = [2,3,5,7]

现在我想测试数字 11 的整洁度。我将通过在 1 + 整数平方根 11 的限制下尝试除以所有素数来做到这一点。因此,一旦元素大于限制,我不会循环遍历列表的所有元素,素数并打破循环,我想知道我是否可以将列表拼接到 Limit 的值。在这种情况下,值将是:

1 + int(sqrt(11)) = 1 + 3 = 4

所以我可以遍历primes[ : up until the value of 4][2,3] 的元素

我知道一些python,但我不确定如何仅使用列表方法来做到这一点......对于高达数十亿的筛法,我可以通过不使用if语句来有效地节省时间......

再次提前致谢!

【问题讨论】:

    标签: python list splice sieve


    【解决方案1】:

    我不太确定我是否完全理解了这个问题(很抱歉,如果没有帮助,我会删除答案)但可能是 bisect 模块(仅适用于排序列表,但如果可行,它会非常快)在这种情况下会有所帮助:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import bisect
    import math
    
    primes = [2,3,5,7] 
    searchedPrime=11
    lookedPosition = 1 + int(math.sqrt(searchedPrime)) 
    
    checkUntil = primes[:bisect.bisect_left(primes, lookedPosition)]
    print "I just have to check %s positions: %s" % (len(checkUntil), checkUntil)
    

    这个输出

    I just have to check 2 positions: [2, 3]
    

    因此,sqrt 方法和 bisect 工具的组合可能会帮助您确定要检查的素数范围。

    编辑:

    哦,看那个...我不知道sqrt这个东西适合查找素数...但是看起来它是...

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import bisect
    import math
    
    foundPrimes = [] 
    def isPrime(number, otherPrimes):
      global foundPrimes
      lookedPosition = 1 + int(math.sqrt(number)) 
      formerPrimes = foundPrimes[:bisect.bisect_left(foundPrimes, lookedPosition)]
      for prime in formerPrimes:
        if prime > 1 and number % prime == 0:
          return False
      return True
    
    def getPrimes(upperLimit):
      for i in range(1, upperLimit):
          if isPrime(i, foundPrimes):
            foundPrimes.append(i)
      return foundPrimes
    
    print "Primes: %s" % getPrimes(1000)
    

    这个输出:

    Primes: [1, 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
    

    ...在我看来很漂亮primy...:-)

    P.S.:不要那样使用那个代码……太垃圾了。

    【讨论】:

    • 没有?哇...我不知道...然后从 2 而不是 1 开始范围:D
    【解决方案2】:

    [i for i in primes if i < 4]

    【讨论】:

    • 该解决方案有效,但它会评估每个元素。如果我们可以安全地假设 OPs 素数列表是按升序排序的,我们希望在到达小于或等于 4 的元素后停止。不需要评估 7。
    【解决方案3】:

    也许itertools.takewhile

    >>> from itertools import takewhile
    >>> primes
    [2, 3, 5, 7]
    >>> b = takewhile(lambda k : k < 4, primes)
    >>> b
    <itertools.takewhile object at 0x0200F418>
    >>> for ele in b:
    ...     print ele
    ... 
    2
    3
    

    itertools.takewhile 返回一个生成器。循环生成器比循环列表更快,但请注意,生成器在循环后会耗尽。

    【讨论】:

      【解决方案4】:

      完整代码如下:

      from math import sqrt
      
      primes = []
      for i in range(2, maxvalue):
          for p in primes:
              if p < (1 + int(sqrt(i))) and i % p == 0:
                  break
          else:
              primes.append(i)
      

      这是更新的防错功能。对于maxvalue = 100,它输出:

      [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]
      

      我会注意到可能还有其他简短的方法,但我会说这是迄今为止人类可读性最强的方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-16
        • 2012-06-14
        • 1970-01-01
        • 2017-06-24
        • 2015-02-19
        • 2014-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多