从Python的课堂打印,列表理解

发布于 2025-01-26 21:33:29 字数 1127 浏览 3 评论 0原文

为什么我的打印在使用循环时从我的班级工作中函数,但是当我使用列表理解打印时返回内存位置?

stocks= ['STY','STY','PRC','STY','PRC','STY','PRC','STY','PRC','STY']
price=[48,46,35,46,235,46,34,64,26,53]
quantity=[10,24,60,24,54,64,10,10,35,10]
type=['sell','sell','sell','sell','sell','buy','buy','buy','buy','buy']

data=list(zip(stocks,price,quantity,type))
print(list(data))


class Order():
    def __init__(self, stock_name, cost, quantity, trx_type):
        self.stock_name = stock_name
        self.cost = cost
        self.cost = cost
        self.quantity = quantity
        self.trx_type = trx_type
    
    def __str__(self):
        return '{stock}: {trx} {quant} at ${cost}'.format(stock = self.stock_name, trx = self.trx_type.title(), quant = self.quantity, cost = self.cost)

order_list=[]
for order in data:
    stock = Order(stock_name  = order[0], cost = order[1], quantity = order[2], trx_type = order[3])
    order_list.append(stock)
    
print([order for order in order_list])  ## This returns list of memory locations

for order in order_list:  ## this returns print statements from __str)__
    print(order)

Why does my print function from my Class work when I am using a loop, but return memory locations when I print using a list comprehension?

stocks= ['STY','STY','PRC','STY','PRC','STY','PRC','STY','PRC','STY']
price=[48,46,35,46,235,46,34,64,26,53]
quantity=[10,24,60,24,54,64,10,10,35,10]
type=['sell','sell','sell','sell','sell','buy','buy','buy','buy','buy']

data=list(zip(stocks,price,quantity,type))
print(list(data))


class Order():
    def __init__(self, stock_name, cost, quantity, trx_type):
        self.stock_name = stock_name
        self.cost = cost
        self.cost = cost
        self.quantity = quantity
        self.trx_type = trx_type
    
    def __str__(self):
        return '{stock}: {trx} {quant} at ${cost}'.format(stock = self.stock_name, trx = self.trx_type.title(), quant = self.quantity, cost = self.cost)

order_list=[]
for order in data:
    stock = Order(stock_name  = order[0], cost = order[1], quantity = order[2], trx_type = order[3])
    order_list.append(stock)
    
print([order for order in order_list])  ## This returns list of memory locations

for order in order_list:  ## this returns print statements from __str)__
    print(order)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2025-02-02 21:33:29
[order for order in order_list]

在这里,您可以使用列表理解将实例存储在list中。当您使用以下方式打印它们时:

print([order for order in order_list])

它将打印实例的list(不是一个实例或一个实例,而是一个完整的列表)。由于您将实例存储在列表中,因此它将存储其位置。

同样,当您使用循环时:

for order in order_list:
    print(order)

现在,您正在一个一个实例访问单个实例并打印它们,它将使用类的__ str __()函数。

[order for order in order_list]

Here you're storing instances in a list using list comprehension. And when you print them using:

print([order for order in order_list])

it prints the list of instances (not a single instance or instances one by one but a complete list). Since you have stored instances in the list, it will store their location.

Similarly when you use loop:

for order in order_list:
    print(order)

Now you're accessing single instances one by one and printing them and it will use the __str__() function of the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文