Python: 如何组合二维数组中的元素?
Python: how to combine elements in a 2d array?
我有这个列表
[[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571], ...]
我希望它是这样的:
[[0, 374,676,844], [1, 186, 440],[2, 202], [3, 198, 571], ...]
我试过这段代码:
for i in range(len(index)-1):
if (index[i][0]==index[i+1][0]):
index[i].append(index[i+1][1])
del index[i+1]
i=i-1
但是没用
一般来说,不建议从您迭代的列表中删除项目。在 Python 中,更常见(也更容易)生成具有理解力的新列表。
您可以使用 itertools.groupby 进行分组,并按列表中的第一项进行分组。然后从这些组中,您可以通过获取密钥加上所有其他第二个元素来创建所需的 sub-groups。类似于:
from itertools import groupby
l = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
[[k] + [i[1] for i in g] for k, g in groupby(l, key=lambda x: x[0])]
# [[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]
这是一个更简单的解决方案:
lst = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
newlst = []
curr_lst = []
max_index = 3
for x in range(max_index+1):
curr_lst = [x]
for y in lst:
if y[0] == x:
curr_lst.append(y[1])
newlst.append(curr_lst)
print(newlst)
输出:
[[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]
最简单的实现方式。在使用该函数之前,请确保您已按子列表的第一个索引对列表进行排序。
a = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
n = len(a)
x = 0
res = []
r = [0]
for i in a:
if i[0] == x:
r.append(i[1])
else:
x += 1
res.append(r)
r = [x, i[1]]
res.append(r)
print(res)
使用集合 OrderedDict
from collections import OrderedDict
l = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
d = OrderedDict()
for inner in l:
if inner[0] in d:
d[inner[0]].append(inner[1])
else:
d[inner[0]] = [inner[1]]
print(d.values())
您可以使用字典从输入列表中收集索引 1 的数字。
a=[[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
b={}
[b[i[0]].append(i[1]) if i[0] in b else b.update({i[0]:[i[0],i[1]]}) for i in a]
b=list(b.values())
print(b)
输出
[[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]
无需导入,您可以按以下方式进行 -
new_dict = {}
result = []
for item in nest:
if item[0] in new_dict.keys():
new_dict[item[0]].append(item[1])
else:
new_dict[item[0]] = [item[1]]
for key, value in new_dict.items():
result.append([key] + value)
我有这个列表
[[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571], ...]
我希望它是这样的:
[[0, 374,676,844], [1, 186, 440],[2, 202], [3, 198, 571], ...]
我试过这段代码:
for i in range(len(index)-1):
if (index[i][0]==index[i+1][0]):
index[i].append(index[i+1][1])
del index[i+1]
i=i-1
但是没用
一般来说,不建议从您迭代的列表中删除项目。在 Python 中,更常见(也更容易)生成具有理解力的新列表。
您可以使用 itertools.groupby 进行分组,并按列表中的第一项进行分组。然后从这些组中,您可以通过获取密钥加上所有其他第二个元素来创建所需的 sub-groups。类似于:
from itertools import groupby
l = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
[[k] + [i[1] for i in g] for k, g in groupby(l, key=lambda x: x[0])]
# [[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]
这是一个更简单的解决方案:
lst = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
newlst = []
curr_lst = []
max_index = 3
for x in range(max_index+1):
curr_lst = [x]
for y in lst:
if y[0] == x:
curr_lst.append(y[1])
newlst.append(curr_lst)
print(newlst)
输出:
[[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]
最简单的实现方式。在使用该函数之前,请确保您已按子列表的第一个索引对列表进行排序。
a = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
n = len(a)
x = 0
res = []
r = [0]
for i in a:
if i[0] == x:
r.append(i[1])
else:
x += 1
res.append(r)
r = [x, i[1]]
res.append(r)
print(res)
使用集合 OrderedDict
from collections import OrderedDict
l = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
d = OrderedDict()
for inner in l:
if inner[0] in d:
d[inner[0]].append(inner[1])
else:
d[inner[0]] = [inner[1]]
print(d.values())
您可以使用字典从输入列表中收集索引 1 的数字。
a=[[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
b={}
[b[i[0]].append(i[1]) if i[0] in b else b.update({i[0]:[i[0],i[1]]}) for i in a]
b=list(b.values())
print(b)
输出
[[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]
无需导入,您可以按以下方式进行 -
new_dict = {}
result = []
for item in nest:
if item[0] in new_dict.keys():
new_dict[item[0]].append(item[1])
else:
new_dict[item[0]] = [item[1]]
for key, value in new_dict.items():
result.append([key] + value)