将工资从一维数组转换为二维数组

时间:2019-09-19 05:09:20

标签: python arrays

我创建了一个工资系统,该系统计算工资总额,税额和净额。我可以从工作人员列表中创建一个一维数组。输出每个工作人员的税金和净额。 我想回答的主要问题(第1个)是

  1. 主要我想将1维数组转换为2维数组。 (我不想使用NUMPY)

如果可以的话,请您解释以下两点

  1. 当我将多个小时连接在一起时,使用逗号时会出现错误,但是使用+时却不会,为什么?

  2. 我该如何编码输出,以便以货币等形式显示值,例如$

这是我的输出atm

output -: ['Kyle', '3,025.00', '605.00', '2,420.00', 'John', '3,025.00', '605.00', '2,420.00', 'Peter', '3,025.00', '605.00', '2,420.00', 'Harry', '3,025.00', '605.00', '2,420.00']

谢谢

ive试图使用嵌套的for循环来转换1darray,但是我不是高级编码员,因此无法实现

#wages system to work out gross tax and net
#me
# #16/9/2019

#this is 20 percent of the gross for tax purposes
taxrate=0.2
#blank list

mylistwage=[]

def calc(i):
#input how much paid per hour joined with first iteration of for loop ie 
#persons name
    pph = float(input("how much does "+ i+ " get paid an hour"))
#as above but different question
    hw = float(input("how many hours did "+ i+" work this week"))
#calculates the weekly gross wage
    gross=hw*pph
#calculates tax needed to be paid
    tax=gross*taxrate
#calculates how much your take home pay is
    net=gross-tax
#adds the gross tax and wage to a one dimentionsal list
    mylistwage.append(i)
#formats appended list so that it is to 2 decimal float appends 3 values
    mylistwage.append(format(gross,",.2f"))
    mylistwage.append(format(tax,",.2f"))
    mylistwage.append(format(net,",.2f"))



mylist=["Kyle","John","Peter","Harry"]
for i in (mylist):
    calc(i)
print(mylistwage)

1 个答案:

答案 0 :(得分:0)

一种方法可以是在calc方法中创建本地列表,然后添加必须在该列表中添加的所有信息,然后返回该列表。

现在在迭代mylist的for循环中,每次仅将其追加到mainlistwage变量中。 我建议对代码进行以下更改: 添加了CAP注释作为解释。

#wages system to work out gross tax and net
#me
# #16/9/2019

#this is 20 percent of the gross for tax purposes
taxrate=0.2
#blank list

mainlistwage=[] #CHANGING VARIABLE NAME HERE

def calc(i):
    mylistwage = []  #CREATING LOCAL VARIABLE
#input how much paid per hour joined with first iteration of for loop ie 
#persons name
    pph = float(input("how much does "+ i+ " get paid an hour"))
#as above but different question
    hw = float(input("how many hours did "+ i+" work this week"))
#calculates the weekly gross wage
    gross=hw*pph
#calculates tax needed to be paid
    tax=gross*taxrate
#calculates how much your take home pay is
    net=gross-tax
#adds the gross tax and wage to a one dimentionsal list
    mylistwage.append(i)
#formats appended list so that it is to 2 decimal float appends 3 values
    mylistwage.append(format(gross,",.2f"))
    mylistwage.append(format(tax,",.2f"))
    mylistwage.append(format(net,",.2f"))
    return mylistwage    # ADDING RETURN STATEMENT


mylist=["Kyle","John","Peter","Harry"]
for i in (mylist):
    mainlistwage.append(calc(i))  # APPENDING EACH RETURN LIST TO MAINLIST VARIABLE
print(mainlistwage)