簡體   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