473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to compare two tuples and return values.

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')
  3.  
  4. newStreetList=[]
  5.  
  6. for street in streetNames:
  7.         if street in (streetSpeeds):
  8.             newStreetList.append(street)
  9.  
  10. print newStreetList
  11. filename = "Street_with_Speed.txt"
  12. f = open(filename, 'w')
  13. f.write("".join(newStreetList))
  14. f.close()
  15.  
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.
Miguel
Attached Files
File Type: txt street.txt (88 Bytes, 400 views)
File Type: txt street_speed.txt (196 Bytes, 424 views)
Mar 29 '11 #1
7 4517
Rabbit
12,516 Expert Mod 8TB
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
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.
Thanks
Mar 29 '11 #3
Rabbit
12,516 Expert Mod 8TB
You could import the two lists into a database and then join the street name field.
Mar 29 '11 #4
dwblas
626 Expert 512MB
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
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()]
  3.  
  4. mySet = set(street_names) - set(street_speeds)
  5. print mySet
  6.  
  7. for i,v in enumerate(mySet):
  8.     print i, v
  9.  
Mar 31 '11 #6
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()
  3.  
  4.  
  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"       
  11.  
This returns a long list but not what I'm looking for. Please let me know if this helps.
Thanks!
Mar 31 '11 #7
dwblas
626 Expert 512MB
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
  2.  
  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
  17.  
  18.     return name_dict, speed_dict 
  19.  
  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)
  34.  
  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

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
4
by: MFRASER | last post by:
How can I compare two double values and return the greater of the two values? example double doublea = 1.0; double doubleb = 2.0 double retVal = ?compare doublea and doubleb
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
5
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via...
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
17
by: binjobster | last post by:
Hello everyone, I'm updating some documents that describes so many C/C++ functions/method(about 2500). But I'm not familiar with these codes. So I want to find such a utility can generate...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
26
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.