list = [[11], [21], [31], [41], [-11, 21], [11, -21],
[-21, 31], [21, -31], [-31, 41], [31, -41]]
输出应包含
list_unique = [11,21,31,41]
如何在python中为此编写代码?
答案 0 :(得分:1)
试试这个:
unique_list = []
for i in list:
for k in i:
if not abs(k) in unique_list:
unique_list.append(abs(k))
答案 1 :(得分: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'并运行 如果您觉得有帮助,请投票给答案。