18/02/03 20:49:35.33 ueJRcD/G.net
URLリンク(qiita.com)
を読んで以下を試したのですが、
list3, list4の出力が正しい内容として、正規表現を使わずに簡潔に書く方法が
あれば教えてください。
内包表記やスライスの使い方も完全に理解できていない自覚はあるのですが、
list4でfindを2回呼んでいるのが冗長です。
print("文字列より最初の'-1'のみ削除")
list = ['1011-1-11', '1111-1-01', '1112-1-02', '1112-2-02', '1113-1-03', '1114-1-2-1-04', '1115-1-1-05']
print('元データ:\t' + str(list))
list2 = [x.strip('-1') for x in list]
print('strip:(NG)\t' + str(list2))
list3 =[x.replace('-1', '', 1) for x in list]
print('replace:(OK)\t' + str(list3))
#list4 = [x[:x.find('-1')]+x[x.find('-1')+2:] if x.find('-1') >= 0 else x for x in list]
list4 = [x[:x.find('-1')]+x[x.find('-1')+2:] if '-1' in x else x for x in list]
print('find:\t' + str(list4))