所以,我試圖做的是洗掉字串串列的一些特定值,我嘗試使用正則運算式沒有成功,我想洗掉所有沒有小數點的值,我不能只從一個某些索引,因為 int 數的位置不同。
這是串列的示例
['178', '34,79', '135925,82', '135926,82']
['102021', '191', '135067,69', '858,13', '859,13']
['176054330', '12', '506', '858,13', '0,00', '1,00']
我試圖做這樣的事情:
re.compile(r'\d{1,5},\d{2}.*')
vend_values = list(filter(r.match, vend_name))
要得到這個結果:
['34,79', '135925,82', '135926,82']
['135067,69', '858,13', '859,13']
['858,13', '0,00', '1,00']
但隨之而來的是它最終也洗掉了一些行
uj5u.com熱心網友回復:
你不需要使用像正則運算式這樣花哨的東西。在 python 中,in
關鍵字將為您完成作業。您也可以使用串列推導:
my_list = ['102021', '191', '135067,69', '858,13', '859,13']
my_filtered_list = [s for s in my_list if ',' in s]
如果你想把它變成一個函式:
def list_sorter(input_list):
return [s for s in input_list if ',' in s]
希望有幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/481198.html
上一篇:多級柵格串列-在R中執行代數
下一篇:遍歷所有可能性Python