【发布时间】:2016-08-03 02:30:33
【问题描述】:
我想遍历这个元组,对于每一行,遍历单词以使用正则表达式查找和替换一些单词(确切地说是互联网地址),同时将它们保留为行。
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"
]
下面的代码几乎可以做到这一点,但它将列表分成由行分隔的单词:
i=0
while i<len(aList):
for line in aList[i].split():
line = re.sub(r"^[http](.*)\/(.*)$", "", line)
print (line)
i+=1
我希望得到除每行中的互联网地址之外的结果:
[
"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"
]
谢谢
【问题讨论】:
-
你有一个无限循环。 Python 不能用 plus plus,你必须做 plus equals。
-
这是 i++、bpachev 的拼写错误,我已经更正了。 John1024,代码运行时没有出现拼写错误,我不允许在问题中输入互联网地址。地址的例子是(T.CO?BUXLVZFDWQ);我已经把它们全部大写了。
-
regex
[http]表示 1 个字符,即 h、t 或 p。 regexhttp表示 4 个字符,依次为 h、t、t 和 p。 -
是的,Aprillion。我想通了。谢谢
标签: python regex loops replace tuples