繁体   English   中英

从列表列表中删除用户名

[英]Remove username from a list of lists

我有一个关于推文的列表列表,我需要删除用户名。

[['@Hegelbon','That','heart','sliding','into','the','waste','basket','.',':('],['“','@ketchBurning',':','I','hate','Japanese','call','him','"','bani','"',
':(',':(','”','Me','too'], ... ]

主要问题是我不知道如何使用列表列表。 我尝试了以下代码,但没有工作:

import re

    for element in tweets:
        for word in element:
            re.sub('@[^\s]+','', tweets)

请帮忙。

您可以使用嵌套列表推导来过滤以@开头的字符串(假设您的列表列表存储为变量l ):

[[i for i in s if not i.startswith('@')] for s in l]

返回:

[['That', 'heart', 'sliding', 'into', 'the', 'waste', 'basket', '.', ':('], ['“', ':', 'I', 'hate', 'Japanese', 'call', 'him', '"', 'bani', '"', ':(', ':(', '”', 'Me', 'too']]

使用列表迭代:

mylist = [['@Hegelbon','That','heart','sliding','into','the','waste','basket','.',':('],['“','@ketchBurning',':','I','hate','Japanese','call','him','"','bani','"',
':(',':(','”','Me','too'] ]



newlist = [ [item for item in sublist if not item.startswith('@')] for sublist in mylist]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM