python - 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
尝试了很多变化,但没有运气...
解决方案
#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
推荐阅读
- javascript - 如何使用多个按钮触发相同的下拉/弹出框?
- android - Android - 如何将渐变颜色应用于位图?
- r - 没有fread函数?
- kotlin - 如何通过两个属性和整理器对对象列表进行排序
- javascript - 无法找出图表专家的 CSS 文件
- python - 创建一个基于空的numpy数组并附加现有数组
- hive - 根据 Hive 中的其他列查找列中时间戳的差异
- django - 表单验证不起作用 Django 并仅在消息上打印
- c# - 在一个视图页面上将 AspNetCore MVC 视图组合在一起的问题
- python - Keras - Get mode of 2 one hot arrays