split string into list of tuples?

10,114

Solution 1

def group(lst, n):
    for i in range(0, len(lst), n):
        val = lst[i:i+n]
        if len(val) == n:
            yield tuple(val)

a = 'Moscow|city|London|city|Royston Vasey|vilage'
list(group(a.split('|'), 2))

The output is [('Moscow', 'city'), ('London', 'city'), ('Royston Vasey', 'vilage')]

Solution 2

This is a pretty easy one really...

first, split the string on '|' then zip every other element together:

data = s.split('|')
print zip(data[::2],data[1::2])

In python3, you'll need: print(list(zip(data[::2],data[1::2]))

Solution 3

s = 'Moscow|city|London|city|Royston Vasey|vilage'

it = iter(s.split('|'))
print [(x,next(it)) for x in it]

Solution 4

For Python2

>>> s = "Moscow|city|London|city|Royston Vasey|vilage"
>>> zip(*[iter(s.split('|'))]*2)
[('Moscow', 'city'), ('London', 'city'), ('Royston Vasey', 'vilage')]

Python3 just needs list(zip(...)) of course

Solution 5

You could use city, status, remaining = s.split("|", 2) and some recursive method city_split(s) to achieve what you want.

Share:
10,114
Ibolit
Author by

Ibolit

Programmer-hobbyist with the ambition to rule the world. -- Who isn't? ;)

Updated on June 20, 2022

Comments

  • Ibolit
    Ibolit almost 2 years

    I have a string of key-value pairs, which unfortunately are separated by the same symbol. Is there a way to "just split" it into a list of tuples, without using a lambda?

    Here is what i have:

    Moscow|city|London|city|Royston Vasey|vilage
    

    What i want:

    [("Moscow","city"), ("London", "city")....] 
    
  • DSM
    DSM about 11 years
    For forward-compatibility reasons, it's probably better to encourage next(it) rather than it.next().
  • mgilson
    mgilson about 11 years
    @NPE -- This one is pretty much straight from somewhere in the itertools documentation. It ends up being useful at times though.
  • eyquem
    eyquem about 11 years
    @DSM OK, thank you. Moreover next(it) is more clear. But what do you mean by froward-compatibility ? Relatively to passage to Python 3 ?
  • John La Rooy
    John La Rooy about 11 years
    @eyquem, yes .next becomes .__next__ in line with other methods like __str__ and __len__ etc.
  • eyquem
    eyquem about 11 years
    @gnibbler OK thank you. I've seen in the Python 3 "s doc, next() has indeed disappeared as a method. - Just a point: "in line with other methods" means "as other methods", doesn't ?
  • eyquem
    eyquem about 11 years
    I +1 but this solution pleases me only at 3/4 because after the creation of list by s.split('|') and creation of the iterator iter(s.split('|')), another list of the same iterator repeated is created, then it needs an additional unpacking to on-the-fly parameters of zip
  • eyquem
    eyquem about 11 years
    @Ibolit You should be aware that if there are N tuples to extract in the string, this solution creates N-1 strings named remaining having no real justification since there are other ways to do without this.