是否可以简化这一点:
from tkinter import *
from random import randint
class BankAccount(object):
def __init__(self, initial_balance=0):
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def get_balance(self, initial_balance, rate):
return self.get_balance() * self._rate
class BankAccountWithInterest(BankAccount):
def __init__(self, initial_balance=0, rate=0.1):
BankAccount.__init__(self, initial_balance)
self._rate = rate
def interest(self):
return self.balance * self._rate
balance = (randint(100, 500))
my_account = BankAccount(balance)
my_interest = BankAccountWithInterest(balance)
interest = my_interest.balance + my_interest.interest()
typeOfAccount = "1"
class GUI:
def __init__(self, master):
frame = Frame(master)
frame.pack()
#Toolbar#
toolbar = Frame(root)
toolbar.pack(side=TOP, fill=X)
#Button#
button1 = Button(toolbar, text="Deposit", width = 13, command=self.depositBalance)
button2 = Button(toolbar, text="Withdraw",width = 13, command=self.depositWithdraw)
button1.pack(side=LEFT)
button2.pack(side=RIGHT)
#Menu#
subMenu = Menu(menu)
menu.add_cascade(label="Type of Account", menu=subMenu)
subMenu.add_command(label="Standard", command= self.standard)
subMenu.add_command(label="Interest", command= self.interest)
#Textbox#
self.text = Entry(root)
self.text.pack()
#Labels#
w = Label(root, text="Current Balance:")
w.pack()
w1 = tkinter.StringVar()
if typeOfAccount == "1":
w1 = Label(root, text=my_account.balance)
w1.pack()
elif typeOfAccount == "2":
w1.set(text=interest)
w1.pack()
def depositBalance(self):
a = int(self.text.get())
my_account.balance = a + my_account.balance
print(my_account.balance)
def depositWithdraw(self):
a = int(self.text.get())
my_account.balance = my_account.balance - a
print(my_account.balance)
def standard(self):
typeOfAccount = "1"
def interest(self):
typeOfAccount = "2"
root = Tk()
menu = Menu(root)
root.config(menu=menu)
root.title("Bank Account")
root.minsize(width=250, height=100)
root.maxsize(width=300, height=150)
GUI(root)
root.mainloop()
import numpy as np
a = np.random.random_sample((40, 3))
data_base = np.random.random_sample((20, 3))
mean = np.random.random_sample((40,))
data = []
for s in data_base:
data.append(mean + np.dot(a, s))
应该是大小(20,40)。我想知道我是否可以做一些广播而不是循环。我无法使用data
和一些np.add
执行此操作。我当然不会正确使用它。
答案 0 :(得分:3)
您的data
创建了一个(20,40)数组:
In [385]: len(data)
Out[385]: 20
In [386]: data = np.array(data)
In [387]: data.shape
Out[387]: (20, 40)
dot
的直接应用产生了同样的结果:
In [388]: M2=mean+np.dot(data_base, a.T)
In [389]: np.allclose(M2,data)
Out[389]: True
matmul
运算符也适用于这些数组(无需展开和挤压):
M3 = data_base@a.T + mean