[英]Python Counter doesn't count
import urllib2
f=urllib2.urlopen("http://www.mbnet.com.pl/dl.txt")
list = range(1,50)
counter={}
for lines in f:
tab_lines=lines.split(" ")
formated_tab=tab_lines[-1].strip().split(',')
#print formated_tab
for i in formated_tab:
if i in list:
counter[i]+=1
print counter.items()
My counter
doesn't work and I don't know why :( 我的
counter
不工作,我也不知道为什么:(
This is a list of lottery numbers. 这是彩票号码的列表。 I would like count how many times drawn each number.
我想算一下每个数字画了多少次。
You are comparing strings with integers . 您正在将字符串与整数进行比较。 Your
if
test never matches: 您的
if
测试永远不匹配:
if i in list:
because each i
is a string. 因为每个
i
都是一个字符串。 Your list
variable on the other hand, contains integers: 另一方面,您的
list
变量包含整数:
list = range(1,50)
Convert i
to an integer to test against other integers: 将
i
转换为整数以针对其他整数进行测试:
if int(i) in list:
Some other remarks: 其他一些说明:
list
is not a good variable name; list
不是一个好的变量名; you are masking the built-in type . 您正在掩盖内置类型 。
You could just test if i
falls in a range by using comparison operators against the start and end values: 您可以通过使用比较运算符针对开始和结束值来测试
i
是否在范围内:
if 1 <= int(i) < 50:
which would be faster as you don't have to scan through the list each time. 由于您不必每次都扫描列表,因此速度会更快。
You cannot assume that the key is already present in counter
. 您不能假定该密钥已经存在于
counter
。 You'd have to test first or use counter.get()
to return a default. 您必须先进行测试或使用
counter.get()
返回默认值。 For example: 例如:
counter[i] = counter.get(i, 0) + 1
To count your values, you could use the standard library collections.Counter()
class: 要计算值,可以使用标准库
collections.Counter()
类:
from collections import Counter counter = Counter() for lines in f: tab_lines = lines.split() # note, no argument! formatted_tab = map(int, tab_lines[-1].split(',')) counter.update(i for i in formatted_tab if 0 < i < 50) print counter.most_common()
In my testing, I didn't see any numbers in that file that where outside the range of 0 to 50 (exclusive), so you can probably get away with just counter.update(formatted_tab)
. 在我的测试中,我没有看到该文件中的任何数字都在0到50(不包括)范围之外,因此您可能只需要使用
counter.update(formatted_tab)
。
Apart from Martin Pieters' answer there is another problem. 除了马丁·彼得斯的回答,还有另一个问题。 You're accessing a dictionary key that doesn't exist in the dictionary.
您正在访问字典中不存在的字典键。 Instead of
代替
counter[i]+=1
you should use something like 你应该使用类似
counter[i] = counter.get(i, 0) + 1
So much thank you guys ! 非常感谢你们!
I finished this code :) ! 我完成了这段代码:)!
import urllib2
f=urllib2.urlopen("http://www.mbnet.com.pl/dl.txt")
list = range(1,50)
counter={}
for lines in f:
tab_lines=lines.split(" ")
formated_tab=tab_lines[-1].strip().split(',')
for i in formated_tab:
if int(i) in list:
counter[i] = counter.get(i, 0) + 1
sumall=sum(counter.values())
for number, value in counter.items():
print ('Number {} drawn {} times and it is {}% of all ').format(number,value,100*value/sumall)
Instead of using: 而不是使用:
counter[i] = counter.get(i, 0) + 1
You can also try collections.defaultdict
: 您也可以尝试
collections.defaultdict
:
counter = defaultdict(int)
So you final version should be look like this: 因此,您的最终版本应如下所示:
import urllib2
from collections import defaultdict
f=urllib2.urlopen("http://www.mbnet.com.pl/dl.txt")
list = range(1,50)
counter=defaultdict(int) # use defaultdict here
for lines in f:
tab_lines=lines.split(" ")
formated_tab=tab_lines[-1].strip().split(',')
for i in formated_tab:
if int(i) in list:
counter[i] += 1 # don't worry, be happy :)
sumall=sum(counter.values())
for number, value in counter.items():
print ('Number {} drawn {} times and it is {}% of all').format(number,value,100*value/sumall)
I'll give you an example to show what collections.defaultdict
does here: 我将举一个例子来说明
collections.defaultdict
在这里的作用:
>>> from collections import defauldict
>>> a = {}
>>> a['notexist']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'notexist'
>>> b = defaultdict(int)
>>> b['notexist']
0
class collections.defaultdict([default_factory[, ...]])
defaultdict is a subclass of the built-in dict class, so don't be scared, but you can do more with it. class collections.defaultdict([default_factory[, ...]])
defaultdict是内置dict类的子类,因此不要害怕,但是您可以使用它做更多的事情。 Once you specified the default_factory
variable, when the key is not exist, defaultdict
will supply one for you according to the default_factory
. 一旦指定了
default_factory
变量,当键不存在时, defaultdict
将根据default_factory
为您提供一个。 Note this magic will only happen when you using dict['key']
or dict.__getitem__(key)
. 注意,只有当您使用
dict['key']
或dict.__getitem__(key)
时,这种魔术才会发生。
The doucumetaion is here: collections.defaultdict doucumetaion在这里: collections.defaultdict
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.