我想通过迭代两个不同的for循环来构建一个字典:
我的代码是:
from bs4 import BeautifulSoup
from xgoogle.search import GoogleSearch, SearchError
try:
gs = GoogleSearch("search query")
gs.results_per_page = 50
results = gs.get_results()
for res in results:
print res.title.encode("utf8")
print res.url.encode("utf8")
print
except SearchError, e:
print "Search failed: %s" % e
这段代码为找到的每个页面输出一个标题和一个url
我想得到以下输出
{title1:url1, title50,url50}
解决这个问题的简洁方法是什么?你知道吗
谢谢!你知道吗
如果需要多个值,则需要一个容器;如果有重复键,则需要collections.defaultdict或dict.setdefault:
我不确定钥匙应该是什么,但逻辑是一样的。你知道吗
如果您的预期输出实际上是不正确的,并且您只想将每个键
t
与单个值配对,那么只需使用普通dict:相关问题 更多 >
编程相关推荐