我查了一下,确实找到了一些帮助,但不幸的是,它们都使用了一个名为replace()的函数,而这个函数在我必须使用的程序中并不存在。在
def getWordList(minLength, maxLength):
url = "http://wordlist.ca/list.txt"
flink = urllib2.urlopen(url)
# where the new code needs to be to strip away the extra symbol
for eachline in flink:
if minLength+2<= len(eachline) <=maxLength+2:
WordList.append(eachline.strip())
return(WordList)
字符串是不可变的,所以我需要为列表中的每个单词创建一个新的字符串,并删除一个字符。在
^{pr2}$
Python字符串是不可变的,但是它们有返回新字符串的方法
退货
^{pr2}$可以通过将子字符串替换为空字符串来删除它:
退货
为了强调方法是如何工作的,它们是内置于字符串对象中的函数。它们也可用作类方法:
退货
如果你有一个字符串列表:
可以用
for
循环替换其中的子字符串:打印输出:
由于字符串是不可变的,所以您的列表实际上不会更改(打印出来并查看),但是您可以使用列表理解功能创建一个新列表:
印刷品:
相关问题 更多 >
编程相关推荐