我擁有以下串列結果。這是文章的AB測驗得到的結果。
texts = [
'A text',
'89',
'71%',
'10%',
'B',
'B text',
'110',
'50%',
'9%',
'C',
'C text',
'30%',
'4%'
]
texts2 = [
'A'
'A text',
'89',
'71%',
'10%',
'B',
'B text',
'110',
'50%',
'9%',
'C text',
'30%',
'4%'
]
只有此串列中的最佳結果不包含任何字母“A”、“B”或“C”。在此串列中,A 結果不包含“A”。但我想知道如何處理沒有“B”和“C”作為字串出現的串列的可能性。
我現在正在嘗試以下代碼,但它不起作用。
有沒有好的解決方案?
while ('A' or 'B' or 'C') in texts:
try:
texts.remove('A')
texts.remove('B')
texts.remove('C')
except Exception as ex:
print(ex)
uj5u.com熱心網友回復:
試試這個:
texts = [
'A text',
'89',
'71%',
'10%',
'B',
'B text',
'110',
'50%',
'9%',
'C',
'C text',
'30%',
'4%'
]
exclude = ['A','B','C']
t = [x for x in texts if (x not in exclude)]
print(t)
輸出:['A text', '89', '71%', '10%', 'B text', '110', '50%', '9%', 'C text', '30%', '4%']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/466087.html
上一篇:如何列印串列值周圍的內容?
