PYTHON去除重複元素2

繼續上篇,看到有個博主有個去除array的重複元素的,有了本次想法,往下看:

import numpy as np
import pandas as pd

#輸入:兩個列表;
#輸出:去除重複元素的列表
#方法:將list轉換爲array,處理以後,轉換回去!挺麻煩啊!

#方式1(瞎折騰)
list1 = [1,3,5,1,7,3,1,1]
list2 = [2,4,6,2,8,4,2,2]

list3 = list(zip(list1,list2))#先將列表壓縮成壓縮對象,再轉換爲list
list3_array = np.array(list3)#這個能夠將list轉換爲array(數組)
print(list3_array)

a = np.array(list(set([tuple(t) for t in list3_array]))) #這個是轉換爲矩陣
b = a.tolist()  #array轉換爲list
print(b)

#方式2:去掉不就行了
c = list(set([tuple(t) for t in list3_array])) #這個返回列表,像是list(zip())以後的
print(c)
#結果:
[[1 2]
 [3 4]
 [5 6]
 [1 2]
 [7 8]
 [3 4]
 [1 2]
 [1 2]]
[[1, 2], [3, 4], [5, 6], [7, 8]]
[(1, 2), (3, 4), (5, 6), (7, 8)]

在 http://www.javashuo.com/article/p-ebhryssz-nk.html 文章中,使用 array = np.asarray(list) 的方法將list轉換爲array,那麼與 np.array(list) 有什麼區別呢?python

推薦你們去看這裏:https://www.jianshu.com/p/a050fecd5a29

數組

從這個博客獲得的靈感(python-去除二維數組/二維列表中的重複行):https://blog.csdn.net/u012991043/article/details/81067207spa

個人這個list轉array的方案來自於(Python中list轉換array的一個問題):https://blog.csdn.net/dta0502/article/details/90215049.net

相關文章
相關標籤/搜索