第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 __add__ 方法相加

使用 __add__ 方法相加

Helenr 2023-12-29 15:06:51
我目前正在研究一個(gè)創(chuàng)建球并為它們提供位置的課程。我想為類創(chuàng)建一個(gè)add方法,該方法獲取兩個(gè)球的位置并將它們加在一起。我正在使用的代碼是:class Ball:    def __init__ (self, x, y):        self.position = [x, y]    def __add__ (self, other):        return self.position + other.position    ball1 = Ball(3, 4)print (ball1.position)ball2 = Ball(5, 8)print (ball2.position)ball3 = Ball(4, 4)print (ball3.position)ball4 = ball1 + ball3print (ball4)代碼現(xiàn)在的工作方式并不符合預(yù)期。我希望 ball1 + ball3 位置相加,但我得到的打印結(jié)果如下:[3, 4][5, 8][4, 4][3, 4, 4, 4]我們將 ball1 和 ball3 的 x 和 y 值并排放置,而不是相加。
查看完整描述

4 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

當(dāng)您將兩個(gè)列表添加在一起時(shí),它只是簡(jiǎn)單地附加。您的添加需要如下所示:

def __add__ (self, other):
    return [self.position[0]+other.position[0], self.position[1]+other.position[1]]


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

使用數(shù)組時(shí),“+”運(yùn)算符連接兩個(gè)操作數(shù)中的元素并返回一個(gè)新數(shù)組。所以plus并沒有按照你想象的那樣做。

您必須開發(fā)另一個(gè)函數(shù)來對(duì)數(shù)組的每個(gè)成員求和并返回這個(gè)新數(shù)組


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是因?yàn)槟?2 個(gè) python 列表之間使用求和運(yùn)算符,這將連接兩個(gè)列表。如果你想按元素求和,你必須這樣做:


return [self.x+other.x, self.y+other.y]

我還建議您查看numpy庫,它將提供您想要的數(shù)學(xué)運(yùn)算符。在這種情況下,您的類可以重寫為:


import numpy as np


class Ball:

    def __init__ (self, x, y):

        self.position = np.array([x, y])

    def __add__ (self, other):

        return self.position + other.position

    

ball1 = Ball(3, 4)

print (ball1.position)

ball2 = Ball(5, 8)

print (ball2.position)

ball3 = Ball(4, 4)

print (ball3.position)

ball4 = ball1 + ball3

print (ball4)

結(jié)果:


>>> ball1 = Ball(3, 4)

>>> print (ball1.position)

[3 4]

>>> ball2 = Ball(5, 8)

>>> print (ball2.position)

[5 8]

>>> ball3 = Ball(4, 4)

>>> print (ball3.position)

[4 4]

>>> ball4 = ball1 + ball3

>>> print (ball4)

[7 8]


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

通過列表理解單獨(dú)添加項(xiàng)目并zip使用:


[b1 + b3 for b1, b3 in zip(ball1.position, ball3.position)]

class Ball:

    def __init__ (self, x, y):

        self.position = [x, y]

    def __add__ (self, other):

        return self.position + other.position


ball1 = Ball(3, 4)

print (ball1.position)

ball2 = Ball(5, 8)

print (ball2.position)

ball3 = Ball(4, 4)

print (ball3.position)

ball4 = [b1 + b3 for b1, b3 in zip(ball1.position, ball3.position)]

print (ball4)


[3, 4]

[5, 8]

[4, 4]

[7, 8]

編輯:您可以進(jìn)一步簡(jiǎn)化列表理解:


ball4 = [sum(b) for b in zip(ball1.position,ball3.position)]


查看完整回答
反對(duì) 回復(fù) 2023-12-29
  • 4 回答
  • 0 關(guān)注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)