python - 将工资从一维数组转换为二维数组
问题描述
我创建了一个工资系统,可以计算工资总额、税收和净额。我可以让它从工作人员列表中创建一个一维数组。为每个工作人员输出总税和净额。我想回答的主要问题(第 1 号)是。
- 主要我想将一维数组转换为二维数组。(我不想使用 NUMPY)
如果可能的话,你能解释一下下面的两点吗
当我连接几个小时时,我工作了多少小时,当我使用逗号时出现错误,但当我使用 + 时却没有,这是为什么
我如何对输出进行编码,以便它向我显示货币的价值,例如 $
这是我的输出 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']
谢谢
我尝试使用嵌套的 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)
解决方案
一种方法可以是在 calc 方法中创建一个本地列表,然后在该列表中添加必须添加的任何信息并返回该列表。
现在在 for 循环中迭代 mylist 时只需将每次附加到 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)
推荐阅读
- gradle - 使用插件“java-gradle-plugin”时找不到插件错误的实现类
- macos - 列选择模式触发 Intellij 上的谷歌搜索
- dialogflow-es - 自己的 Google 助手:Dialogflow 模拟到 action sdks action.json
- ios - 导航回视图时更新 SwiftUI 选择器最小值和最大值
- javascript - D3 折线图 - 当 sidenav 打开或关闭时重新渲染轴(x 和 y)
- javascript - 按字符串拆分仅适用于第一个子字符串
- php - 循环返回一些奇怪的点
- kubernetes - GKE 在 2 个后端应用程序之间的集群负载平衡
- ios - 如何将百分比值精确四舍五入到小数点后两位?
- javascript - 纯 JavaScript async/await 在 while 循环中等待每个 XMLHttpRequest 完成,然后再发送下一个 XMLHttpRequest