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

How to compare two tuples and return values.

P: 39
I have a list of street names(street_names) and another list of street names and speeds(streets_speeds).
street_names is shorter than streets_speeds.
I want to compare the values of street_names to streets_speeds and if the value is in streets_speeds, then it will return the name of the street and speed into a list.

Expand|Select|Wrap|Line Numbers
  1. streetNames=open('street.txt','r')
  2. streetSpeeds=open('street_speed.txt','r')
  4. newStreetList=[]
  6. for street in streetNames:
  7.         if street in (streetSpeeds):
  8.             newStreetList.append(street)
  10. print newStreetList
  11. filename = "Street_with_Speed.txt"
  12. f = open(filename, 'w')
  13. f.write("".join(newStreetList))
  14. f.close()
When I run this, it returns nothing.
I'd like it to return the name of the street with the associated speed from the streetSpeeds list.
Thanks for your help.
Attached Files
File Type: txt street.txt (88 Bytes, 332 views)
File Type: txt street_speed.txt (196 Bytes, 341 views)
Mar 29 '11 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 10K+
P: 12,383
There's no need for two text files for what you want to do. If it's in street speeds, then you already have the street name.
Mar 29 '11 #2

P: 39
Hi Rabbit,
It's not that I don't need two lists, it's that I have two lists.
I basically have a really long list of roads in our county, about 2000 of them that don't have speed limits associated with them.
I have another list which is a list of all the roads in the state, with speed limits and there's about 10,000 roads in that list.
I want to be able to pull the speed limits of the 2000 streets from the list of 10,000 and make a new list with the 2000 streets and speedlimits.
Mar 29 '11 #3

Expert Mod 10K+
P: 12,383
You could import the two lists into a database and then join the street name field.
Mar 29 '11 #4

Expert 100+
P: 624
You should start by printing the first few records of each file
Expand|Select|Wrap|Line Numbers
  1. street_name=open('street.txt','r').readlines()
  2. for ctr in range(5):
  3.     print street_name[ctr]
as there are probably newline character(s), white space, and blank lines that you would have to get rid of before comparing. Then you would search the speeds list.
Expand|Select|Wrap|Line Numbers
  1. street_speeds=open('street_speed.txt','r').readlines()
  2. if street_name in street_speeds: 
Usually, each record is converted to lower case before comparing to avoid capitalization errors, and special characters, ".", "," etc. are eliminated as well, and then you should consider whether you want to convert "Avenue" and "Av." to "Ave" as an example to avoid abbreviation mismatches.
Mar 29 '11 #5

P: 39
Thanks dwblas. I've got the search part but what I'm trying to do now is return both values from the list street speeds.
So if for example Permission Street from the streets list is found in the street_speed list then it would return the street name and street speed from street_speed
Permission street, 55

I'm having challenges with the return portion of the code. I'm wondering if I should make a dictionary out of street_speeds and then use the iter.items() function to return all associated values.

For now this is the code I have which returns a new set of the values of the street names found in street_speed.

Expand|Select|Wrap|Line Numbers
  1. street_speeds = [s.strip() for s in open("street_speed.txt").readlines()]
  2. street_names = [s.strip() for s in open("street.txt").readlines()]
  4. mySet = set(street_names) - set(street_speeds)
  5. print mySet
  7. for i,v in enumerate(mySet):
  8.     print i, v
Mar 31 '11 #6

P: 39
I don't know if I'm asking the right question here because none of these responses are getting me what I'm looking for. So here's another crack at it.

Here's list 1:
Charles Way, 55
Carter Road, 35
Washington Street, 55
Highway 15, 25
Blaker Way, 15
Person crossing, 40
Istambul Corridor, 45
Permission Street, 55
Python Way, 80
Imperial Highway, 75

Here's list 2:
Charles Way
Washington Street
Person Crossing
Permission Street
Imperial Highway
tompson street

Here's the resulting list I want:
Charles Way, 55
Washington Street, 55
Person Crossing, 40
Permission Street, 55
Imperial Highway, 75
tompson street, null

Currently the code I have is this where the lists are imported from external text files:
Expand|Select|Wrap|Line Numbers
  1. street_name=open('street.txt','r').readlines()
  2. street_speeds=open('street_speed.txt','r').readlines()
  5. for x in street_speeds:
  6.     for y in street_name:
  7.         if y in x:
  8.             print x
  9.         else:
  10.             print y + "null"       
This returns a long list but not what I'm looking for. Please let me know if this helps.
Mar 31 '11 #7

Expert 100+
P: 624
For now this is the code I have which returns a new set of the values of the street names found in street_speed.
The following code returns a set of records of street names that are not in street speeds.
Expand|Select|Wrap|Line Numbers
  1. mySet = set(street_names) - set(street_speeds)
The second block of code should work fine. If you want to save the results, then append to a list, or add to a dictionary. A generic lookup would be something like:
Expand|Select|Wrap|Line Numbers
  1. import string
  3. def create_speed_dict(fname):
  4.     ## stores the record twice-take your pick of which 
  5.     ## dictionary you want to use
  6.     name_dict = {}
  7.     speed_dict = {}
  8.     for rec in open(fname, "r"):
  9.         substrs = rec.split(",")
  10.         if len(substrs):   ## allow for blank records
  11.             name = substrs[0].lower()
  12.             speed = int(substrs[1].strip())
  13.             if speed not in speed_dict:
  14.                 speed_dict[speed] = []
  15.             speed_dict[speed].append(name)
  16.             name_dict[name] = speed
  18.     return name_dict, speed_dict 
  20. name_dict, speed_dict = create_speed_dict("test1")
  21. print name_dict
  22. print speed_dict
  23. for n in ['Charles Way',
  24.           'Washington Street',
  25.           'Person Crossing',
  26.           'Permission Street',
  27.           'Imperial Highway',
  28.           'tompson street' ]:
  29.     n_low = n.lower()
  30.     if n_low in name_dict:
  31.         print "the speed for %s is %d" % (n, name_dict[n_low])
  32.     else:
  33.         print "the speed for %s is null" % (n)
  35. print "\nstreets that have a speed of 55 ="
  36. for name in speed_dict[55]:
  37.     print "     ", string.capwords(name) 
Mar 31 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.