Calgarypuck Forums - The Unofficial Calgary Flames Fan Community

Go Back   Calgarypuck Forums - The Unofficial Calgary Flames Fan Community > Main Forums > The Off Topic Forum > Tech Talk
Register Forum Rules FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 12-06-2012, 10:41 AM   #1
mykalberta
Franchise Player
 
mykalberta's Avatar
 
Join Date: Aug 2005
Location: Calgary
Exp:
Default Anyone know how to code Python?

I am trying to help my cuz who is taking it in an adv High School programming class. I have only ever taken Java and C++ so Python is sort of new to me and doesnt seem as structured

He has the following data in different text files: Calgary, Edmonton, etc

FNameLName community CommunityName (all on 1 line all in this same format)

JamesPuck community Evergreen
RobNewsome community Varsity

He wants to parse the list and get rid of the community placeholder so he can use the dictionary function to compare and count

People who live in Evergreen: JamesPuck, ...
Number of people who live in Evergreen: X
Number of peple who live in textfile_Calgary: X

We have got this far:

import text ('data.txt','r')
words=text.split() #words list
print(words)

This gets us a list: 'JamesPuck', 'community', 'Evergreen', 'RobNewsome', 'Community', 'Mulletville'

He wants to use dictionary but with community we cant seem to remove it from the list. If we can then we can simply compare Name to CommunityName

Any tips or help would be appreciated
__________________
MYK - Supports Arizona to democtratically pass laws for the state of Arizona
Rudy was the only hope in 08
2011 Election: Cons 40% - Nanos 38% Ekos 34%
mykalberta is offline   Reply With Quote
Old 12-06-2012, 11:11 AM   #2
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Just filter it out in the split:
words=text.split(" community ")
BlackEleven is offline   Reply With Quote
Old 12-06-2012, 11:25 AM   #3
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

And a tip to get you a little further: you'll want to turn that list into a dictionary to answer those questions you posted. I'll spoiler it in case you want to figure it out on your own.

Spoiler!

Last edited by BlackEleven; 12-06-2012 at 11:33 AM.
BlackEleven is offline   Reply With Quote
The Following User Says Thank You to BlackEleven For This Useful Post:
Old 12-06-2012, 11:30 AM   #4
comrade
Crash and Bang Winger
 
Join Date: Aug 2004
Exp:
Default

Quote:
Originally Posted by BlackEleven View Post
Just filter it out in the split:
words=text.split(" community ")
Won't that result in ["JamesPuck", "Evergreen RobNewsome", "Varsity"]?
comrade is offline   Reply With Quote
Old 12-06-2012, 11:51 AM   #5
mykalberta
Franchise Player
 
mykalberta's Avatar
 
Join Date: Aug 2005
Location: Calgary
Exp:
Default

We tried that, the default for split() removes whitespaces and \n, if you replace then it wont remove the whitespaces and \n

If we replace () with (' community ') then we get something weird like this:

Orig Data
JamesPuck community Evergreen
RobNewsome community Varsity

split() = 'JamesPuck', 'community', 'Evergreen', 'RobNewsome', 'Community', 'Varsity'
split(' community ') = 'JamesPuck ',' Evergreen/nRobNewsome ',' Varsity/n'
__________________
MYK - Supports Arizona to democtratically pass laws for the state of Arizona
Rudy was the only hope in 08
2011 Election: Cons 40% - Nanos 38% Ekos 34%
mykalberta is offline   Reply With Quote
Old 12-06-2012, 12:42 PM   #6
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Oh right, didn't think about the newlines.

Perhaps the easiest way is just to iterate line by line. This will give you something to start on:
Code:
communityDict = {}
communityFile = open( filePath, "r" )
for line in communityFile.readlines():
   if ( "community" in line ):
      communityDict[ line.split("community")[0].strip() ] = line.split("community")[1].strip()
Edit: Disclaimer: I didn't try this nor am I a Python programmer ( I do C++), this is off the top of my head.

Last edited by BlackEleven; 12-06-2012 at 12:46 PM.
BlackEleven is offline   Reply With Quote
Old 12-06-2012, 12:59 PM   #7
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

If you prefer your original methodology you could use regular expressions instead of split to do the parsing, but for someone learning programming I'd recommend using the simple method I posted above. It's easier to understand when you're learning.
BlackEleven is offline   Reply With Quote
The Following User Says Thank You to BlackEleven For This Useful Post:
Old 12-06-2012, 10:25 PM   #8
FlamesPuck12
First Line Centre
 
Join Date: Apr 2007
Exp:
Default

Late to the party but here's my contribution. I don't like using the imperative approach to solve this problem in Python.

Try using the dictionary constructor for this problem.

d = dict(line.strip().split(' community ') for line in open('data.txt'))
FlamesPuck12 is offline   Reply With Quote
Reply

Tags
coding , mountain dew , programming , python


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:49 AM.

Calgary Flames
2023-24




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Calgarypuck 2021