[英]Accessing a variable value from a python function inside another function
I have one function that calls for the function rlEncode which is supposed to take the data list and compress it so it counts how many values in a row there are and would output for example [1, 5, 3, 2, 5, 6] and so on.我有一个函数调用函数 rlEncode,该函数应该获取数据列表并对其进行压缩,以便计算一行中有多少个值,并输出例如 [1, 5, 3, 2, 5, 6]等等。 But when I run it just out puts the [1,5] again and again and not moving the n value over however so many spaces in the list.但是当我运行它时,它一次又一次地将 [1,5] 放入,而不是将 n 值移动到列表中的这么多空格上。 How would I get the n value from the function rlEncode to be used in the other function?我如何从函数 rlEncode 中获取要在另一个函数中使用的 n 值?
def rlEncode(n, z, data_list): while data_list[n] == data_list[n+1]: z = z + 1 n = n + 1 while data_list[n] != data_list[n+1]: return n return z def unitTest( ): c = 0 n = 0 z = 1 data_list = [1,1,1,1,1,3,3,5,5,5,5,5,5,6,8,8,1,1,1,5,5,5,5,13,14, 14] compress_list = [ ] while c < (len(data_list)): n = rlEncode(n, 1, data_list) z = rlEncode(0, z, data_list) rlEncode(0, 1, data_list) compress = [data_list[n], z] c = c + 1 compress_list = compress_list + compress print(compress_list) n = n+1
Python passes immutable objects by value. Python 按值传递不可变对象。 See this previous answer: How do I pass a variable by reference?请参阅上一个答案: 如何通过引用传递变量?
The simplest solution in your case is to have the inner function return the value of n
to the outer function, which assigns it to its local n
.在您的情况下,最简单的解决方案是让内部函数将n
的值返回给外部函数,后者将其分配给其本地n
。
compress
is a list, which is mutable, so you can use +=
to mutate the list in place rather than creating a new local variable. compress
是一个列表,它是可变的,所以你可以使用+=
来改变列表,而不是创建一个新的局部变量。
I also added a check against the list length, otherwise the references to n+1
will cause IndexError.我还添加了对列表长度的检查,否则对n+1
的引用将导致 IndexError。
I also don't think you need the second while
loop in rlEncode but I'll leave that up to you to sort out... :)我也不认为你需要 rlEncode 中的第二个while
循环,但我会留给你来整理...... :)
def rlEncode(n, z, data_list, compress):
while (n < len(data_list)-1) and (data_list[n] == data_list[n+1]):
z = z + 1
n = n + 1
while (n < len(data_list)-1) and (data_list[n] != data_list[n+1]):
compress += [data_list[n], z]
n = n + 1
return n
def unitTest(data_list):
c = 0
n = 0
compress = []
while c < (len(data_list)):
n = rlEncode(n, 1, data_list, compress)
c = c + 1
return ('list: ', data_list, "compressed list: ", compress)
sample_data = [1,1,1,1,1,3,3,5,5,5,5,5,5,6,8,8,1,1,1,5,5,5,5,13, 14, 14]
unitTest(sample_data)
I like doing these things recursively.我喜欢递归地做这些事情。 Python isn't great with recursion but I wanted to see how it's done so I guess I'll share: Python 在递归方面不是很好,但我想看看它是如何完成的,所以我想我会分享:
def compress(lst):
if not lst:
return []
current = lst[0]
result = compress(lst[1:])
if not result:
# item is last
return [(1, current)]
nxt = result[0][1]
if current == nxt:
# items is same as next
return [(result[0][0] + 1, current)] + result[1:]
# different items
return [(1, current)] + result
To use it:要使用它:
print [x[0] for x in compress(lst)]
The obvious and efficient way would be a generator:明显而有效的方法是生成器:
def group_gen(lst):
buff = []
for item in lst:
if buff and item == buff[0]:
buff.append(item)
else:
if buff:
yield buff
buff = [item]
if buff:
yield buff
print list(map(len, group_gen(lst)))
Check it using:使用以下方法检查:
print list(map(len, group_gen(lst)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.