5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python 文字リスト(配列)の操作

Posted at

文字リストの結合と分割

文字リストの要素結合

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

実行結果:
'And I love her'

要素分割

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

実行結果:
array(['And,', 'I', 'love', 'her'], dtype=object)

文字リスト間の結合

ここでは、tag=['svd3' 'svd3' 'svd3' 'svd3' 'svd5' 'svd5' 'svd5' 'svd3' 'svd3' 'svd3' 'svd3']というndarrayの要素それぞれの末尾に"iseg"という語を追加したい

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

実行結果:

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

文字リストへの変換

文字リストから数値リストへ

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

result:
[1.0, 2.0, 0.004]

数値リストから文字リストへ

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

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

数値型に変換できる文字かどうか判定したい(整数の場合)

val. isdigit()

数値型に変換できる文字かどうか判定したい

floatの場合は厄介である

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

詳しくはhttp://d.hatena.ne.jp/artgear/20120217/1329493335などを参照

5
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?