472,353 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Python Dict problems

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
1 1366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 385 open (+21) / 3790 closed (+21) / 4175 total (+42) Bugs : 1029 open (+43) / 6744...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.