def remove_cruft(s):
return s[4:-4]
sites=['www.hattrick.com', 'www.google.com', 'www.wampum.net', 'www.newcom.com']
[remove_cruft(s) for s in sites]
结果:
['hattrick', 'google', 'wampum', 'newcom']
如果你知道你要删除的所有字符串,可以使用replace来摆脱它们.如果您不确定所有网址都以“www”开头,或者TLD的长度不是三个字符,那么这一点很有用.
def remove_bad_substrings(s):
badSubstrings = ["www.", ".com", ".net", ".museum"]
for badSubstring in badSubstrings:
s = s.replace(badSubstring, "")
return s
sites=['www.hattrick.com', 'www.google.com',
'www.wampum.net', 'www.newcom.com', 'smithsonian.museum']
[remove_bad_substrings(s) for s in sites]
结果:
['hattrick', 'google', 'wampum', 'newcom', 'smithsonian']