我如何访问实例变量项以从每个类中打印出唯一的项列表?

乔伊·莱索斯(Joey Lesus)
class Trip(object):
    'a class that abstracts a trip to a destination'

    def __init__(self, destination='Nowhere', items=[] ):
        'Initialize the Trip class'
        self.destination = destination
        self.items = items

class DayTrip(Trip):
    'a class that abstracts a trip to a destination on a specific day'

    def __init__(self, destination='Nowhere', items=[] , day='1/1/2019' ):
        'Initialize the Trip class'
        self.destination = destination
        self.items = items
        self.day = day

class ExtendedTrip(Trip):
    'a class that abstracts a trip to a destination on between two dates'

    def __init__(self, destination='Nowhere', items=[] , day='1/1/2019' , 
endDay = '12/31/2019' ):
        'Initialize the ExtendedTrip class'
        self.destination = destination
        self.items = items
        self.day = day
        self.endDay = endDay

def luggage(lst):

我希望行李带走每个班级的所有物品,并打印一张列出所有这些独特物品的清单。

这些是一些示例输入:

trip1 = Trip('Basement', ['coat','pants','teddy bear'])

trip2 = DayTrip('Springfield',['floss', 'coat','belt'], '2/1/2018')

trip3 = ExtendedTrip('Mars',['shampoo', 'coat','belt'], '4/1/2058')

trip4 = ExtendedTrip()

每次旅行都会附加到一个列表中,然后它将获取一组打印此内容的项目列表:

{'pants', 'belt', 'toothbrush', 'floss', 'shampoo', 'teddy bear', 'coat'}
特里·简·里迪

请尝试以下操作(未经测试)。

def luggage(lst):
    items = set()
    for trip in lst:
        for item in trip.items:
            items.add(item)
    return items

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何唯一地打印出字典中列表的元素

如何打印列表中字典项的每个值

将类实例附加到另一个类的列表变量时如何消除重复项?

Python 初学者:我想知道为什么我的附加列表中的最后一项没有打印出来

集列表中的唯一项

在列表中查找唯一项

如何从类中获取变量的每个实例的列表?

使用每个条目实例变量删除列表中重复项的最快方法

如何让一个类从位于另一个类中的对象数组列表中打印出字符串变量?

Django在多个manytomany实例中创建查询集或唯一项目列表

Python:通过唯一索引引用列表中的每个重复项

Ruby:我应该如何访问类中的实例变量?

Excel 2010:如何计算一列中每个重复项的每个实例?

如何保留字符串中的重复项[]从列表中排除单词并打印出来

如何从列表中创建列表,每个列表都像原始列表,但缺少一项

在许多列表中删除列表的唯一项

如何在Pandas中为每个唯一行值删除重复项?

我如何在 python 3 中打印出数字变量?

显示列表中的唯一第一项

SASS中多个列表中的唯一项

如何在C#中从唯一项列表中顺序执行多个Regex替换

如何从 Google Apps 脚本中的列表中获取唯一项目?

如何在mysql中显示每个唯一项目的信息,如何组合每个项目的信息

如何获得列表中每个项目的第一项和最后一项

如何使用“zip()”循环遍历列表并为元组列表中的每个元组添加一项?

如何为每个类实例设置唯一约束?

如何处理MySql中某一类别下的唯一项,其中不同类别下的项可能不唯一?

对于文件中每个可能的两个唯一单词对,打印出该对出现的次数

从列表中删除唯一值,仅保留重复项