我有关于此算法输出的问题。 例如:输入chunk([1, 2, 3, 4, 5, 6, 7, 8], 3)
它应该返回[[ 1, 2, 3], [4, 5, 6], [7, 8, '']]
,但它会返回[[7, 8, 6], [7, 8, 6], [7, 8, 6]]
。
但是,当在m_list
循环下定义for r in range(rows):
时,它会返回正确的值。
如果在循环m_list
之外定义for r in range(rows):
,我无法弄清楚为什么它会返回错误的值。可能是什么原因?
# --- Directions
# Given an array and chunk size, divide the array into many subarrays
# where each subarray is of length size
# --- Examples
# chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
# chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5, '']]
# chunk([1, 2, 3, 4, 5, 6, 7], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, '', '']]
import math
def chunk (array, size):
rows = 0
l = len(array)
if l % size == 0:
rows = l/size
else:
rows = int(math.floor(l/size) + 1)
m_list = ['' for e in range(size)]
m_matrix = [['' for g in range(size)] for w in range(rows)]
i = 0
for r in range(rows):
for u in range(size):
if i == l:
break
else:
m_list[u] = array[i]
i += 1
m_matrix[r] = m_list
return m_matrix
length = int(raw_input('how many elements you want in the array?: '))
m_inputArray = ['' for q in range(length)]
print 'Debug0:--> ' + str(m_inputArray)
for z in range(length):
p = int(raw_input('Enter the value at index %i: ' %(z)))
m_inputArray[z] = p
m_inputSize = int(raw_input('Enter the size: '))
result = chunk(m_inputArray, m_inputSize)
print result
答案 0 :(得分:1)
seems a bit over complicated. this is what i came up with. written for python 3 but does work in 2.
def pop_with_replace(array, index=0, blank=''):
try:
return array.pop(index)
except IndexError:
return blank
def chunk(array, size):
out = []
while array:
t_list = []
for i in range(size):
t_list.append(pop_with_replace(array))
out.append(t_list)
return out
if __name__ == '__main__':
print(chunk(list(range(10)), 3))
there's some things we could change as well. like removing this method pop_with_replace for a ternary operator? i didn't put this in the first solution as they can be awkward to read if not used to them.
t_list.append(array.pop() if array else '')
looking at this we could roll it all up into a list comp. but we're starting to get hard to read.
While array:
out.append([array.pop(0) if array else '' for x in range(size)]
but it does leave the final code looking nice and small.
def chunk(array, size):
out = []
while array:
out.append([array.pop(0) if array else '' for x in range(size)])
return out
答案 1 :(得分:1)
您的代码有几个问题。首先,snapshot.getData()["env"]
的每个循环u
的起始值都是前一个列表(所以第一次是m_list
,但第二次是['','','']
,第三次它是[1,2,3]
。这意味着,由于第三次只有一个值留在数组中,只有[4,5,6]
中的第一个值被重新定义,导致m_list为m_list
。< / p>
其次,通过说:[7,5,6]
您正在为m_list创建引用,而不是将m_matrix[u] = m_list
复制到m_list
。这意味着一旦m_matrix
发生变化,m_list
中的值也会发生变化。这意味着最终您将m_matrix
定义为m_matrix
,从而产生[m_list,m_list,m_list]
的结果。对此的解决方案是切片[[7,5,6],[7,5,6],[7,5,6]]
,如下所示:m_list
。
这就是我要做的事情:
m_matrix = m_list[:]
如果您不再需要原始数组,也可以删除def chunk(inputarray,size):
array = inputarray[:]
m_matrix = []
while len(array) > 0:
if len(array[:size]) < size:
array.extend(['' for j in range(size-len(array[:size]))])
m_matrix.append(array[:size])
del array[:size]
return m_matrix
代码行。此外,可能不是最快/最好的方式,但我只是想提供一些快速的东西。这是在 python 2.7 中完成的,所以如果您使用其他版本,则可能需要更改某些内容。
答案 2 :(得分:0)
在此示例中,只有一个m_list用于更新值,最终结果为[m_list,m_list,m_list ... m_list]。 如果在循环中定义m_list,则将在每个循环传递中创建新列表。
您可以直接分配给m_matrix [r] [u] = array [i]
请注意,您使用的是列表,而不是真正的矩阵,m_matrix [r] = m_list将参考m_list列表替换第r个位置的列表。