How to split a string into an array of characters in Python?

前端 未结 13 1230
一整个雨季
一整个雨季 2020-11-22 01:54

I\'ve tried to look around the web for answers to splitting a string into an array of characters but I can\'t seem to find a simple method

str.split(//)

相关标签:
13条回答
  • 2020-11-22 02:16

    If you want to process your String one character at a time. you have various options.

    uhello = u'Hello\u0020World'
    

    Using List comprehension:

    print([x for x in uhello])
    

    Output:

    ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    

    Using map:

    print(list(map(lambda c2: c2, uhello)))
    

    Output:

    ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    

    Calling Built in list function:

    print(list(uhello))
    

    Output:

    ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    

    Using for loop:

    for c in uhello:
        print(c)
    

    Output:

    H
    e
    l
    l
    o
    
    W
    o
    r
    l
    d
    
    0 讨论(0)
  • 2020-11-22 02:25

    split() inbuilt function will only separate the value on the basis of certain condition but in the single word, it cannot fulfill the condition. So, it can be solved with the help of list(). It internally calls the Array and it will store the value on the basis of an array.

    Suppose,

    a = "bottle"
    a.split() // will only return the word but not split the every single char.
    
    a = "bottle"
    list(a) // will separate ['b','o','t','t','l','e']
    
    0 讨论(0)
  • 2020-11-22 02:25

    Unpack them:

    word = "Paralelepipedo"
    print([*word])
    
    0 讨论(0)
  • 2020-11-22 02:28

    If you wish to read only access to the string you can use array notation directly.

    Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> t = 'my string'
    >>> t[1]
    'y'
    

    Could be useful for testing without using regexp. Does the string contain an ending newline?

    >>> t[-1] == '\n'
    False
    >>> t = 'my string\n'
    >>> t[-1] == '\n'
    True
    
    0 讨论(0)
  • 2020-11-22 02:31
    >>> s = "foobar"
    >>> list(s)
    ['f', 'o', 'o', 'b', 'a', 'r']
    

    You need list

    0 讨论(0)
  • 2020-11-22 02:31

    The task boils down to iterating over characters of the string and collecting them into a list. The most naïve solution would look like

    result = []
    for character in string:
        result.append(character)
    

    Of course, it can be shortened to just

    result = [character for character in string]
    

    but there still are shorter solutions that do the same thing.

    list constructor can be used to convert any iterable (iterators, lists, tuples, string etc.) to list.

    >>> list('abc')
    ['a', 'b', 'c']
    

    The big plus is that it works the same in both Python 2 and Python 3.

    Also, starting from Python 3.5 (thanks to the awesome PEP 448) it's now possible to build a list from any iterable by unpacking it to an empty list literal:

    >>> [*'abc']
    ['a', 'b', 'c']
    

    This is neater, and in some cases more efficient than calling list constructor directly.

    I'd advise against using map-based approaches, because map does not return a list in Python 3. See How to use filter, map, and reduce in Python 3.

    0 讨论(0)
提交回复
热议问题