小编use*_*014的帖子

setitem和getitem - python

我创建了一个创建矢量的python程序.现在,我想设置使用功能的项目__setitem____getitem__.因此,例如,if vector = Vec()vector[3] = 26将更改空向量[0, 0, 0, 26].我需要覆盖__getitem__并且__setitem__我已经列出了下面的代码,但是我遇到了get和set函数的麻烦.有什么建议?

class Vec:
    def __init__(self, length = 0):
        self.vector = [0]*length

    def __str__(self):
        return '[{}]'.format(', '.join(str(i) for i in self.vector))
        #This formats the output according to the homework.
        #Adds '[' and ']' and commas between each 0

    def __len__(self):
        return len(self.vector)

    def extend(self, newLen):
        self.vector.append([0]*newLen)
        return (', '.join(str(j) for j in self.vector))

    def __setitem__(self, key, item):
        self.vector[key] = item …
Run Code Online (Sandbox Code Playgroud)

python class

19
推荐指数
3
解决办法
6万
查看次数

重载加法,减法和乘法运算符

你如何重载加法,减法和乘法运算符,以便我们可以添加,减去和相乘两个不同或相同大小的向量?例如,如果向量是不同的大小,我们必须能够根据最小的向量大小来加,减或乘两个向量?

我已经创建了一个允许你修改不同向量的函数,但是现在我正在努力使运算符超载,并且不知道从哪里开始.我将粘贴下面的代码.有任何想法吗?

def __add__(self, y):
    self.vector = []
    for j in range(len(self.vector)):
        self.vector.append(self.vector[j] + y.self.vector[j])
    return Vec[self.vector]
Run Code Online (Sandbox Code Playgroud)

python class vector operators

16
推荐指数
3
解决办法
4万
查看次数

所得税计算python

如何制作70000及以上范围的for循环?我正在为所得税进行循环,当收入超过70000时,税率为30%.我会做点什么for income in range(income-70000)吗?

好吧,起初我开发了一个没有使用循环的代码,它运行得很好,但后来我被告知我需要在我的代码中加入一个循环.这就是我所拥有的,但对我来说使用for循环是没有意义的.有人能帮我吗?

def tax(income):

for income in range(10001):
    tax = 0
    for income in range(10002,30001):
        tax = income*(0.1) + tax
        for income in range(30002,70001):
            tax = income*(0.2) + tax
            for income in range(70002,100000):
                tax = income*(0.3) + tax
print (tax)
Run Code Online (Sandbox Code Playgroud)

好的,所以我现在尝试使用while循环,但它没有返回值.告诉我你的想法.我需要根据收入计算所得税.先10000美元没有税.接下来的20000有10%.接下来40000有20%.超过70000是30%.

def taxes(income):

income >= 0
while True:
    if income < 10000:
        tax = 0
    elif income > 10000 and income <= 30000:
        tax = (income-10000)*(0.1)
    elif income > 30000 and income <= 70000:
        tax …
Run Code Online (Sandbox Code Playgroud)

python iteration for-loop bisection

3
推荐指数
1
解决办法
1万
查看次数

Python - 读取文件并确定最低分和人

有一个文件包含相同顺序的以下信息:LastName FirstName IDnumber score1 score2 finalScore.我的目标是确定整个文件的最低finalScore以及获得该分数的学生的ID号.这就是我所拥有的,但我不确定如何将IDnumber与分数相关联.

scores = open('text.txt', 'w')
scores.write('Johnson Jeff 1213 91 92 94\n')
scores.write('Johnson Alan 5553 81 82 84\n')
scores.write('Johnson Bart 8973 91 82 98\n')
scores.close()
grades = open('text.txt','r')
listy = []
for i in grades:
    a = i.split()
    for j in a[3:]:
        listy.append(j)
print(min(listy))
Run Code Online (Sandbox Code Playgroud)

python file-io split for-loop list

1
推荐指数
1
解决办法
210
查看次数

标签 统计

python ×4

class ×2

for-loop ×2

bisection ×1

file-io ×1

iteration ×1

list ×1

operators ×1

split ×1

vector ×1