By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Python Dict problems

P: n/a
Hello people,
iam finding great difficulty in handling a two-dimentional dictionary or any
other similar data structure for the following purpose:
from a collection of words,i need to have the count of each word so if i
use:
str=f.getline()
for l in range(flen):
if subs[l].has_key(str):
subs[l][str]=1
else:
subs[l][str]+=1

for this iam getting an error as "Type error:cannot concatenate str + int"
i would be very grateful to the python-list if any one of you could kindly
help me in solving this problem.

__________________________________________________ _______________
Easiest Money Transfer to India . Send Money To 6000 Indian Towns.
http://go.msnserver.com/IN/42198.asp Easiest Way To Send Money Home!
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
venu gopal wrote:
iam finding great difficulty in handling a two-dimentional dictionary or
any other similar data structure for the following purpose:
from a collection of words,i need to have the count of each word so if i
use:
str=f.getline()
for l in range(flen):
if subs[l].has_key(str):
subs[l][str]=1
else:
subs[l][str]+=1

If it is a smalish file::

content = f.read()
result = {}
for word in words:
result[word] = content.count(word)

For big files::

result = {}
for l in f:
for word in words:
result[word] = result.get(word, 0) + l.count(word)
There are probably faster methods using re, where the counting is done
by the reg-ex engine.
regards Max M
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.