stripping values from string
I have two list from my input form: ListA: [a,b,c,d] ListB: [1,2,3,4] How can i strip ListA and ListB so that the first value of ListA will correspond to the first value of ListB and the second of A to sencond of B and so on: Example: (a,1), (b,2),(c,3),(d,4) I want to be able to populate a database with these values. I've tried to accomplish this using dtml-in but failed miserably. Can someone give me a pointer on how to do this? Thanks in advance for your suggestions, Mike
I have two list from my input form:
ListA: [a,b,c,d] ListB: [1,2,3,4]
How can i strip ListA and ListB so that the first value of ListA will correspond to the first value of ListB and the second of A to sencond of B and so on:
Example: (a,1), (b,2),(c,3),(d,4)
In Python, you'd just use zip: $ python Python 2.2.1 (#1, Jun 25 2002, 10:55:46) [GCC 2.95.3-5 (cygwin special)] on cygwin Type "help", "copyright", "credits" or "license" for more information.
a = ['a', 'b', 'c', 'd'] b = [1, 2, 3, 4] zip(a, b) [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
I assume you're doing this in a Python script? Try zip. I haven't tried it and I don't know whether zip is considered restricted (not sure why it would be); I'm just saying I haven't tried this from a Python Script inside Zope (although I don't see why it wouldn't work). You could also just iterate over one of the lists and append a tuple of its element and the corresponding element from the other list into a new list. assert len(a) == len(b) new_list = [] for i in range(len(a)): new_list.append((a[i], b[i])) // m -
On Wed, Jan 08, 2003 at 09:22:09AM -0700, Mike Doanh Tran wrote:
I have two list from my input form:
ListA: [a,b,c,d] ListB: [1,2,3,4]
How can i strip ListA and ListB so that the first value of ListA will correspond to the first value of ListB and the second of A to sencond of B and so on:
Example: (a,1), (b,2),(c,3),(d,4)
The python built-in function zip does this. ListC = zip(ListA, ListB) Unfortunately, zip seems to be disallowed in through-the-web zope code. weird. So you can roll your own: ListC = [] for i in range(min(len(ListA), len(ListB))): ListC.append( (ListA[i], ListB[i])) The call to min() ensures that ListC is only as long as the shortest of ListA and ListB so you don't get an IndexError by trying to access non-existent elements of the shorter list. not sure if that's what you want. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's MEAT- MOLE RAT! (courtesy of isometric.spaceninja.com)
Mike Doanh Tran writes:
I have two list from my input form:
ListA: [a,b,c,d] ListB: [1,2,3,4]
How can i strip ListA and ListB so that the first value of ListA will correspond to the first value of ListB and the second of A to sencond of B and so on:
Example: (a,1), (b,2),(c,3),(d,4)
This is done trivially in Python using map:
map(None, ['a','b','c','d'], [1,2,3,4]) [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
HTH(tm), -tree -- Tom Emerson Basis Technology Corp. Software Architect http://www.basistech.com "Beware the lollipop of mediocrity: lick it once and you suck forever"
participants (4)
-
Mark McEahern -
Mike Doanh Tran -
Paul Winkler -
Tom Emerson