Reputation: 3151
I have a huge list in python that looks like this:
('foo','bar','foo/bar','foo1','bar/1')
Each value above demonstrates the character variety that the list contains - aplhanumeric plus slash. I need a way to turn that list into a list of tuples, like this:
(('foo','foo'),('bar','bar'),('foo/bar','foo/bar'),('foo1','foo1'),('bar/1','bar/1'))
So what better way to do this than Regex search and replace, right? (correct me if I'm wrong).
I am therefore trying to match anything between the quotes except for the commas, because technically, they are also between quotes. I used lookahead and lookbehind to match anything:
(?<=')(.*?)(?=')
But that only matches the values within the quotes and the commas. What I need is to match the value plus the quotes except the commas, and use a replacing regex to make the list look like the tuple above.
I can't do this by hand because the list is huge.
Any thoughts?
Upvotes: 1
Views: 84
Reputation: 365925
OK, you have a huge list
of strings. You want a tuple
, where for each element of the list
, you have the pair (element, element)
.
That's exactly what zip
does, except that it returns a list
of such pairs in 2.x, or an iterator in 3.x. Either way, you can convert that to a tuple
just by calling tuple
. So:
tuple(zip(huge_list, huge_list))
More generally, if you want to transform a sequence element by element, you can use a comprehension or a generator expression. There are no "tuple comprehensions", but just passing a generator expression to the tuple
function does the same thing. So:
tuple((element, element) for element in huge_list)
Or, if you wanted a tuple
of (s[0], s[1:])
pairs instead of (s, s)
pairs:
tuple((element[0], element[1:]) for element in huge_list)
And so on.
Meanwhile, I can't think of any situation where converting an object into its repr
to run a regex transformation on it and re-parse it would be a good idea in Python. This isn't just a "Now they have two problems" issue; parsing the resulting string (and, even if you don't care about safety, figuring out how to deal with things where eval(repr(x)) != x
) is going to be a harder problem than whatever you started with. So, if you ever spot yourself trying to make that work, take a step back.
Upvotes: 2