随机化一个数组到两个数组
我有一个数字列表,我需要分裂成不同大小的相应数组,但是它构成了数组分裂的所有组合。例如,如果我有一个数组a=[1,2,3,4,5]
,并且我想将它分割为一个大小为3的数组,而另一个为2.随机化一个数组到两个数组
所以我想制作两个数组来保存每个数组,并且因为具有相同数量的大小3和2号阵列我可以匹配起来,然后执行我的测试。 (这是一个统计类,所以如果有更好的scipy或numpy实现,我很乐意听到它,因为我想移动使用这些,最后我想了解不同方法之间的所有差异阵列)
但在这里我的代码是提前
import itertools
#defines the array of numbers and the two columns
number = [53, 64, 68, 71, 77, 82, 85]
col_one = []
col_two = []
#creates an array that holds the first four
results = itertools.combinations(number,4)
for x in results:
col_one.append(list(x))
print col_one
#attempts to go through and remove those numbers in the first array
#and then add that array to col_two
for i in range(len(col_one)):
holder = number
for j in range(4):
holder.remove(col_one[i][j])
col_two.append(holder)
感谢
编辑:现在看来,这搞砸代码的间距 - 我向你保证的间距是确定的,虽然当我运行代码我无法从holder
中删除一个项目,因为它不在那里。
我测试了你的代码,我看到了问题。在这段代码中,
for i in range(len(col_one)):
holder = number
for j in range(4):
holder.remove(col_one[i][j])
col_two.append(holder)
行holder = number
不会复制number
,它只是给number
第二个名字,holder
。然后,当你从holder
中删除东西时,它们也会从number
中删除,所以当循环再次出现时,数字中的数字就减少了四个。无限广告。
你想使数量的一个副本:
for i in range(len(col_one)):
holder = list(number)
for j in range(4):
holder.remove(col_one[i][j])
col_two.append(holder)
。这将创建number
一个新的列表称为holder
。现在只有holder
被更改。
holder = number[:]
也可以工作。
您还应该使用for
的全部潜力避免指数的变量:
for num_list in col_one:
holder = list(number)
for num in num_list:
holder.remove(num)
col_two.append(holder)
这做同样的事情,更容易阅读和可能更快启动。
现在为下一步,列表解析。这是避免嵌套循环的好方法。
for c1_list in col_one:
c2_list = [n for n in number if n not in c1_list]
col_two.append(c2_list)
这和上面的做法是一样的。你甚至可以让这一个班轮:
col_two = [[n for n in number if n not in c1_list] for c1_list in col_one]
它一起结合:
number = [53, 64, 68, 71, 77, 82, 85]
col_one = list(itertools.combinations(number, 4))
col_two = [[n for n in number if n not in c1_list] for c1_list in col_one]
这个解决方案应该是大数组更有效,因为它使用一个set
来计算的指数第二阵列,和预分配存储器:
import scipy as sp
import itertools
number = sp.array([53, 64, 68, 71, 77, 82, 85])
len_number = len(number)
# number of combinations
ncomb = sp.comb(len_number, 4)
# pre-allocate memory
col_one = sp.empty((ncomb, 4))
col_two = sp.empty((ncomb, len_number-4))
indices = range(len_number)
indices_set = set(indices)
for i, idx in enumerate(itertools.combinations(indices, 4)):
col_one[i,:] = number[list(idx)]
col_two[i,:] = number[list(indices_set.difference(idx))]
甚至更有效的解决方案可以通过产生长度len_number
续的所有布尔阵列来获得癌宁正好4 True
值,这将允许你写
col_one[i,:] = number[bool_idx]
col_two[i,:] = number[sp.logical_not(bool_idx)]
如果可能的话,我会避免通过计算循环所需的统计数据,并将它们存储不是存储col_one
和col_two
。
我为此道歉,但感谢您的帮助。 – tshauck 2011-03-03 06:20:31