【发布时间】:2015-03-09 07:23:33
【问题描述】:
从以下 url "https://cfstatic.org/static/words.txt" 将字典作为列表加载到 python 中。使用这个单词列表,创建一个 python 字典(如果你使用的是 php,则创建一个数组)
具有以下属性:
键:字母 [a-z]
value:字典中以该字母开头的单词数
我希望结果为
a : number of words start with a
b : number of words start with b
[...]
z : number of words start with z
我做了以下,
import urllib2 # the lib that handles the url stuff
try:
input_file = urllib2.urlopen('https://cfstatic.org/static/words.txt') # it's a file like object and works just like a file
myNames = []
for line in input_file:
myNames.append(line.strip()) #strips the new line in list
print myNames #print the file as list
except urllib2.URLError as e: #raise the exception if url is not found
print "Error Message : %s" %e
else:
print "File reading operation successful!!!"
【问题讨论】:
-
请阅读How do I format my posts using Markdown or HTML?并相应地编辑您的问题。
-
请提问。这是一个问答网站。
-
那么,你在哪里卡住了?您是否尝试过创建字典?
标签: python list dictionary key-value counting