使用Python从列表中删除不必要的数据
我有一个这样的列表,想知道怎么用Python把\xe2\x80\x99、\xe2\x80\x9c等这些东西从我的列表中去掉。有没有什么方法可以清理这些数据呢?有没有常见的做法?
['guest', 'demo', ':', 'eric', 'iverson', '\xe2\x80\x99s', 'itty', 'bitty', 'search', 'february', '16', 'th', ',', '2010', 'by', 'daniel', 'tunkelang', 'respond', 'i', '\xe2\x80\x99m', 'back', 'from', 'vacation', ',', 'and', 'still', 'digging', 'my', 'way', 'out', 'of', 'everything', 'that', '\xe2\x80\x99s', 'piled', 'up', 'while', 'i', '\xe2\x80\x99ve', 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', '\xe2\x80\x99d', 'share', 'with', 'you', 'a', 'demo', 'that', 'eric', 'iverson', 'was', 'gracious', 'enough', 'to', 'share', 'with', 'me', 'it', 'uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', 'experience', 'on', 'top', 'of', 'a', 'general', 'web', 'search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', 'application', 'retrieves', 'a', 'set', 'of', 'related', 'term', 'candidates', 'using', 'yahoo', '\xe2\x80\x99s', 'key', 'terms', 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', 'it', 'is', 'global', 'occurrence', 'count', '\xe2\x80\x93a', 'relevance', 'measure', 'similar', 'to', 'one', 'my', 'former', 'colleagues', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', 'has', 'rough', 'edges', ',', 'it', 'produces', 'nice', 'results', '\xe2\x80\x93especially', 'considering', 'the', 'simplicity', 'of', 'the', 'approach', 'here', '\xe2\x80\x99s', 'an', 'example', 'of', 'how', 'i', 'used', 'the', 'application', 'to', 'explore', 'and', 'learn', 'something', 'new', 'i', 'started', 'with', '["', 'information', 'retrieval', '"]', 'i', 'noticed', '\xe2\x80\x9c', 'interactive', 'information', 'retrieval', '\xe2\x80\x9d', 'as', 'a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', 'suggestions', 'looked', 'familiar', 'to', 'me', '\xe2\x80\x93but', 'an', 'unfamiliar', 'name', 'caught', 'my', 'attention', ':', '\xe2\x80\x9c', 'anton', 'leuski', '\xe2\x80\x9d', 'following', 'my', 'curiosity', ',', 'i', 'refined', 'again', 'looking', 'at', 'the', 'results', ',', 'i', 'immediately', 'saw', 'that', 'leuski', 'had', 'done', 'work', 'on', 'evaluating', 'document', 'clustering', 'for', 'interactive', 'information', 'retrieval', 'further', 'exploration', 'made', 'it', 'clear', 'this', 'is', 'someone', 'whose', 'work', 'i', 'should', 'get', 'to', 'know', '\xe2\x80\x93check', 'out', 'his', 'home', 'page', '!', 'i', 'can', '\xe2\x80\x99t', 'promise', 'that', 'you', '\xe2\x80\x99ll', 'have', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', 'i', 'encourage', 'you', 'to', 'try', 'eric', '\xe2\x80\x99s', 'demo', 'it', '\xe2\x80\x99s', 'simple', 'examples', 'like', 'these', 'that', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', '\xe2\x80\x99ll', 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', 'course', 'i', '\xe2\x80\x99ll', 'share', 'them', 'here']
3 个回答
你可以使用列表推导式。假设你只是想完全去掉列表中包含非字母数字字符的元素。如果你的列表存储在一个变量 a
中:
[x for x in a if x.isalnum()]
这样做会返回一个新的列表,里面不包含那些有 \xe2\x80\x99
等字符的元素。
这和 @ssm 提到的 过滤器方法 是一样的,他们只是先提到这个方法了。
看起来你有一堆想要去掉的unicode字符串。你可以像这样选择列表中的字母和数字:
>>> filter( lambda m: m.isalnum() ,p)
这样就能去掉那些unicode的东西了……
另外一个方法是直接对字符串进行编码和解码……
>>> ' '.join(p).decode('ascii', 'ignore').encode('ascii').split()
这样做效果会好很多……
如果我猜得没错,输入的内容是utf8编码,你可以试试下面的做法:
>>> from unidecode import unidecode
>>> my_list = ['guest', 'demo', ':', 'eric', 'iverson', '\xe2\x80\x99s', 'itty', 'bitty', 'search', 'february', '16', 'th', ',', '2010', 'by', 'daniel', 'tunkelang', 'respond', 'i', '\xe2\x80\x99m', 'back', 'from', 'vacation', ',', 'and', 'still', 'digging', 'my', 'way', 'out', 'of', 'everything', 'that', '\xe2\x80\x99s', 'piled', 'up', 'while', 'i', '\xe2\x80\x99ve', 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', '\xe2\x80\x99d', 'share', 'with', 'you', 'a', 'demo', 'that', 'eric', 'iverson', 'was', 'gracious', 'enough', 'to', 'share', 'with', 'me', 'it', 'uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', 'experience', 'on', 'top', 'of', 'a', 'general', 'web', 'search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', 'application', 'retrieves', 'a', 'set', 'of', 'related', 'term', 'candidates', 'using', 'yahoo', '\xe2\x80\x99s', 'key', 'terms', 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', 'it', 'is', 'global', 'occurrence', 'count', '\xe2\x80\x93a', 'relevance', 'measure', 'similar', 'to', 'one', 'my', 'former', 'colleagues', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', 'has', 'rough', 'edges', ',', 'it', 'produces', 'nice', 'results', '\xe2\x80\x93especially', 'considering', 'the', 'simplicity', 'of', 'the', 'approach', 'here', '\xe2\x80\x99s', 'an', 'example', 'of', 'how', 'i', 'used', 'the', 'application', 'to', 'explore', 'and', 'learn', 'something', 'new', 'i', 'started', 'with', '["', 'information', 'retrieval', '"]', 'i', 'noticed', '\xe2\x80\x9c', 'interactive', 'information', 'retrieval', '\xe2\x80\x9d', 'as', 'a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', 'suggestions', 'looked', 'familiar', 'to', 'me', '\xe2\x80\x93but', 'an', 'unfamiliar', 'name', 'caught', 'my', 'attention', ':', '\xe2\x80\x9c', 'anton', 'leuski', '\xe2\x80\x9d', 'following', 'my', 'curiosity', ',', 'i', 'refined', 'again', 'looking', 'at', 'the', 'results', ',', 'i', 'immediately', 'saw', 'that', 'leuski', 'had', 'done', 'work', 'on', 'evaluating', 'document', 'clustering', 'for', 'interactive', 'information', 'retrieval', 'further', 'exploration', 'made', 'it', 'clear', 'this', 'is', 'someone', 'whose', 'work', 'i', 'should', 'get', 'to', 'know', '\xe2\x80\x93check', 'out', 'his', 'home', 'page', '!', 'i', 'can', '\xe2\x80\x99t', 'promise', 'that', 'you', '\xe2\x80\x99ll', 'have', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', 'i', 'encourage', 'you', 'to', 'try', 'eric', '\xe2\x80\x99s', 'demo', 'it', '\xe2\x80\x99s', 'simple', 'examples', 'like', 'these', 'that', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', '\xe2\x80\x99ll', 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', 'course', 'i', '\xe2\x80\x99ll', 'share', 'them', 'here']
>>> my_clean_list = [unidecode(x.decode('utf8')) for x in my_list]
>>> my_clean_list
['guest', 'demo', ':', 'eric', 'iverson', "'s", 'itty', 'bitty', 'search', 'february', '16', 'th', ',', '2010', 'by', 'daniel', 'tunkelang', 'respond', 'i', "'m", 'back', 'from', 'vacation', ',', 'and', 'still', 'digging', 'my', 'way', 'out', 'of', 'everything', 'that', "'s", 'piled', 'up', 'while', 'i', "'ve", 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', "'d", 'share', 'with', 'you', 'a', 'demo', 'that', 'eric', 'iverson', 'was', 'gracious', 'enough', 'to', 'share', 'with', 'me', 'it', 'uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', 'experience', 'on', 'top', 'of', 'a', 'general', 'web', 'search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', 'application', 'retrieves', 'a', 'set', 'of', 'related', 'term', 'candidates', 'using', 'yahoo', "'s", 'key', 'terms', 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', 'it', 'is', 'global', 'occurrence', 'count', '-a', 'relevance', 'measure', 'similar', 'to', 'one', 'my', 'former', 'colleagues', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', 'has', 'rough', 'edges', ',', 'it', 'produces', 'nice', 'results', '-especially', 'considering', 'the', 'simplicity', 'of', 'the', 'approach', 'here', "'s", 'an', 'example', 'of', 'how', 'i', 'used', 'the', 'application', 'to', 'explore', 'and', 'learn', 'something', 'new', 'i', 'started', 'with', '["', 'information', 'retrieval', '"]', 'i', 'noticed', '"', 'interactive', 'information', 'retrieval', '"', 'as', 'a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', 'suggestions', 'looked', 'familiar', 'to', 'me', '-but', 'an', 'unfamiliar', 'name', 'caught', 'my', 'attention', ':', '"', 'anton', 'leuski', '"', 'following', 'my', 'curiosity', ',', 'i', 'refined', 'again', 'looking', 'at', 'the', 'results', ',', 'i', 'immediately', 'saw', 'that', 'leuski', 'had', 'done', 'work', 'on', 'evaluating', 'document', 'clustering', 'for', 'interactive', 'information', 'retrieval', 'further', 'exploration', 'made', 'it', 'clear', 'this', 'is', 'someone', 'whose', 'work', 'i', 'should', 'get', 'to', 'know', '-check', 'out', 'his', 'home', 'page', '!', 'i', 'can', "'t", 'promise', 'that', 'you', "'ll", 'have', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', 'i', 'encourage', 'you', 'to', 'try', 'eric', "'s", 'demo', 'it', "'s", 'simple', 'examples', 'like', 'these', 'that', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', "'ll", 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', 'course', 'i', "'ll", 'share', 'them', 'here']
在这里,我使用了unidecode这个模块,把那些“花哨”的字符转换成最接近的ascii字符:
>>> for before, after in zip(my_list, my_clean_list):
... if before != after:
... print before, ' --> ', after
...
’s --> 's
’m --> 'm
’s --> 's
’ve --> 've
’d --> 'd
’s --> 's
–a --> -a
–especially --> -especially
’s --> 's
“ --> "
” --> "
–but --> -but
“ --> "
” --> "
–check --> -check
’t --> 't
’ll --> 'll
’s --> 's
’s --> 's
’ll --> 'll
’ll --> 'll
你可能能猜到,这看起来像是一些英文数据本来应该在单词的边界处进行分割,但这个操作做错了。如果是你的代码生成了这些数据,我建议你在问题的源头上解决这个问题!