在元组python中迭代和替换单词

2024-04-25 21:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想遍历这个元组,对于每一行,迭代这些单词,使用regex查找并替换一些单词(确切地说是互联网地址),同时将它们保留为行。在

aList=
[
  "being broken changes people, \nand rn im missing the old me", 
  "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point", 
  "#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a l... T.CO/CHPBRVH9WKk", 
  "@_EdenRodwell \ud83d\ude29\ud83d\ude29ahh I love you!! Missing u, McDonald's car park goss soon please \u2764\ufe0f\u2764\ufe0fxxxxx", 
  "This was my ring tone, before I decided change was good and missing a call was insignificant T.CO?BUXLVZFDWQ", 
  "want to go on holiday again, missing the sun\ud83d\ude29\u2600\ufe0f"
]

下面的代码几乎做到了这一点,但它将列表分解为以行分隔的单词:

^{pr2}$

我希望每行都有结果,除了互联网地址:

[
  "being broken changes people, \nand rn im missing the old me", 
  "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point", 
  "#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a ", 
  "@_EdenRodwell \ud83d\ude29\ud83d\ude29ahh I love you!! Missing u, McDonald's car park goss soon please \u2764\ufe0f\u2764\ufe0fxxxxx", 
  "This was my ring tone, before I decided change was good and missing a call was insignificant", 
  "want to go on holiday again, missing the sun\ud83d\ude29\u2600\ufe0f"
]

谢谢


Tags: theyou地址互联网单词changesmissingwas
2条回答

从这里:

re.sub(r"^[http](.*)\/(.*)$", "", line)

在我看来,好像你希望你所有的网址都在这一行的末尾。在这种情况下,请尝试:

^{pr2}$

这里,http://匹配以http://开头的任何内容。.*匹配下面的所有内容。在

示例

以下是添加了一些URL的列表:

aList = [
  "being broken changes people, \nand rn im missing the old me",
  "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point",
  "#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a http://example.com/CHPBRVH9WKk",
  "@_EdenRodwell ahh I love you!! Missing u, McDonald's car park goss soon please xxxxx",
  "This was my ring tone, before I decided change was good and missing a call was insignificant http://example.com?BUXLVZFDWQ",
  "want to go on holiday again, missing the sun"
  ]

结果如下:

>>> [re.sub('http://.*', '', s) for s in aList]
['being broken changes people, \nand rn im missing the old me',
 "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point",
 '#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a ',
 "@_EdenRodwell ahh I love you!! Missing u, McDonald's car park goss soon please xxxxx",
 'This was my ring tone, before I decided change was good and missing a call was insignificant ',
 'want to go on holiday again, missing the sun']

你的问题有点不清楚,但我想我明白你的意思

newlist = [re.sub(r"{regex}", "", line) for line in alist]

应该迭代字符串列表,并使用python列表理解将匹配regex模式的任何字符串替换为空字符串

旁注:

仔细看看你的正则表达式,它看起来不像你想的那样 我会看一下这个关于匹配regex中的url的栈over-flow帖子

Regex to find urls in string in Python

相关问题 更多 >