tags:

views:

61

answers:

3

I have two lists of strings, which have the following format:

[x1,x2,x3,x4,...] [y1,y2,y3,y4...]

Call it lst1.

lst2 would be:

[x1',x2',x3',x4',...] [y1,y2,y3,y4,...]

you can assume that each string in lst1 has matching y's in the corresponding element in lst2, and the same number of x's, but it would be great to throw an error if some x and x' is not of the same length.

Then, I want to merge lst1 and lst2, in the following way:

create a new list of strings where:

[x1-x1',x2-x2',....] [y1,y2,y3,y4...]

I am really curious to see what kind of solutions would come up with that... I am new to python and I want to see what kind of different ways there are to do things for a data processing of this type (which I do a lot).

Thanks.

+2  A: 
(sub(a,b) for (a,b) in itertools.izip(lst1, lst2))

where sub() is whatever kind of 'substracting' you want to do between respective strings

Javier
you mean itertools.izip()
singularity
I looked at itertools.izip. I don't think that would work, but the idea of using itertools could be useful. I didn't know about it. Thanks. (I think you are assuming a different format, or different task.)
stler
tks, fixed.....
Javier
I think you mean [sub(a,b) for (a,b) in itertools.izip(lst1, lst2)] if he wants a list
Jared Updike
@stler: there are plenty of ambigüities in the original question; but in almost every kind of list processing you want to do, the answer starts with itertools
Javier
@jared: in general i prefer using generators, because they can be composed further and only traversed at the very end. no need to create lots of intermediate arrays
Javier
@Javier: that's great. They didn't exist when I was learning Python; I'm used to this from LINQ in C# or Haskell's built in lazy lists. Time to learn a new (to me) library for this old Python programmer.
Jared Updike
A: 

Based on the comment above, that - is not actually a subtraction, but a dash in a string,

Furthermore depending on how you want to deal with the result? As a list:

["%s-%s"%(a,b) for (a,b) in itertools.izip(lst1, lst2)]

Or as an iterator:

("%s-%s"%(a,b) for (a,b) in itertools.izip(lst1, lst2))

Also, instead of itertools.izip you can just use zip but I don't know the implications of that.

NorthIsUp
in python 2.x, `zip()` creates the zip()ed list before returning, while `izip()` returns a generator
Javier
A: 

Oh wait - -you have two Strings, with brackets and items as characters in it? That is what I infere from your comment """ The list is of strings which look like this: "[a,b,c,d,e] [x,y,z,w,u]" -- meaning two bracketed substrings – stler 28 mins ago """ -

That is a totally different thing form what one understands from your question, as lists are native objects in python.

To process a string like that, you have to break it apart (using the split method) on the "]" character, and then at the comas:

lst1 = "[a,b,c,d,e] [x,y,z,w,u]"
lst2 = "[1,2,3,4,5] [x,y,z,w,u]"

# part the strings in two parts:
part1, part2 = lst1.split("]",1)

# isolate the elements in part1:
part1 = part1.split(",")
# separate the desired elements from string 2: split at "]", throw "[" away, split at  ",":
part3= lst2.split("]")[0].strip("[").split(",")

parts_list = []

for element1, element2 in zip(part1, part3):
  if len(element1.strip("[")) != len(element2):
      raise ValueError("List parts differ in lenght")
  parts_list.append("%s-%s" % (element1, element2))

final_list = ",".join(parts_list) + "]" + part2
jsbueno