我必须在表格中列出大量的单词:
['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
然后使用条带功能,将其转换为:
['this', 'is', 'a', 'list', 'of', 'words']
我以为我写的东西会起作用,但我一直都会收到错误说:
“‘list’ object has no attribute ‘strip'”
这是我试过的代码:
strip_list = []
for lengths in range(1,20):
strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
strip_list.append(lines[a].strip())
解决方法:
>>> my_list = ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
>>> map(str.strip, my_list)
['this', 'is', 'a', 'list', 'of', 'words']