How to compare two tuples and return values.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel Valenzue
    New Member
    • Dec 2010
    • 39

    How to compare two tuples and return values.

    I have a list of street names(street_na mes) 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.

    Code:
    streetNames=open('street.txt','r')
    streetSpeeds=open('street_speed.txt','r')
    
    newStreetList=[]
    
    for street in streetNames:
            if street in (streetSpeeds):
                newStreetList.append(street)
            
    print newStreetList
    filename = "Street_with_Speed.txt"
    f = open(filename, 'w')
    f.write("".join(newStreetList))
    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.
    Miguel
    Attached Files
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • Miguel Valenzue
      New Member
      • Dec 2010
      • 39

      #3
      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

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You could import the two lists into a database and then join the street name field.

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          You should start by printing the first few records of each file
          Code:
          street_name=open('street.txt','r').readlines()
          for ctr in range(5):
              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.
          Code:
          street_speeds=open('street_speed.txt','r').readlines()
          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.

          Comment

          • Miguel Valenzue
            New Member
            • Dec 2010
            • 39

            #6
            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.

            Code:
            street_speeds = [s.strip() for s in open("street_speed.txt").readlines()]
            street_names = [s.strip() for s in open("street.txt").readlines()]
            
            mySet = set(street_names) - set(street_speeds)
            print mySet
            
            for i,v in enumerate(mySet):
                print i, v

            Comment

            • Miguel Valenzue
              New Member
              • Dec 2010
              • 39

              #7
              Am I asking the right question here?

              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:
              Code:
              street_name=open('street.txt','r').readlines()
              street_speeds=open('street_speed.txt','r').readlines()
              
              
              for x in street_speeds:
                  for y in street_name:
                      if y in x:
                          print x
                      else:
                          print y + "null"
              This returns a long list but not what I'm looking for. Please let me know if this helps.
              Thanks!

              Comment

              • dwblas
                Recognized Expert Contributor
                • May 2008
                • 626

                #8
                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.
                Code:
                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:
                Code:
                import string
                
                def create_speed_dict(fname):
                    ## stores the record twice-take your pick of which 
                    ## dictionary you want to use
                    name_dict = {}
                    speed_dict = {}
                    for rec in open(fname, "r"):
                        substrs = rec.split(",")
                        if len(substrs):   ## allow for blank records
                            name = substrs[0].lower()
                            speed = int(substrs[1].strip())
                            if speed not in speed_dict:
                                speed_dict[speed] = []
                            speed_dict[speed].append(name)
                            name_dict[name] = speed
                
                    return name_dict, speed_dict 
                
                name_dict, speed_dict = create_speed_dict("test1")
                print name_dict
                print speed_dict
                for n in ['Charles Way',
                          'Washington Street',
                          'Person Crossing',
                          'Permission Street',
                          'Imperial Highway',
                          'tompson street' ]:
                    n_low = n.lower()
                    if n_low in name_dict:
                        print "the speed for %s is %d" % (n, name_dict[n_low])
                    else:
                        print "the speed for %s is null" % (n)
                
                print "\nstreets that have a speed of 55 ="
                for name in speed_dict[55]:
                    print "     ", string.capwords(name)

                Comment

                Working...