我有一個像這樣的字串串列list = ['Fruits in ['Apples', 'Mangoes']','Vegetables in ['Carrots', 'Onion']']我想要一個串列,其中包含這樣的串列值的方括號內的值,'list1 = [[Apples', 'Mangoes'], ['Carrots', 'Onion']請幫助我。
uj5u.com熱心網友回復:
import re
def extract_items(s):
return re.compile("(?<=[^\(]')\w ").findall(s)
mylist = ["Fruits in ['Apples', 'Mangoes']","Vegetables in ['Carrots', 'Onion']"]
list1 = list(map(extract_items, mylist))
print(list1)
uj5u.com熱心網友回復:
你可以這樣做:
new_list = []
list = ['Fruits in ["Apples", "Mangoes"]','Vegetables in ["Carrots", "Onion"]']
for elem in list:
# gets the text after the opening bracket and avoids the last character
brackets = elem.split("[")[1][:-1]
inside_list = brackets.split(',')
new_list.append(inside_list)
print(f'{new_list}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/414410.html
標籤:
上一篇:在macOS上通過單擊從SwiftUI串列中取消選擇專案
下一篇:如何從C中的字串中列印特定字符
