我需要从对象列表中创建非重复整数元素列表。
例如:有一个对象有两个属性:'id'和'other_id':
first = [elem.id for elem in objects_list]
second = [elem.other_id for elem in objects_list]
print first
[0,1,2,3,4,5]
print second
[4,5,6,7,9]
现在我可以创建两个列表,其中包含来自所有对象的这两个属性,如下所示:
first = [elem.id for elem in objects_list]
first.extend(elem.other_id for elem in objects_list if elem.other_id not in first)
print first
[0,1,2,3,4,5,6,7,9]
有没有办法以更短的方式做到这一点?