在Python中打印格式化字典,而不使用JSON、panda、numpys

50pmv0ei  于 2023-02-26  发布在  Python
关注(0)|答案(2)|浏览(117)

这是一个任务,因此有限制。
这是我的字典:

{'A': 44.0, 'B': 79.0, 'C': 60.0,
 'D': 56.0, 'E': 46.0, 'F': 50.0,
 'G': 65.0, 'H': 45.0, 'I': 38.0,
 'J': 49.0, 'K': 51.0, 'L': 36.0,
 'M': 48.0, 'N': 55.0, 'O': 56.0,
 'P': 53.0, 'Q': 41.0, 'R': 58.0,
 'S': 65.0, 'T': 36.0, 'U': 23.0,
 'V': 49.0, 'W': 46.0, 'X': 48.0,
 'Y': 54.0, 'Z': 52.0, 'a': 63.0,
 'b': 55.0, 'c': 37.0, 'd': 55.0,
 'e': 63.0, 'f': 58.0, 'g': 40.0,
 'h': 31.0, 'i': 36.0, 'j': 50.0,
 'k': 51.0, 'l': 36.0, 'm': 32.0,
 'n': 58.0, 'o': 50.0, 'p': 55.0,
 'q': 55.0, 'r': 53.0, 's': 61.0, 
 't': 57.0, 'u': 37.0, 'v': 33.0, 
 'w': 59.0, 'x': 53.0, 'y': 54.0,
 'z': 39.0, '!': 35.0, '&': 65.0,
 '*': 65.0, '$': 65.0, '#': 64.0,
 '.': 30.0, ',': 36.0, '<': 38.0,
 '>': 38.0, '?': 45.0, '@': 65.0}

我怎么打印出这样的?

Pricing for font size 24pts
===========================
A 44    B 79    C 60    D 56    E 46    F 50    G 65    
H 45    I 38    J 49    K 51    L 36    M 48    N 55    
O 56    P 53    Q 41    R 58    S 65    T 36    U 23    
V 49    W 46    X 48    Y 54    Z 52    a 63    b 55    
c 37    d 55    e 63    f 58    g 40    h 31    i 36    
j 50    k 51    l 36    m 32    n 58    o 50    p 55    
q 55    r 53    s 61    t 57    u 37    v 33    w 59    
x 53    y 54    z 39    ! 35    & 65    * 65    $ 65    
# 64    . 30    , 36    < 38    > 38    ? 45    @ 65

这是我到目前为止所拥有的,它以3行漂亮的行打印出来,但不太理想。您可以忽略fontpricing,因为它只是用于更改每个字符的价格。

def showPricing(charPricing, fontPricing):
    fontsize = int (input("Enter font size: "))
    print (f'Pricing for font size {fontsize}pts')
    for k, v in charPricing.items():
        print (k, math.ceil(v * fontPricing[fontsize]), end = ('\t\t\t\t'))
wfsdck30

wfsdck301#

既然你知道每行需要7个字符,那么就用end=""打印每个键-值对来隐藏换行符,然后每7次迭代打印一个:

for i, (char, price) in enumerate(charPricing.items()):
    char_price = math.ceil(price * fontPricing[fontsize])
    # create desired format and print
    print(f"{char} {char_price:.0f}\t", end="") 

    if i % 7 == 6:
        print() # print newline

它提供了所需的输出:

A 44    B 79    C 60    D 56    E 46    F 50    G 65    
H 45    I 38    J 49    K 51    L 36    M 48    N 55    
O 56    P 53    Q 41    R 58    S 65    T 36    U 23    
V 49    W 46    X 48    Y 54    Z 52    a 63    b 55    
c 37    d 55    e 63    f 58    g 40    h 31    i 36    
j 50    k 51    l 36    m 32    n 58    o 50    p 55    
q 55    r 53    s 61    t 57    u 37    v 33    w 59    
x 53    y 54    z 39    ! 35    & 65    * 65    $ 65    
# 64    . 30    , 36    < 38    > 38    ? 45    @ 65

注意:因为没有指定fontPricing[fontsize],所以我假设它是1

li9yvcax

li9yvcax2#

下面是一个可能的解决方案。注意,通过指定输出表中的列数,我们可以一次打印一个单元格,并计算相应键在char定价字典中的位置。

import math

char_price = {'A': 44.0, 'B': 79.0, 'C': 60.0, 'D': 56.0, 'E': 46.0, 'F': 50.0, 'G': 65.0, 'H': 45.0, 'I': 38.0, 'J': 49.0, 'K': 51.0, 'L': 36.0, 'M': 48.0, 'N': 55.0, 'O': 56.0, 'P': 53.0, 'Q': 41.0, 'R': 58.0, 'S': 65.0, 'T': 36.0, 'U': 23.0, 'V': 49.0, 'W': 46.0, 'X': 48.0, 'Y': 54.0, 'Z': 52.0, 'a': 63.0, 'b': 55.0, 'c': 37.0, 'd': 55.0, 'e': 63.0, 'f': 58.0, 'g': 40.0, 'h': 31.0, 'i': 36.0, 'j': 50.0, 'k': 51.0, 'l': 36.0, 'm': 32.0, 'n': 58.0, 'o': 50.0, 'p': 55.0, 'q': 55.0, 'r': 53.0, 's': 61.0, 't': 57.0, 'u': 37.0, 'v': 33.0, 'w': 59.0, 'x': 53.0, 'y': 54.0, 'z': 39.0, '!': 35.0, '&': 65.0, '*': 65.0, '$': 65.0, '#': 64.0, '.': 30.0, ',': 36.0, '<': 38.0, '>': 38.0, '?': 45.0, '@': 65.0}
font_price = {24: 1}

def showPricing(charPricing, fontPricing, num_col=7):
    fontsize = int (input("Enter font size: "))
    title = f'Pricing for font size {fontsize}pts'
    print (title)
    print('=' * len(title))
    keys = list(charPricing.keys())
    row = 0
    for row in range(math.ceil(len(charPricing) / num_col)):
        for col in range(num_col):
            k = keys[num_col * row + col]
            print(k, math.ceil(charPricing[k] * fontPricing.get(fontsize, 0)), end='\t')
        print()  # new line
    
showPricing(char_price, font_price)

相关问题