簡體   English   中英

遍歷列表列表並刪除不需要的字符串

[英]Iterate through list of lists and remove unwanted strings

我正在玩游戲,我從網站上抓取了一份配料表。 我現在有一個列表列表。

ingrediant_list = []

for ingrediant in soup.select('.wprm-recipe-ingredient'):
    ingrediant_list.append(ingrediant.text)

full_list = []

for item in ingrediant_list:
    full_list.append(item.split())

這是我生成列表列表的代碼。 首先,我從網站上獲取原料並將它們放入ingrediants_list 然后我將每個字符串拆分為一個單獨的列表,在full_list下生成一個列表列表

我的清單如下:

[['400', 'g', '5%', '脂肪', '剁碎', '牛肉'], ['1', '大', '洋蔥', '細碎', '切碎'], ['3', '丁香', '大蒜', '精細', '磨碎'], ['5', '蘑菇', '切片'], ['1', '大', '胡蘿卜', '精細','切碎'],['1','棒','芹菜','精細','切碎'],['1','紅色','胡椒','精細','切碎' ], ['2', '罐頭', '切碎', '番茄'], ['1', 'tbsp', '番茄', '果泥'], ['1', 'tbsp', '混合' , 'Italian', 'Herbs'], ['1', 'tbsp', 'Balsamic', 'Vinegar'], ['1', 'Red', 'Wine', 'Stock', 'Pot'], ['250', 'ml', 'Beef', 'Stock', 'make', 'using', '1-2', 'beef', 'stock', 'cubes'], ['dash', " Henderson's", 'Relish/Worcestershire', 'Sauce'], ['Low', '卡路里', 'Cooking', 'Spray'], ['200', 'g', 'Dried', 'Pasta', ' use', 'whichever', 'shape', 'you', 'prefer'], ['80', 'g', 'Reduced', 'Fat', 'Cheddar', 'Cheese']]

我如何遍歷這個列表列表,刪除像“finely”、“chopped”和“grated”這樣的字符串,用“grams”替換“tbsp”,然后創建另一個類似於“ingrediants_list”的列表,但我沒有做任何事情不想?

newlist  = [i for i in oldlist if unwanted_string not in i]

我將用一個例子進行擴展

item_list = ["BigCar", "SmallCar", "BigHouse", "SmallHouse"]
unwanted_string = "Big"
[i for i in item_list if not unwanted_string in i]

結果:

['SmallCar', 'SmallHouse']

首先,不需要拆分字符串來替換不必要的單詞,您可以使用str.replace()

full_list = []
replace_rules = {
    'finely': '',
    'chopped': '',
    'grated': '',
    'tbsp': 'grams'
}
for s in ingrediant_list:
    for old, new in replace_rules.items():
        s = s.replace(old, new)
    full_list.append(s.rstrip())  # .rstrip() removes trailing spaces if exist

上面的代碼有效,但它只會替換小寫的單詞。 我們可以使用正則表達式來解決它:

import re

full_list = []
replace_rules = {
    r'\s*(finely|chopped|grated)': '',
    r'(\s*)tbsp': r'\1grams'
}
for s in ingrediant_list:
    for old, new in replace_rules.items():
        s = re.sub(old, new, s, re.IGNORECASE)
    full_list.append(s)

如果由於某些原因需要拆分句子,則可以使用嵌套循環:

replace_rules = {
    'finely': '',
    'chopped': '',
    'grated': '',
    'tbsp': 'grams'
}
result_list = []
for l in full_list:
    temp_list = []
    for w in l:
        if w.lower() in replace_rules:
            if replace_rules[w.lower()]:
                temp_list.append(replace_rules[w.lower()])
        else:
            temp_list.append(w)
    result_list.append(temp_list)

或者你可以使用列表推導來做同樣的事情:

filter_list = {'finely', 'chopped', 'grated'}  # words to ignore
replace_rules = {'tbsp': 'grams'}              # words to replace
result_list = [[replace_rules.get(w.lower(), w) for w in l if w.lower() not in filter_list] for l in full_list]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM