简体   繁体   English

从列表列表中删除用户名

[英]Remove username from a list of lists

I have a list of lists regarding tweets and I need to remove the username. 我有一个关于推文的列表列表,我需要删除用户名。

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

The main problem is that I don't know how to work with a list of lists. 主要问题是我不知道如何使用列表列表。 I tried the following code among other things but did not work: 我尝试了以下代码,但没有工作:

import re

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

Please help. 请帮忙。

You can use a nested list comprehension to filter strings that starts with @ (assuming your list of lists is stored as variable l ): 您可以使用嵌套列表推导来过滤以@开头的字符串(假设您的列表列表存储为变量l ):

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

This returns: 返回:

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

Use list iterations: 使用列表迭代:

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