Splitting strings in Python without split()

后端 未结 8 716
醉话见心
醉话见心 2021-01-17 03:59

What are other ways to split a string without using the split() method? For example, how could [\'This is a Sentence\'] be split into [\'This\', \'is\', \'a\', \'Sentence\']

8条回答
  •  礼貌的吻别
    2021-01-17 04:25

    Starting with a list of strings, if you would like to split these strings there are a couple ways to do so depending on what your desired output is.

    Case 1: One list of strings (old_list) split into one new list of strings (new_list).

    For example ['This is a Sentence', 'Also a sentence'] -> ['This', 'is', 'a', 'Sentence', 'Also', 'a', 'sentence'].

    Steps:

    1. Loop through the strings. for sentence in old_list:
    2. Create a new string to keep track of the current word (word).
    3. Loop through the characters in each of these strings. for ch in sentence:
    4. If you come across the character(s) you want to split on (spaces in this example), check that word is not empty and add it to the new list, otherwise add the character to word.
    5. Make sure to add word to the list after looping through all the characters.

    The final code:

    new_list = []
    for sentence in old_list:
        word = ''
        for ch in sentence:
            if ch == ' ' and word != '':
                new_list.append(word)
                word = ''
            else:
                word += ch
        if word != '':
            new_list.append(word)
    

    This is equivalent to

    new_list = []
    for sentence in old_list:
        new_list.extend(sentence.split(' '))
    

    or even simpler

    new_list =  ' '.join(old_list).split(' ')
    

    Case 2: One list of strings (old_list) split into a new list of lists of strings (new_list).

    For example ['This is a Sentence', 'Also a sentence'] -> [['This', 'is', 'a', 'Sentence'], ['Also', 'a', 'sentence']].

    Steps:

    1. Loop through the strings. for sentence in old_list:
    2. Create a new string to keep track of the current word (word) and a new list to keep track of the words in this string (sentence_list).
    3. Loop through the characters in each of these strings. for ch in sentence:
    4. If you come across the character(s) you want to split on (spaces in this example), check that word is not empty and add it to sentence_list, otherwise add the character to word.
    5. Make sure to add word to sentence_list after looping through all the characters.
    6. Append (not extend) sentence_list to the new list and move onto the next string.

    The final code:

    new_list = []
    for sentence in old_list:
        sentence_list = []
        word = ''
        for ch in sentence:
            if ch == ' ' and word != '':
                sentence_list.append(word)
                word = ''
            else:
                word += ch
        if word != '':
            sentence_list.append(word)
        new_list.append(sentence_list)
    

    This is equivalent to

    new_list = []
    for sentence in old_list:
        new_list.append(sentence.split(' '))
    

    or using list comprehensions

    new_list =  [sentence.split(' ') for sentence in old_list]
    

提交回复
热议问题