繁体   English   中英

Python函数不返回变量

[英]Python function not return variable

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

这是我的功能

#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

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

#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM