1

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.

7
  • How is [x1,x2,x3,x4,...] [y1,y2,y3,y4...] a list? Isn't this two lists? Commented Oct 28, 2010 at 18:35
  • 1
    I'm confused is the list with the Xs in it list one? or is [x...][y...] list one? i.e. is list one a list of lists? Commented Oct 28, 2010 at 18:36
  • 1
    Post an actual example of a pre processed and post processed list maybe? Commented Oct 28, 2010 at 18:38
  • it is a list of strings... so it should be x1 dash x1'... Commented Oct 28, 2010 at 18:42
  • 1
    The list is of strings which look like this: "[a,b,c,d,e] [x,y,z,w,u]" -- meaning two bracketed substrings Commented Oct 28, 2010 at 18:45

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.)
I think you mean [sub(a,b) for (a,b) in itertools.izip(lst1, lst2)] if he wants a list
@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
@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: 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.
0

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.

1 Comment

in python 2.x, zip() creates the zip()ed list before returning, while izip() returns a generator
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.