list = [[11], [21], [31], [41], [-11, 21], [11, -21],
[-21, 31], [21, -31], [-31, 41], [31, -41]]
輸出應該包含查找重複Python中列表的列表
list_unique = [11,21,31,41]
如何在Python編寫代碼呢?
list = [[11], [21], [31], [41], [-11, 21], [11, -21],
[-21, 31], [21, -31], [-31, 41], [31, -41]]
輸出應該包含查找重複Python中列表的列表
list_unique = [11,21,31,41]
如何在Python編寫代碼呢?
試試這個:
unique_list = []
for i in list:
for k in i:
if not abs(k) in unique_list:
unique_list.append(abs(k))
對不起。我錯過了正面/負面的 –
你可以不要downvote –
我能夠在這裏得到的輸出是下面的代碼:
input_list = [[11], [21], [31], [41], [-11, 21], [11, -21], [-21, 31], [21, -31], [-31, 41], [31, -41]]
def get_explode_list(input_list):
explode_list = []
for inner_list in input_list:
for item in inner_list:
explode_list.append(item)
return explode_list
explode_list = get_explode_list(input_list)
dictionary = {}
for x in explode_list:
value = dictionary.get(x,0)
value = value+1
dictionary[x] = value
def get_final_list(dictionary):
final_list = []
for key,value in dictionary.items():
if(value > 1 and key >= 0):
final_list.append(key)
return final_list
final_list = get_final_list(dictionary)
final_list = [31,41,11,21] 您可以使用此如果輸入列表不是int,只需在get_final_list方法中取出'和key> = 0'並運行 如果您覺得它有幫助,請投票回答。
這很不清楚。爲什麼'-31'和'-21'不包含在你的輸出中,當它們在輸入中出現多次? –
這是一個安排問題,所以理想情況下31和-31是相同的並且是重複的,所以我只需要輸出11,21,31,41,儘管它們顯示爲否定。 –
那麼應該在你的問題。請參閱[如何提問](http://stackoverflow.com/help/how-to-ask) - 您需要準確解釋您嘗試解決什麼問題,您如何解決問題以及出現什麼問題你有你的嘗試解決方案。 –