[英]Can I run the same function multiple times with the return value as the argument?
The code is supposed to return a final color value if you give it a string of colours, like如果你给它一串颜色,代码应该返回一个最终的颜色值,比如
Colour here: GG, BG, RG, BR此处颜色:GG、BG、RG、BR
Becomes colour: G, R, B, G变色:G、R、B、G
For example: 'RGBG' would return 'B' Here, i wrote a function to assign color tags (Assign) and another funtion which would feed the broken down primary string.例如:'RGBG' 将返回 'B' 在这里,我写了一个 function 来分配颜色标签(分配)和另一个将提供分解的主字符串的函数。 It all works fine when i manually type the new_value = assign(main(again)) multiple times depending on the size of the primary string(RGBG -> BRR -> GR ->B; 4 times here but i run the first iteration in advance because the again parameter would have a value to begin with), but returns an empty value when i use again = assign(main(again)) even though i have given the parameter a value on the previous lines so it's not a "undefined param" error.. i think.当我根据主字符串的大小手动键入new_value = assign(main(again))时一切正常(RGBG -> BRR -> GR ->B; 4 次,但我在advance 因为 again 参数将有一个值开头),但是当我再次使用 = assign(main(again))时返回一个空值,即使我在前面的行中给了参数一个值所以它不是“未定义的”参数”错误..我想。
I'm pretty new to this and would greatly appreciate it if you could point me in the right way.我对此很陌生,如果您能以正确的方式指出我,我将不胜感激。 Hoping this message finds you in safety:)希望这条消息能让你平安无事:)
def assign(xlist):
for x in range(len(xlist)):
if xlist[x] in ('BG', 'GB', 'RR'): xlist[x] = 'R'
elif xlist[x] in ('RG', 'GR', 'BB'): xlist[x] = 'B'
elif xlist[x] in ('BR'. 'RB', 'GG'): xlist[x] = 'G'
return ''.join(xlist)
def main(xrow):
nrow = []
for i in range(len(xrow)-1):
nrow.append(xrow[i] + xrow[i+1])
return(nrow)
def triangle(row):
global again
again = assign(main(row))
print(row)
print(again)
for k in range(len(row)-1):
again = assign(main(again))
return again
You could do it like this:你可以这样做:
s = 'RGBG'
dict_map = {
'BG':'R',
'GB':'R',
'RR':'R',
'RG':'B',
'GR':'B',
'BB':'B',
'BR':'G',
'RB':'G',
'GG':'G'
}
def process(s):
while len(s)!=1:
s = "".join([dict_map["".join(x)] for x in zip(s[:-1], s[1:])])
return s
print(process(s))
# B
Lets break it down:让我们分解一下:
you can use a dictionary to translate values to other values based on their keys:您可以使用字典根据键将值转换为其他值:
dict_map["BG"]
will return "R". dict_map["BG"]
将返回“R”。 To use the dict we need a list of elements with 2 characters.要使用字典,我们需要一个包含 2 个字符的元素列表。
We create that list by using the following: [x for x in zip(s[:-1], s[1:])]
This will return a list of overlapping tupels.我们使用以下命令创建该列表: [x for x in zip(s[:-1], s[1:])]
这将返回重叠元组的列表。 Eg ("R", "G")例如(“R”,“G”)
Since we cant use this tupels with the dictionary we "".join()
them together resulting eg in "RG".由于我们不能将此元组与字典一起使用,因此我们将它们"".join()
在一起,从而产生例如“RG”。
Now we ask the dictionary which values is used for that key by using dict_map["RG"]
and get "R" in this case.现在我们通过使用dict_map["RG"]
询问字典哪些值用于该键,并在本例中得到“R”。
The whole thing is done here: [dict_map["".join(x)] for x in zip(s[:-1], s[1:])]
整个事情在这里完成: [dict_map["".join(x)] for x in zip(s[:-1], s[1:])]
Now we have again a list of translated values.现在我们又有了一个翻译值列表。 We "".join()
it together again to get a new string: "BRR".我们再次将它"".join()
在一起得到一个新的字符串:“BRR”。 We repeat the whole process until the string has a length of 1.我们重复整个过程,直到字符串的长度为 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.