くだすれPython(超初心者用) その36at TECH
くだすれPython(超初心者用) その36 - 暇つぶし2ch263:sage
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))


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch