gpt4 book ai didi

python - 如何删除矩阵中的重复值?

转载 作者:行者123 更新时间:2023-12-01 11:11:05 25 4
gpt4 key购买 nike

如何删除矩阵中的重复值?

mat = [[5,6,8] , [8,5,3] , [9,10,3]]
res= []
for i in mat:
if i not in res:
res.append(i)
print(res)
fit= set(res)
print(fit)

最佳答案

mat = [[5,6,8] , [8,5,3] , [9,10,3]]
res=[]
for i in mat:
res+=i
duplicated= set([i for i in res if res.count(i)>1])
new=[]
for i in mat:
new_in=[]
for j,val in enumerate(i):
if val not in duplicated:
new_in.append(val)
new.append(new_in)
print(new)

如果你期望你的输出像 [[6], [], [9, 10]]

关于python - 如何删除矩阵中的重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60389204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com