Manipulating Python character lists (arrays)

Joining and splitting character lists

Element combination of character list

    a=["And","I","love","her"]
    b=" ".join(a)
    print b

Execution result: 'And I love her'

Element split

    import numpy as np
    np.core.defchararray.split(np.array(b))

Execution result: array(['And,', 'I', 'love', 'her'], dtype=object)

Join between character lists

Here, tag = ['svd3''svd3''svd3''svd3''svd5''svd5''svd5''svd3''svd3''svd3' I want to add the word " iseg "to the end of each element of the ndarray called'svd3']

    import numpy as np
    num=len(tag)
    iseg= np.array(["iseg"]*num)
    tag=np.core.defchararray.add(tag,iseg)
    print tag

Execution result:

['svd3iseg' 'svd3iseg' 'svd3iseg' 'svd3iseg' 'svd5iseg' 'svd5iseg'
 'svd5iseg' 'svd3iseg' 'svd3iseg' 'svd3iseg' 'svd3iseg']

Conversion to character list

From character list to number list

    a=["1.0","2","4.e-3"]
    map(float,a)

result: [1.0, 2.0, 0.004]

From numeric list to character list

    b=[1.0, 2.0, 0.004]
    map(str,a)

result: ['1.0', '2', '4.e-3']

I want to judge whether a character can be converted to a numeric type (in the case of an integer)

val. isdigit()

I want to judge whether a character can be converted to a numeric type

It's awkward for floats

val.replace(".","",1).isdigit()

For details, refer to http://d.hatena.ne.jp/artgear/20120217/1329493335 etc.

Recommended Posts

Manipulating Python character lists (arrays)
Find memory-consuming lists / arrays on Python
Manipulating Twitter lists
python character code
Python Node.js character manipulation
Memorize Python commentary 5 --Lists
Python3 | Lists, Tuples, Dictionaries
Python> Tuples versus Lists
Python lists, tuples, dictionaries
About Python3 character code
[Python] Extract only numbers from lists and character strings
3-3, Python strings and character codes
python> Handling of 2D arrays
2.x, 3.x character code of python
Python Beginner's Guide (Variables / Arrays)
Character code learned in Python
Python f character (formatted string)
Today's python error: SyntaxError Non-ASCII character
Manipulating EAGLE .brd files with Python
Easily handle lists with python + sqlite3
[Beginner] Extract character strings with Python
I'm addicted to Python 2D lists