memorandum.
I want to keep only the elements that start with a specific string in the array.
I have an array of ['hogehoge.form','hugahuga.form','hogehoge.form2']
, and I want to keep only the elements that start with hogehoge
and delete the other elements.
startswith ()
Method that can determine the beginning of a character string
moji = 'hogehoge'
#Returns true
moji.startswith('hoge')
#Returns false
moji.startswith('huga')
#Original array
moji_ary = ['hogehoge.form', 'hugahuga.form', 'hogehoge.form2']
#New array new_In ary, the original array moji_Of the elements of ary`ho`Insert only elements starting with
new_ary = [hoge for hoge in moji_ary if hoge.startswith('ho')]
#Output result['hogehoge.form', 'hogehoge.form2']
print(new_ary)
There is also a method ʻendswith ()
`to determine the mating (?) Of a string.
You can find other useful things by google with "python string manipulation".
Recommended Posts