首页 > 解决方案 > Python函数不返回变量

问题描述

我不明白为什么 print 函数正在打印预期的输出但是返回,返回 None ...

这是我的功能

#json search function:
def searchfunction(searches,query,myresults,i = 0):

    print('i:',i,'nr of searches',len(searches),'nr results:',len(myresults))
    newresults = []

    if i <= len(searches)-1:
        for item in myresults:
            for key in item:
                if key == searches[i]:
                    #print(key, item[key],query.split(','))
                    if item[key] in query.split(','):
                        #print(key, item[key], query.split(','))
                        newresults.append(item)
        i += 1
        myresults = newresults
        tmp = searchfunction(searches, query, myresults, i)
    else:
        print('print result:',myresults)
        return myresults

使用功能:

searches = ["Product featured","Master Messaging","Phase","length","Asset 1"]

query = "ipl5,Female 25+,catch,6,copy_press-play"
searchresults = searchfunction(searches, query, results)

print('result?',searchresults)

输出:

i: 0 nr of searches 5 nr results: 448
i: 1 nr of searches 5 nr results: 132
i: 2 nr of searches 5 nr results: 66
i: 3 nr of searches 5 nr results: 18
i: 4 nr of searches 5 nr results: 9
i: 5 nr of searches 5 nr results: 1

print result: [{'id': '4', 'label': 'GFX_watching-tv-netflix_ipl5_Female 25+_catch_6', 'Base name': 'watching-tv-netflix', 'Product featured': 'ipl5', 'Master Messaging': 'Female 25+', 'Phase': 'catch', 'length': '6', 'Asset 1': 'copy_press-play', 'options overrides': '', 'Asset 2': 'copy_on-smooth-skin', 'Asset 3': 'endcard_long-lasting-smooth-skin-your-way', 'Asset 4': ''}]

#???
result? None

尝试了很多变化,但没有运气...

标签: pythonjson

解决方案


#json search function:
def searchfunction(searches,query,myresults,i = 0):

    print('i:',i,'nr of searches',len(searches),'nr results:',len(myresults))
    newresults = []

    if i <= len(searches)-1:
        for item in myresults:
            for key in item:
                if key == searches[i]:
                    #print(key, item[key],query.split(','))
                    if item[key] in query.split(','):
                        #print(key, item[key], query.split(','))
                        newresults.append(item)
        i += 1
        myresults = newresults
        return searchfunction(searches, query, myresults, i)
    else:
        return myresults

推荐阅读