繁体   English   中英

如何“重新排列”我的 python 代码以适应某种格式?

[英]How do I 'rearrange' my python code to fit a certain format?

这是我目前的代码。 请注意“percentageOff”和“originalPrices”是包含浮点数/整数的列表。

print("Percent off:", percentageOff[0], '%')
for percentage in percentageOff[1:]:
    print("\t\t\t", percentage, "%")

count = 0
for prices in originalPrices:
    for percentage in percentageOff:
        discount = prices * (percentage/100)
        newPrice = prices - discount
        count += 1
        if(0 < count < 11):
            print("%.2f" % newPrice)
        elif(11 < count < 21):
            print("\t\t\t%.2f" % newPrice)

输出是(与其余代码):

        **Sale Prices**
Normal Price:           $9.95   $14.95  $19.95  $24.95  $29.95  $34.95  $39.95  $44.95  $49.95
______________________________________________________________________________________
Percent off: 5 %
             10 %
             15 %
             20 %
             25 %
             30 %
             35 %
             40 %
             45 %
             50 %
9.45
8.96
8.46
7.96
7.46
6.96
6.47
5.97
5.47
4.97

但我希望输出是

        **Sale Prices**
Normal Price:           $9.95   $14.95  $19.95  $24.95  $29.95  $34.95  $39.95  $44.95  $49.95
______________________________________________________________________________________
Percent off: 5 %        9.45
             10%        8.96
             15%        8.46
             20%        7.96
             25%        7.46
             30%        6.96
             35%        6.47
             40%        5.97
             45%        5.47
             50%        4.97

我该如何解决我的问题?

取决于@danidee

x = [1,2,3,4,5,34]
y = [3,4,5,6,5,4]
print("Percent off:",end="\t")

for i, j in zip(x, y):
    print(i, '\t\t', j,end="\n\t\t")

输出是;

Percent off:    1        3
                2        4
                3        5
                4        6
                5        5
                34       4

不使用外部库的最佳解决方案是在您迭代它们并测试它们时将不同的值放入列表中,然后以并行方式打印它们......因为您正在测试两种不同的条件,并且只有一个条件为真一次。

概念证明

x = [1,2,3,4,5,34]
y = [3,4,5,6,5,4]

for i, j in zip(x, y):
    print(i, '\t\t\t', j)

输出

1            3
2            4
3            5
4            6
5            5
34           4

我建议使用 python 格式化程序非常好! 假设您使用的是 Python 3,代码可以这样重新排列(它的注释很好,所以希望不需要额外的解释):

#!/usr/bin/env python3
percentageOff = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
originalPrices = [9.95, 14.95, 19.95, 24.95, 29.95, 34.95, 39.95, 44.95, 49.95]

# Helper function
def new_price(price, off):
    """Return a new price, calculated as original price minus discount."""
    return price - (price * off / 100)

## Print header
price_header = "".join(["{0}$\t".format(str(p)) for p in originalPrices])
# centre string, given the string length is 78 chars
print("{0:^78}".format("Normal Price")) 
print("Off % | {0}".format(price_header))
print('-' * 78)

## Print rows
for off in percentageOff:
    # padding number to 4 digits; prevent newline char at the end
    print("{0:4d}% | ".format(off), end="") 
    for price in originalPrices:
        # 
        print("{0:.2f}\t".format(new_price(price, off)), end="")
    # print newline at the end of the row
    print()

这将产生如下输出:

                                 Normal Price                                 
Off % | 9.95$   14.95$  19.95$  24.95$  29.95$  34.95$  39.95$  44.95$  49.95$  
------------------------------------------------------------------------------
   5% | 9.45    14.20   18.95   23.70   28.45   33.20   37.95   42.70   47.45   
  10% | 8.96    13.45   17.95   22.45   26.95   31.46   35.96   40.46   44.96   
  15% | 8.46    12.71   16.96   21.21   25.46   29.71   33.96   38.21   42.46   
  20% | 7.96    11.96   15.96   19.96   23.96   27.96   31.96   35.96   39.96   
  25% | 7.46    11.21   14.96   18.71   22.46   26.21   29.96   33.71   37.46   
  30% | 6.96    10.46   13.96   17.46   20.96   24.47   27.97   31.47   34.97   
  35% | 6.47    9.72    12.97   16.22   19.47   22.72   25.97   29.22   32.47   
  40% | 5.97    8.97    11.97   14.97   17.97   20.97   23.97   26.97   29.97   
  45% | 5.47    8.22    10.97   13.72   16.47   19.22   21.97   24.72   27.47   
  50% | 4.97    7.47    9.97    12.47   14.97   17.48   19.98   22.48   24.98

希望有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM