Python – 分解值的百分比并向列表添加新行

huangapple go评论90阅读模式
英文:

Python - breaking down the percentage of values and adding new rows to the list

问题

I'm a beginner in python and can't figure out the percentage breakdown of the amount. I would like the rows to contain lines with the calculated amounts.

DATA FROM TXT:

  1. ['Date', 'Period', 'NumberBill', 'Amount', 'KEY', 'WHAT\n']
  2. ['31.01.2022', '2', 'BILL3', '-1800.00', 'TEAM', 'BILL\n']
  3. ['03.02.2022', '2', 'BILL2', '644.00', 'PERSON','BILL\n']
  4. ['03.02.2022', '2', 'BILL1', '150.00', 'TEAM', 'BILL']

OUT_my:

  1. ['31.01.2022', '2', 'BILL3', '-1800.00', 'TEAM', 'BILL\n', -540.0, -360.0, -900.0]
  2. ['03.02.2022', '2', 'BILL1', '150.00','TEAM', 'BILL', 45.0, 30.0, 75.0]

I wrote:

  1. filepath = "test.txt"
  2. file = open(filepath, "r")
  3. for i in file:
  4. iSplit = i.split(";")
  5. percentage1 = 0.30
  6. percentage2 = 0.20
  7. percentage3 = 0.50
  8. result1 = 0
  9. result2 = 0
  10. result3 = 0
  11. if iSplit[4] == 'TEAM':
  12. result1 = float(iSplit[3]) * percentage1
  13. result2 = float(iSplit[3]) * percentage2
  14. result3 = float(iSplit[3]) * percentage3
  15. iSplit.append(round(result1, 2))
  16. iSplit.append(round(result2, 2))
  17. iSplit.append(round(result3, 2))
  18. print(iSplit)

What I want to get:

  1. ['03.02.2022', '2', 'BILL2', '644.00', 'PERSON','BILL']
  2. ['31.01.2022', '2', 'BILL3', '-540.00', 'TEAM', 'BILL']
  3. ['31.01.2022', '2', 'BILL3', '-360.00', 'TEAM', 'BILL']
  4. ['31.01.2022', '2', 'BILL3', '-900.00', 'TEAM', 'BILL']
  5. ['03.02.2022', '2', 'BILL1', '45.00', 'TEAM', 'BILL']
  6. ['03.02.2022', '2', 'BILL1', '30.00', 'TEAM', 'BILL']
  7. ['03.02.2022', '2', 'BILL1', '75.00', 'TEAM', 'BILL']
英文:

I'm a beginner in python and can't figure out the percentage breakdown of the amount. I would like the rows to contain lines with the calculated amounts.

DATA FROM TXT:

  1. ['Date', 'Period', 'NumberBill', 'Amount', 'KEY', 'WHAT\n']
  2. ['31.01.2022', '2', 'BILL3', '-1800.00', 'TEAM', 'BILL\n']
  3. ['03.02.2022', '2', 'BILL2', '644.00', 'PERSON','BILL\n']
  4. ['03.02.2022', '2', 'BILL1', '150.00', 'TEAM', 'BILL']

OUT_my:

  1. ['31.01.2022', '2', 'BILL3', '-1800.00', 'TEAM', 'BILL\n', -540.0, -360.0, -900.0]
  2. ['03.02.2022', '2', 'BILL1', '150.00','TEAM', 'BILL', 45.0, 30.0, 75.0]

I wrote:

  1. filepath = "test.txt"
  2. file = open(filepath, "r")
  3. for i in file:
  4. iSplit = i.split(";")
  5. percentage1 = 0.30
  6. percentage2 = 0.20
  7. percentage3 = 0.50
  8. result1 = 0
  9. result2 = 0
  10. result3 = 0
  11. if iSplit[4] == 'TEAM':
  12. result1 = float(iSplit[3]) * percentage1
  13. result2 = float(iSplit[3]) * percentage2
  14. result3 = float(iSplit[3]) * percentage3
  15. iSplit.append(round(result1, 2))
  16. iSplit.append(round(result2, 2))
  17. iSplit.append(round(result3, 2))
  18. print(iSplit)`

What I want to get:

  1. ['03.02.2022', '2', 'BILL2', '644.00', 'PERSON','BILL]
  2. ['31.01.2022', '2', 'BILL3', '-540.00', 'TEAM', 'BILL]
  3. ['31.01.2022', '2', 'BILL3', '-360.00', 'TEAM', 'BILL]
  4. ['31.01.2022', '2', 'BILL3', '-900.00', 'TEAM', 'BILL]
  5. ['03.02.2022', '2', 'BILL1', '45.00', 'TEAM', 'BILL']
  6. ['03.02.2022', '2', 'BILL1', '30.00', 'TEAM', 'BILL']
  7. ['03.02.2022', '2', 'BILL1', '75.00', 'TEAM', 'BILL']

答案1

得分: 0

要获得您想要的结果,您应该为每个百分比打印一个新行,将金额替换为金额乘以百分比,而不是将它们全部附加到原始行。

要获取非团队行,请使用一个 else: 块来打印原始行。

  1. filepath = "test.txt"
  2. team_percentages = [0.30, 0.20, 0.50]
  3. with open(filepath, "r") as file:
  4. for i in file:
  5. iSplit = i.split(";")
  6. if iSplit[4] == 'TEAM':
  7. for percent in team_percentages:
  8. new_row = iSplit.copy()
  9. new_row[3] = str(float(iSplit[3]) * percent)
  10. print(new_row)
  11. else:
  12. print(iSplit)
英文:

To get the result you want, you should print a new row for each percentage, with the amount replaced by the amount multiplied by the percentage, not append them all to the original row.

And to get the non-team rows, use an else: block to print the original row.

  1. filepath = "test.txt"
  2. team_percentages = [0.30, 0.20, 0.50]
  3. with open(filepath, "r") as file:
  4. for i in file:
  5. iSplit = i.split(";")
  6. if iSplit[4] == 'TEAM':
  7. for percent in team_percentages:
  8. new_row = isplit.copy()
  9. new_row[3] = str(float(iSplit[3]) * percent)
  10. print(new_row)
  11. else:
  12. print(iSplit)

huangapple
  • 本文由 发表于 2023年3月7日 06:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定