[Tutor] range function

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Jan 30 00:41:00 CET 2005



On Sat, 29 Jan 2005, Srinivas Iyyer wrote:

> I have bunch of coordinates for various vectors.
>
> small vecs:
>
> name      cord. X    cord. Y   Sector no.
> smvec1    75          100        1aa
> smvec2    25          50         1aa
> smvec3    135         155        1ab
>
> large vecs:                                zone
> Lvec1     10          50          1aa      ut
> Lvec1     60          110         1aa      cd
> Lvec1     130         180         1ab      cd
>
> Now I am checking if small vecs are falling in which
> large vecs.


Hi Srivivas,


Some of the problem statement is confusing me slightly, since there's more
infomation here than just "vector" information.

Is it accurate to say that the essential part of the data is something
like this?

###
small_vecs = [ ("smvec1", 75, 100, "1aa"),
               ("smvec2", 25, 50, "1aa"),
               ("smvec3", 135, 155, "1ab") ]

large_vecs = [ ("Lvec1", 10, 50, "1aa", "ut"),
               ("Lvec1", 60, 110, "1aa", "cd"),
               ("Lvec1", 130, 180, "1ab", "cd") ]
###


Or do you really just care about a portion of the data?

###
small_vecs = [ ("smvec1", 75, 100),
               ("smvec2", 25, 50,),
               ("smvec3", 135, 155) ]

large_vecs = [ ("Lvec1", 10, 50),
               ("Lvec1", 60, 110),
               ("Lvec1", 130, 180) ]
###

I'm just trying to digest what part of the program we're trying to solve.
*grin*


Rather than work on text files directly as part of the algorithm, it might
be easier to split the problem into two pieces:

    Part 1.  A function that takes the text file and turns it into a data
    structure of Python tuples, lists, and numbers.

    Part 2.  A function to do the matching against those data structures.

The reason this breakup might be useful is because you can test out the
vector-matching part of the program (Part 2) independently of the
file-reading-parsing part (Part 1).

And parsing files is sometimes really messy, and we often want to keep
that messiness localized in one place.  As a concrete example, we probably
need to do something like:

    cols = line.split('\t')
    (x, y) = (int(cols[1]), int(cols[2]))

where we have to sprinkle in some string-to-int stuff.




> The other way by taking tuples:
>
> for line in smallvecs:
>      cols = line.split('\t')
>      smvec_tup = zip(cols[1],cols[2])

zip() is probably not the best tool here.  We can just write out the tuple
directly, like this:

    smvec_tup = (cols[1], cols[2])


zip() is meant for something else: here's an example:

###
>>> languages = ["Python", "Perl", "Java", "Ocaml", "C"]
>>> prefixes = ["py", "pl", "java", "ml", "c"]
>>> languages_and_prefixes = zip(languages, prefixes)
>>>
>>> print languages_and_prefixes
[('Python', 'py'), ('Perl', 'pl'), ('Java', 'java'), ('Ocaml', 'ml'),
 ('C', 'c')]
###

So zip() is more of a bulk-tuple constructing function: it's not really
meant to be used for making just a single tuple.  It takes in lists of
things, and pairs them up.


Please feel free to ask more questions about this.  Best of wishes to you!



More information about the Tutor mailing list