小编Ser*_*ado的帖子

通过在python中排列元素来最小化矩阵中的列总和

我有以下矩阵:

([2, 5, 5, 10]
 [7, 1, 4, 1]
 [1, 3, 3, 9])
Run Code Online (Sandbox Code Playgroud)

如果列总和,则结果为:

[10, 9, 12, 20]
Run Code Online (Sandbox Code Playgroud)

我的目标是确定对不同行中的元素进行排序的最佳方法,以便最小化列总和中的最大元素.

例如,一种可能性是:

([2, 5, 5, 10]
 [7, 1, 4, 1]
 [1, 9, 3, 3])
Run Code Online (Sandbox Code Playgroud)

如果列总和,则结果为:

[10, 15, 12, 14]
Run Code Online (Sandbox Code Playgroud)

这是比第一个更好的解决方案.

最简单的方法是检查所有可能的排列,但随着矩阵的增长,这种方法在python中变得非常慢.

有没有想过以更快的方式做到这一点?

python algorithm matrix

3
推荐指数
1
解决办法
498
查看次数

在两个列表之间随机打乱元素

我有两个清单:

list_1 = [a, b, c, d, e]

list_2 = [f, g, h, i]
Run Code Online (Sandbox Code Playgroud)

我想要做的是一种在列表之间随机打乱两个元素的方法,例如:

shuffle_two_lists(list_1, list_2 )

list_1 = [g, b, c, d, e]

list_2 = [f, a, h, i]
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

python random shuffle list

0
推荐指数
1
解决办法
617
查看次数

如果数组行具有不同的长度,则对数组列求和

我想知道如果行具有不同的长度,是否可以对数组的列求和.

这是我要总结的数组:

input_val = [[1, 2, 3, 5],
             [1, 2, 3],
             [1, 2, 3, 4, 5]]
Run Code Online (Sandbox Code Playgroud)

这将是结果:

output_val = [3, 6, 9, 9, 5]
Run Code Online (Sandbox Code Playgroud)

我想到了为行添加零的可能性:

input_val = [[1, 2, 3, 5, 0],
             [1, 2, 3, 0, 0],
             [1, 2, 3, 4, 5]]
Run Code Online (Sandbox Code Playgroud)

然后总结列,但也许有更好的方法来做到这一点?

python arrays sum list python-2.7

-1
推荐指数
1
解决办法
131
查看次数

标签 统计

python ×3

list ×2

algorithm ×1

arrays ×1

matrix ×1

python-2.7 ×1

random ×1

shuffle ×1

sum ×1