[英]Apply function to each element of list recursion
I have a function that takes a keyword as input and returns a list of related keywords.我有一个将关键字作为输入并返回相关关键字列表的函数。 What I need is a function that will get the related keywords and then run again on each keyword on the returned list, and so on for a defined number of times.
我需要的是一个函数,它将获取相关的关键字,然后在返回列表中的每个关键字上再次运行,依此类推定义的次数。
def find_related(kw):
#magic happens
return related_kws
'''
Example:
>>>find_related('eat')
{
'base_kw': 'eat'
'related_kws': ['eat pasta', 'eat meat', 'eat fish']
}
'''
What I need is something like this:我需要的是这样的:
def difficult_function(kw, cycles):
#cycles determines number of times to run recursion
#magic beyond my league happens...
return more_related_keywords
'''
EXAMPLE #1
>>>difficult_function('eat', 2)
{
'base_kw': 'eat'
'related_kws': {
'eat pasta': ['eat cold pasta', 'eat italian pasta', 'eat instant pasta'],
'eat meat': ['eat raw meat', 'eat cow meat', 'eat roasted meat'],
'eat fish': ['eat raw fish', 'eat rotten fish', 'eat fresh fish']
}
}
'''
'''
EXAMPLE #2
>>>difficult_function('eat', 3)
{
'base_kw': 'eat'
'related_kws': {
'eat pasta': {
'eat cold pasta': [
'eat cold pasta with sauce',
'eat cold pasta and wine',
'eat cold pasta at night'
],
'eat italian pasta': [
'eat italian cheap pasta',
'eat best italian pasta',
'eat italian pasta and pesto'
],
'eat instant pasta': [
'eat instant pasta snack',
'eat instant cheese pasta',
'eat healthy instant pasta'
]
},
'eat meat': {
'eat raw meat': [
'eat raw meat dangerous',
'eat raw chicken meat',
'eat raw meat with lemon'
],
'eat cow meat': [
'eat cow meat and salad',
'eat cow meat and rice,
'eat cow meat benefits'
],
'eat roasted meat': [
'eat roasted meat sandwich',
'eat cold roasted meat',
'don\'t eat roasted meat'
]
},
'eat fish': {
'eat raw fish': [
'eat raw fish sushi',
'eat raw fish health', 'eat raw fish japanese'],
'eat rotten fish': [
'eat rotten fish bacteria',
'eat rotten fish death',
'eat rotten fish stomach'
],
'eat fresh fish': [
'eat fresh fish restaurant',
'eat fresh fish with lemon',
'eat fresh fish at home']
}
}
}
'''
I know I'm supposed to share here what I already tried, but the truth is I don't even know where to begin.我知道我应该在这里分享我已经尝试过的东西,但事实是我什至不知道从哪里开始。 Any ideas?
有任何想法吗?
EDIT:编辑:
The function below simulates the functioning of find_related(kw).下面的函数模拟了 find_related(kw) 的功能。 My apologies if the question isn't presented in the appropiate way, I'm a total noob
如果问题没有以适当的方式提出,我很抱歉,我是个菜鸟
import string
import random
def find_related(kw):
# A lot of magic happens, the list comprehension
# below simulates the magic
related_kws = [
kw + ' ' + ''.join(
random.choice(string.ascii_lowercase) for
_ in range(4)) for i in range(3)
]
results = {
'base_kw': kw,
'related_kws': related_kws
}
return results
Mock function that matches examples given.匹配给定示例的模拟函数。
def find_related(kw):
'''mock function'''
# A lot of magic happens, this simulates the magic
d = {'eat': ['eat pasta', 'eat meat', 'eat fish'],
'eat pasta': ['eat cold pasta', 'eat italian pasta', 'eat instant pasta'],
'eat meat': ['eat raw meat', 'eat cow meat', 'eat roasted meat'],
'eat fish': ['eat raw fish', 'eat rotten fish', 'eat fresh fish'],
'eat cold pasta': ['eat cold pasta with sauce', 'eat cold pasta and wine', 'eat cold pasta at night'],
'eat italian pasta': ['eat italian cheap pasta', 'eat best italian pasta', 'eat italian pasta and pesto'],
'eat instant pasta': ['eat instant pasta snack', 'eat instant cheese pasta', 'eat healthy instant pasta'],
'eat raw meat': ['eat raw meat dangerous', 'eat raw chicken meat', 'eat raw meat with lemon'],
'eat cow meat': ['eat cow meat and salad', 'eat cow meat and rice', 'eat cow meat benefits'],
'eat roasted meat': ['eat roasted meat sandwich', 'eat cold roasted meat', 'don\'t eat roasted meat'],
'eat raw fish': ['eat raw fish sushi', 'eat raw fish health', 'eat raw fish japanese'],
'eat rotten fish': ['eat rotten fish bacteria', 'eat rotten fish death', 'eat rotten fish stomach'],
'eat fresh fish': ['eat fresh fish restaurant', 'eat fresh fish with lemon', 'eat fresh fish at home']
}
results = {
'base_kw': kw,
'related_kws': d[kw]
}
Here's the answer in case someone is looking for something similar.如果有人正在寻找类似的东西,这就是答案。 Again my apologies if the question was poorly presented.
如果问题表述不当,我再次表示歉意。
import pprint
import string
import random
def find_related(kw):
# A lot of magic happens, the list comprehension
# below simulates the magic
related_kws = [
kw + ' ' + ''.join(
random.choice(string.ascii_lowercase) for
_ in range(4)) for i in range(3)
]
results = {
'base_kw': kw,
'related_kws': related_kws
}
return results
def difficult_function(kw, cycles):
return {
'base_kw': kw,
'related_kws': rec_kw(kw, cycles)
}
def rec_kw(kw, cycles):
if cycles == 1:
return find_related(kw)
else:
result = {}
for k in find_related(kw)['related_kws']:
result[k] = rec_kw(k, cycles-1)
return result
pprint.pprint(difficult_function('eat', 3))
This code do something you need.此代码执行您需要的操作。 In order to see the results, I wrote it to work with global list INP, but there principle will work for you.
为了看到结果,我写了它来与全局列表 INP 一起使用,但是这个原则对你有用。
INP = ["Lorem", "Ipsum", "dolor", "atea"]
def recursive (kw, max, ref=None):
ret = None
if ref is None:
ret = {'base_kw' : kw, 'related_kws' : {}}
ref = ret ['related_kws']
if max != 1:
words = [' '.join (INP [0: len (INP) - max + 1]), ]
for kw in words:
ref [kw] = {}
ref = ref [kw]
recursive (kw, max -1, ref)
else:
ref [kw] = [' '.join (INP)]
return ret
print (recursive ("Lorem", 3))
This is the output:这是输出:
{'related_kws': {'Lorem Ipsum': {'Lorem Ipsum dolor': {'Lorem Ipsum dolor': ['Lorem Ipsum dolor atea']}}}, 'base_kw': 'Lorem'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.