简体   繁体   中英

pythonic way to split string into two lists

I have a lot of files with string pairs looking like:

first_string~second_string

The first part is what to change and the second, to what change the first part.

These are regular expressions and I run my app with these collections to apply all modifications to dirty tv schedule listings for more than a hundred channels. I did it before with C# but now I am reworking it in Python.

Let's imagine I have a text file with many strings each on its own line that look like find_this~change_to_this . I need to get two lists. First will contain all find strings and the second will contain all change strings.

Let's imagine, I have 120 such pairs. Now I divide these pairs into two lists, each has size of 120 items. One has finds, the other - changes. Now I can get both strings by some index, for example, 57, and it will give me 57th item from both lists, so I get a right change string for any find string. I found some variants, but not sure which one is better.

What is the pythonic to split a collection of strings like this:

first_string~second_string

Using that input to split it into two lists where first list contains items before ~ and the second one - after.

x = ["c~d", "e~f", "g~h"]
a, b = zip(*(s.split("~") for s in x))
print a
print b

prints

('c', 'e', 'g')
('d', 'f', 'h')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM