将 python 中的嵌套排序循环转换为 javascript
Transforming a nested sort loop in python to javascript
我正在尝试将排序和循环从 python 转换为 javascript,但无法弄清楚:(
G = {
0: {0: 100000, 1: 294, 2: 366},
1: {0: 294, 1: 100000, 2: 140},
2: {0: 366, 1: 140, 2: 100000}
}
我在 python 中有这个 for 循环:
for W,u,v in sorted((G[u][v], u, v) for u in G for v in G[u]):
并想转换成javascript(必要时使用下划线),并添加魔法使其变成这样:
var y = [
[140, 1, 2),
[140, 2, 1],
[294, 0, 1],
[294, 1, 0],
[366, 0, 2],
[366, 2, 0],
[100000, 0, 0],
[100000, 1, 1],
[100000, 2, 2]]
我已经能够使用两个 for in
循环获得数组中的结果列表:
for (u in x) {
for (v in x[u]) {
console.log([x[u][v], u, v])
}
}
这给了我:
[100000, "0", "0"]
[294, "0", "1"]
[366, "0", "2"]
[294, "1", "0"]
[100000, "1", "1"]
[140, "1", "2"]
[366, "2", "0"]
[140, "2", "1"]
[100000, "2", "2"]
但现在我正试图让这些数组按我相信的索引 0 和 1 排序。
也许这会真正做到:
const original = {
0: {0: 100000, 1: 294, 2: 366},
1: {0: 294, 1: 100000, 2: 140},
2: {0: 366, 1: 140, 2: 100000}
}
const result = []
for (u in original) {
for (v in original[u]) {
result.push([original[u][v], u, v])
}
}
result.sort((a1, a2) => a1[1] - a2[1]).sort((a1, a2) => a1[0] - a2[0])
console.log(result)
您可以通过获取键和索引来获取对象并构建数组,然后获取值和索引。然后按第一个第二个和第三个索引处的值对数组进行排序。
var g = { 0: { 0: 100000, 1: 294, 2: 366 }, 1: { 0: 294, 1: 100000, 2: 140 }, 2: { 0: 366, 1: 140, 2: 100000 } },
result = Object
.assign([], g)
.reduce((r, o, i) => {
Object.assign([], o).forEach((v, j) => r.push([v, i, j]));
return r;
}, [])
.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我正在尝试将排序和循环从 python 转换为 javascript,但无法弄清楚:(
G = {
0: {0: 100000, 1: 294, 2: 366},
1: {0: 294, 1: 100000, 2: 140},
2: {0: 366, 1: 140, 2: 100000}
}
我在 python 中有这个 for 循环:
for W,u,v in sorted((G[u][v], u, v) for u in G for v in G[u]):
并想转换成javascript(必要时使用下划线),并添加魔法使其变成这样:
var y = [
[140, 1, 2),
[140, 2, 1],
[294, 0, 1],
[294, 1, 0],
[366, 0, 2],
[366, 2, 0],
[100000, 0, 0],
[100000, 1, 1],
[100000, 2, 2]]
我已经能够使用两个 for in
循环获得数组中的结果列表:
for (u in x) {
for (v in x[u]) {
console.log([x[u][v], u, v])
}
}
这给了我:
[100000, "0", "0"]
[294, "0", "1"]
[366, "0", "2"]
[294, "1", "0"]
[100000, "1", "1"]
[140, "1", "2"]
[366, "2", "0"]
[140, "2", "1"]
[100000, "2", "2"]
但现在我正试图让这些数组按我相信的索引 0 和 1 排序。
也许这会真正做到:
const original = {
0: {0: 100000, 1: 294, 2: 366},
1: {0: 294, 1: 100000, 2: 140},
2: {0: 366, 1: 140, 2: 100000}
}
const result = []
for (u in original) {
for (v in original[u]) {
result.push([original[u][v], u, v])
}
}
result.sort((a1, a2) => a1[1] - a2[1]).sort((a1, a2) => a1[0] - a2[0])
console.log(result)
您可以通过获取键和索引来获取对象并构建数组,然后获取值和索引。然后按第一个第二个和第三个索引处的值对数组进行排序。
var g = { 0: { 0: 100000, 1: 294, 2: 366 }, 1: { 0: 294, 1: 100000, 2: 140 }, 2: { 0: 366, 1: 140, 2: 100000 } },
result = Object
.assign([], g)
.reduce((r, o, i) => {
Object.assign([], o).forEach((v, j) => r.push([v, i, j]));
return r;
}, [])
.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }