我们说我有以下列表,其中包含一组对象:
people = [("John","Smith"), ("Jane","Doe"), ("Jane","Smith")]
对象是通过一个单独的类创建的,该类具有person.getFirstName
和person.getLastName
defs。
如何解析列表,以便最终得到一个由
组成的单独值列表 uniqueNames = ["Smith","Doe"]
答案 0 :(得分:3)
使用列表理解可能会满足您的需求:
>>> people = [("John","Smith"), ("Jane","Doe"), ("Jane","Smith")]
>>> uniqueNames = list(set(i[1] for i in people))
>>> uniqueNames
['Smith', 'Doe']