[英]How can I prevent this from returning a long list of None values?
This script determines which websites out of a possible number of websites has the data that I want. 该脚本确定了可能数量的网站中的哪些网站具有我想要的数据。 z
runs all of the potential websites through data_grabber(item)
, which returns a list of the indexes of all the valid URLs. z
通过data_grabber(item)
运行所有潜在网站,该网站返回所有有效URL的索引列表。 It also includes None
values for the websites that weren't valid. 它还包含无效网站的“ None
值。
import requests
a = range(0, 10)
b = 'http://www.grandexchangecentral.com/include/gecgraphjson.php?jsid=%r'
websites = []
for i in a:
result = b % a[i]
websites.append(result)
def data_grabber(item):
url = item
r = requests.get(url, headers={'Referer': 'www.grandexchangecentral.com'})
data = r.json
if data != None:
return websites.index(item)
z = [data_grabber(x) for x in websites]
z1 = filter(None, z)
This returns [None, None, 2, None, None, None, 6, None, 8, None]
, which z1
shortens to [2, 6, 8]
. 这将返回[None, None, 2, None, None, None, 6, None, 8, None]
,其中z1
缩短为[2, 6, 8]
z1
[2, 6, 8]
。 The only problem with this is that the list of possible websites can get very long, and so it can take a few minutes to generate many None
placeholders in z
. 唯一的问题是,可能的网站列表可能会很长,因此可能要花费几分钟才能在z
生成许多None
占位符。 Would it be possible to make prevent z
from generating None
items? 可以防止z
生成None
项目吗? Thanks in advance for any help. 在此先感谢您的帮助。
EDIT: Now I realize that it's actually data_grabber
taking most of the time. 编辑:现在我意识到,实际上大部分时间都是data_grabber
。 Still nice to know this though. 仍然很高兴知道这一点。
I agree with @DSM that it seems unlikely this is costing you too much time. 我同意@DSM,这似乎不太可能花费您太多时间。 But this will avoid that: 但这可以避免:
filter(None, (data_grabber(x) for x in websites))
Using the parentheses instead of brackets will make a generator of data_grabber
results, which filter
will then consume, building up the result list without ever making the intermediate list with None
s in it. 使用括号而不是括号将生成data_grabber
结果的生成器,然后该filter
将使用该filter
,从而建立结果列表,而无需在中间列表中添加None
。
Just a test of your claim about timings: 只是测试一下您对计时的要求:
>>> %timeit filter(None, [None for x in range(100000)])
100 loops, best of 3: 9.22 ms per loop
Not exactly the same thing, but this makes a list of 100,000 None
s and then filters them all out in 9 milliseconds on my computer. 并非完全相同,但这会列出100,000个None
,然后在9毫秒内将它们全部过滤掉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.