Home > Mobile >  Append from a string list until a semi-colon in Python 3.9
Append from a string list until a semi-colon in Python 3.9

Time:12-21

How do I append from multiple string lists I don't know what are, but I'll need to separate into new different lists using the semi-colon as a separator in Python? For example, if I'm given the following string list:

example = ['pineapple; lemon; watermelon']

I need to get the following results:

list1 = ["pineapple"]
list2 = ["lemon"]
list3 = ["watermelon"]

and append with the same logic and order when given a new string

P.S.: Since there are no patterns in the list I'll be given, I can't just use

list1.append(example[0:number])

for the task, which is what I've tried.

CodePudding user response:

If you really need each variable to be a list containing a single string (which is unusual but consistent with your input), you could do this:

list1,list2,list3 = ([s] for s in example[0].split('; '))

This works with the example given but it begs the questions: Can your original list contain more than one string ? Can the strings contain more (or fewer) than 3 parts separated by semicolons ? Do you really need lists with a single string in them as the output ? Where in your question is there an 'append' operation to be done and under what rules ?

With answers to all these questions, your request could be interpreted very differently. For example, append 3 parts of each string in the source list to the corresponding 3 lists. in which case you could have an input and output like this:

list1 = []
list2 = []
list3 = []

example = ['pineapple; lemon; watermelon',
           'apple; orange; banana',
           'grape; peach; pear']

lists = [list1,list2,list3]
for aString in example:
    for aList,part in zip(lists, aString.split('; ')):
        aList.append(part)

Output:

list1: ['pineapple', 'apple', 'grape']
list2: ['lemon', 'orange', 'peach']
list3: ['watermelon', 'banana', 'pear']

CodePudding user response:

you want

output_lists = [list(map(str.strip, s.split(';'))) for s in example]

then your desired results are output_lists[i][j], where i is the position of the original list in the example, and j is the position in the string that got split.

explanation:

  • s.split(';') takes your semicolon delimited string and returns a list of the elements
  • map(str.strip, s.split(';')) removes whitespace from the beginning and end of every element in the list of strings
  • list(map()) turns the output of map into a list (map returns a python iterator)
  • [list(map()) for s in example] is a python list comprehension

If for some reason you need your output to be singleton lists then you can do

def singleton_strip(to_strip):
    return [to_strip.strip()]

output_lists = [list(map(singleton_strip, s.split(';'))) for s in example]

If you know all elements will always be separated by '; ' then you can do s.split('; ') and drop the list(map()) construction.

CodePudding user response:

Here is a quick and dirty implementation. I am sure this can be improved on and made more efficient in various ways, but it solves your problem.

  • Related