我正在从这里修改Eratosthenes的无限筛子,所以它使用轮子分解来跳过更多的复合材料而不是当前检查所有可能性的形式.
我已经弄清楚如何生成步骤以达到车轮上的所有间隙.从那里我想我可以用+ 2代替这些轮子步骤,但它导致筛子跳过素数.这是代码:
from itertools import count, cycle
def dvprm(end):
"finds primes by trial division. returns a list"
primes=[2]
for i in range(3, end+1, 2):
if all(map(lambda x:i%x, primes)):
primes.append(i)
return primes
def prod(seq, factor=1):
"sequence -> product"
for i in seq:factor*=i
return factor
def wheelGaps(primes):
"""returns list of steps to each wheel gap
that start from the last value in primes"""
strtPt= primes.pop(-1)#where the wheel starts
whlCirm= prod(primes)# wheel's circumference
#spokes are every number that are divisible …Run Code Online (Sandbox Code Playgroud) python primes infinite sieve-of-eratosthenes wheel-factorization
我正在压缩文件。单个过程对其中一些来说没问题,但我正在压缩数千个,这可能(并且已经)需要几天时间,所以我想通过多处理来加速它。我读过我应该避免让多个进程同时读取文件,我猜我也不应该让多个进程同时写入。这是我当前单独运行的方法:
import tarfile, bz2, os
def compress(folder):
"compresses a folder into a file"
bz_file = bz2.BZ2File(folder+'.tbz', 'w')
with tarfile.open(mode='w', fileobj = bz_file) as tar:
for fn in os.listdir(folder):
read each file in the folder and do some pre processing
that will make the compressed file much smaller than without
tar.addfile( processed file )
bz_file.close()
return
Run Code Online (Sandbox Code Playgroud)
这是获取一个文件夹并将其所有内容压缩到一个文件中。这使它们更易于处理和更有条理。如果我只是把它扔进一个池子里,那么我会有几个进程同时读取和写入,所以我想避免这种情况。我可以修改它,所以只有一个进程正在读取文件,但我仍然有多个进程在写:
import multiprocessing as mp
import tarfile, bz2, os
def compress(file_list):
folder = file_list[0]
bz_file = bz2.BZ2File(folder+'.tbz', 'w')
with tarfile.open(mode='w', fileobj = bz_file) …Run Code Online (Sandbox Code Playgroud) 我知道浮点数包含大量的数字和一些基数的指数,通常为十.因此,通过有效数字的任何东西都不会准确,因为没有任何东西,但是当将float转换为int(或long)时,会出现虚假数字.
这是一个Python示例:
>>> int(1e308)
100000000000000001097906362944045541740492309677311846336810682903157585404911491537163328978494688899061249669721172515611590283743140088328307009198146046031271664502933027185697489699588559043338384466165001178426897626212945177628091195786707458122783970171784415105291802893207873272974885715430223118336L
Run Code Online (Sandbox Code Playgroud)
当一遍又一遍地给出相同的值时,它们似乎没有改变,所以它可能不是完全随机的或从记忆中的神秘位置拉出来的.这些随机数字来自哪里?究竟是什么产生这些尾随数字?为什么转换只是将随机数设置为零?
我有一个函数,可以返回给定位置的日出,日落,太阳正午和黄昏时间。该函数使用pyephem,由于不推荐使用,我想我会重新编写该函数以使用skyfield。然而,skyfield不具备的功能.previous_rising,.next_setting或.next_transit(至少,我找不到它们),这是我与pyephem使用。
skyfield确实具有function find_discrete,它将在给定时间之间进行搜索以查找函数何时发生更改,因此我编写了以下代码进行测试:
from datetime import datetime, timedelta
from skyfield import api, almanac
import pytz
ts = api.load.timescale()
planets = api.load('de421.bsp')
sun, earth = planets['sun'], planets['earth']
lat, lon = '44.59028 N', '104.71528 W' # UFO Mooring Site
tzn, elv = 'US/Mountain', 1559
tz = pytz.timezone(tzn)
loc = api.Topos(lat, lon, elevation_m=elv)
earth_loc = earth + loc
def civil_twil(time):
"returns true/false if sun is above -6 degrees"
alt, az, dis = earth_loc.at(time).observe(sun).apparent().altaz()
return …Run Code Online (Sandbox Code Playgroud) 我在Python中混合了两个列表,虽然我知道有类似的问题,但我需要以某种方式处理它.
我有两个列表,它们的长度可能相同,也可能不同.我需要交织两者并使用填充物作为较短的列表,但填充物必须在较短列表中的任何值之前.
这就是我目前拥有的:
def mix(a, b):
fill='spam'
if len(a) > len(b):
d = len(a) - len(b)
while d:
b.insert(0, fill)
d-=1
if len(a) < len(b):
d = len(b) - len(a)
while d:
a.insert(0, fill)
d-=1
n = zip(a, b)
mixed = [item for sub in n for item in sub]
return mixed
Run Code Online (Sandbox Code Playgroud)
例:
>>> mix([1, 2 ,3], ['A', 'B', 'C', 'D', 'E', 'F'])
['spam', 'A', 'spam', 'B', 'spam', 'C', 1, 'D', 2, 'E', 3, 'F']
Run Code Online (Sandbox Code Playgroud)
所以代码可以工作,但它似乎不是一个好方法.有没有更好的办法?