合并返回的列表`关于芬德尔()`

2024-05-21 04:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个包含多个字符串的文件;每个字符串都有以$开头的单词,我希望将所有以$开头的单词存储在一个文件中,作为单个列表(mainList)。
我编写了这个函数来逐行读取文件。我得到的临时数组在一行中填充了以$开头的所有值,但无法添加返回的所有单个列表关于芬德尔作为单一主列表。在


Tags: 文件to字符串in列表forlineall
1条回答
网友
1楼 · 发布于 2024-05-21 04:29:47

尝试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)

相关问题 更多 >