我在python中有一個字串專案串列:
positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
我想從包含空格的專案中洗掉空格(例如' RAMF')。
我已經使用以下代碼進行了嘗試:
positions = [x.strip(' ') for x in positions]
# AttributeError: 'NoneType' object has no attribute 'strip'
player_positions = [x.text.strip(' ') for x in player_positions]
# AttributeError: 'str' object has no attribute 'text'
positions = map(str.strip, positions)
# <map at 0x7fde15d19a90>
如何洗掉此處包含空格的任何串列項中的空格?
uj5u.com熱心網友回復:
解決方案(已編輯):
positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
positions = [x.strip(" ") for x in positions]
uj5u.com熱心網友回復:
我推測您的實際串列可能包含一些不是字串的專案。使用此串列推導將它們重繪 :
bad_positions = [p for p in positions if not isinstance(p, str)]
uj5u.com熱心網友回復:
這個問題可能模棱兩可,所以我將提供 3 個選項:
positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
# replace all/any spaces
positions = [p.replace(' ', '') for p in positions]
# remove leading and trailing spaces
positions = [p.strip(' ') for p in positions]
# remove leading and trailing whitespace
positions = [p.strip() for p in positions]
uj5u.com熱心網友回復:
你給定的清單
positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
并且代碼不會導致您的所有錯誤。尤其
positions = [x.strip(' ') for x in positions]
只會作業。
這可以從您的訊息中扣除:
positions = [x.strip(' ') for x in positions] # AttributeError: 'NoneType' object has no attribute 'strip'
該錯誤告訴您其中至少有一個元素positions是None- 而不是字串。
player_positions = [x.text.strip(' ') for x in player_positions] # AttributeError: 'str' object has no attribute 'text'
該錯誤告訴您其中至少有一個元素player_positions是字串 - 字串沒有屬性text。如果player_positions 相同,positions則表示該值之前至少有 1 個字串None值。
positions = map(str.strip, positions) # <map at 0x7fde15d19a90>
map在python中回傳一個迭代器-該函式在您迭代它時應用于您的值-您不知道這就是為什么您還沒有例外。如果迭代,這會產生與第一個串列類似的錯誤。_
通過執行以下操作,您將得到所有錯誤:
p = ['CF', None, 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
try:
p1 = [x.strip(' ') for x in p]
except Exception as e:
print(type(e), e)
==><class 'AttributeError'> 'NoneType' object has no attribute 'strip'
try:
p2 = [x.text.strip(' ') for x in p]
except Exception as e:
print(type(e), e)
==><class 'AttributeError'> 'str' object has no attribute 'text'
try:
p3 = map(str.strip, p)
p3l = list(p3)
except Exception as e:
print(type(e), e)
==><class 'TypeError'> descriptor 'strip' for 'str' objects doesn't apply to a 'NoneType' object 類似于<class 'AttributeError'> 'NoneType' object has no attribute 'strip'
從您的資料中過濾掉 None 值可能是您最好的猜測:
filtered_pos = [p for p in positions if p is not None]
如果你只有字串和None資料,你應該沒問題。
如果您需要保持非字串/None完整,請strip()僅適用于字串:
p = ['CF', None, 'LCMF', 42, 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
p1 = [x.strip(' ') if isinstance(x, str) else x for x in p]
print(p1)
將只更改字串,不理會其他字串:
['CF', None, 'LCMF', 42, 'AMF', 'LW', 'RAMF', 'LCMF', 'AMF', 'RB']
uj5u.com熱心網友回復:
嘗試使用下面提到的字串替換功能。
positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']
print([position.replace(' ','') for position in positions])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/452317.html
