2017-02-28 82 views
-2
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编写代码呢?

+1

这很不清楚。为什么'-31'和'-21'不包含在你的输出中,当它们在输入中出现多次? –

+0

这是一个安排问题,所以理想情况下31和-31是相同的并且是重复的,所以我只需要输出11,21,31,41,尽管它们显示为否定。 –

+0

那么应该在你的问题。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask) - 您需要准确解释您尝试解决什么问题,您如何解决问题以及出现什么问题你有你的尝试解决方案。 –

回答

1

试试这个:

unique_list = [] 
for i in list: 
    for k in i: 
     if not abs(k) in unique_list: 
      unique_list.append(abs(k)) 
+0

对不起。我错过了正面/负面的 –

+0

你可以不要downvote –

0

我能够在这里得到的输出是下面的代码:

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'并运行 如果您觉得它有帮助,请投票回答。