append vs. extend。我在这里得到了答案我只需要使用extend关键字而不是append。
def extractDollar(line):
global mainList
temp=[]
#lowercasing all the string
line=line.lower()
#storing all word starting with $ in a line in temp
#then adding that to existing list mainList
#to form a single list and removing empty value
temp= re.findall(r'$\w+',line)
mainList=mainList+[j for i in zip(mainList,temp) for j in i]
mainList= filter(None, mainList)
return line
我有一个包含多个字符串的文件;每个字符串都有以$开头的单词,我想将以$开头的所有单词作为单个List(mainList)存储在文件中。
我写了这个函数来逐行读取文件。我正在获取临时数组,其中包含以$开头的所有值,但不能将re.findall返回的所有单个列表添加为单个主列表。
答案 0 :(得分:0)
尝试reduce(sum, line)
:
def extractDollar(line):
global mainList
temp=[]
#lowercasing all the string
line=line.lower()
#storing all word starting with $ in a line in temp
#then adding that to existing list mainList
#to form a single list and removing empty value
temp= re.findall(r'$\w+',line)
mainList=mainList+[j for i in zip(mainList,temp) for j in i]
mainList= filter(None, mainList)
return reduce(sum,line)