Python 如何删除所有包含特殊字符和字符串的元素?
我试图删除所有包含特殊字符或字符串的元素,但其中一些元素仍然存在Python 如何删除所有包含特殊字符和字符串的元素?,python,list,Python,List,我试图删除所有包含特殊字符或字符串的元素,但其中一些元素仍然存在 description_list = ['$', '2,850', 'door', '.', 'sale', '...', 'trades', '.', 'pay', 'pp', 'fees', 'shipping', 'cost', 'desirable', '\x932', 'liner', 'dial\x94', 'eta', 'movement', 'watch', '\x93safe', 'queen\x94', ','
description_list = ['$', '2,850', 'door', '.', 'sale', '...', 'trades', '.', 'pay', 'pp', 'fees', 'shipping', 'cost', 'desirable', '\x932', 'liner', 'dial\x94', 'eta', 'movement', 'watch', '\x93safe', 'queen\x94', ',', 'pristine', 'condition', '.', 'i\x92m', 'original', 'owner', 'worn', 'watch', 'gently', 'handful', 'times', '.', 'protective', 'plastics', 'still', 'intact', 'case', 'back', ',', 'parts', 'clasp', 'full', 'original', 'kit', 'you\x92ll', 'see', 'pics', '.', 'includes', 'original', 'boxes', ',', 'manuals', ',', 'warranty', 'card', 'ad', ',', 'spare', 'bracelet', 'links', ',', 'dive', 'strap', '&', 'extension', ',', 'etc', 'payment', 'paypal', ',', 'due', 'quickly', 'upon', 'agreement', 'purchase', 'watch', '.', 'holds', ',', 'delays', ',', 'games', '.', 'pay', 'pp', 'fees', 'shipping', 'us', 'postal', 'service', 'priority', 'mail', 'w/signature', 'confirmation', ',', 'paypal', 'verified', 'address', 'inside', 'usa', '.', 'please', 'don\x92t', 'ask', 'ship', 'outside', 'usa', '.', 'exceptions', 'made', '.', 'please', 'e-mail', '[', 'email', 'protected', ']', '.', 'also', 'text', 'call', '210-705-3383.', 'name', 'james', 'crockett', 'thank', ',', 'james', 'crockett', '$', '2,850', 'door', '.', 'sale', '...', 'trades', '.', 'pay', 'pp', 'fees', 'shipping', 'cost', 'desirable', '\x932', 'liner', 'dial\x94', 'eta', 'movement', 'watch', '\x93safe', 'queen\x94', ',', 'pristine', 'condition', '.', 'i\x92m', 'original', 'owner', 'worn', 'watch', 'gently', 'handful', 'times', '.', 'protective', 'plastics', 'still', 'intact', 'case', 'back', ',', 'parts', 'clasp', 'full', 'original', 'kit', 'you\x92ll', 'see', 'pics', '.', 'includes', 'original', 'boxes', ',', 'manuals', ',', 'warranty', 'card', 'ad', ',', 'spare', 'bracelet', 'links', ',', 'dive', 'strap', '&', 'extension', ',', 'etc', 'payment', 'paypal', ',', 'due', 'quickly', 'upon', 'agreement', 'purchase', 'watch', '.', 'holds', ',', 'delays', ',', 'games', '.', 'pay', 'pp', 'fees', 'shipping', 'us', 'postal', 'service', 'priority', 'mail', 'w/signature', 'confirmation', ',', 'paypal', 'verified', 'address', 'inside', 'usa', '.', 'please', 'don\x92t', 'ask', 'ship', 'outside', 'usa', '.', 'exceptions', 'made', '.', 'please', 'e-mail', '[', 'email', 'protected', ']', '.', 'also', 'text', 'call', '210-705-3383.', 'name', 'james', 'crockett', 'thank', ',', 'james', 'crockett']
price_list = [x for x in description_list if any(c.isdigit() for c in x)]
输出
# price_list
['2,850', '\x932', '210-705-3383.', '2,850', '\x932', '210-705-3383.']
应该是这样的(逗号可以接受,因为要提取价格编号)
您可以在列表内部执行
all
检查,检查字符串是否包含所有数字或逗号,然后仅过滤逗号值:
price_list = [x for x in description_list if all(c.isdigit() or c == ',' for c in x) and x != ',']
# ['2,850', '2,850']
假设您希望保留包含数字或带逗号的数字的数据,那么就接近了。
price\u list
的当前列表理解将返回至少包含一位数字的字符串
[str(x) for x in description_list if str(x).replace(',', '').isdigit()]
Regex答案
import re
price_list = [x for x in description_list if re.match('\d+(,*\d+)?$', x)]
您可以编写一个正则表达式来测试它是否与带有逗号的数字匹配,或者您是否只对解析整数值感兴趣,或者您是否也可能有带小数点的数字?例如“123456.78”。只解析整数值
import re
price_list = [x for x in description_list if re.match('\d+(,*\d+)?$', x)]