简体   繁体   English

如何在不使用 ljust/rjust 的情况下并排放置列表

[英]How do I put lists side by side also without using ljust/rjust

As of now this is what my code looks like (It is not fully finished but to continue on this I would like to get the structure down first) :到目前为止,这就是我的代码的样子(它还没有完全完成,但要继续我想先把结构放下来):

(Also more text below) (下面还有更多文字)

def Main():定义主():

city = ['City', '________________', 'Boston', 'Montreal', 'Toronto', 'Detroit', 'Mackinaw City', 'Sault Ste. Marie', 'Wawa', 'Marathon', 'Thunder Bay', 'Duluth', 'Minneapolis', 'Fargo', 'Winnipeg', 'Regina', 'Swift Current', 'Medicine Hat', 'Calgary', 'Great Falls', 'Butte', 'Spokane', 'Seattle', 'Vancouver']
for items in city:
    a = items
    while len(a) < 16:
        a += " "
    
state = ['State/Province', '________________', 'Massachusetts', 'Quebec', 'Ontario', 'Michigan', 'Michigan', 'Ontario', 'Ontario', 'Ontario', 'Ontario', 'Minnesota', 'Minnesota', 'North Dakota', 'Manitoba', 'Saskatachewan', 'Saskatachewan', 'Alberta', 'Alberta', 'Montana', 'Montana', 'Washington', 'Washington', 'British Columbia']
for items in state:
    b = items
    while len(b) < 16:
        b += " "
    
miles = [0, 310, 335, 237, 292, 53, 132, 116, 169, 191, 156, 237, 218, 357, 156, 144, 191, 278, 155, 318, 282, 168]
for items in miles:
    c = items
    kilometers = [c * 1.609344 for c in miles]

    
liters = [35.56, 36.23, 24.26, 32.29, 6.47, 14.27, 12.91, 17.77, 20.67, 18.89, 28.96, 25.78, 37.54, 20.36, 17.58, 22.59, 30.74, 19.57, 35.13, 32.37, 21.19]
for items in liters:
    d = items
    gallons = [d * 3.785411784 for d in liters]

    print (a,b,c,d)

When I do this and run the program it turns out like this:当我这样做并运行程序时,结果是这样的:

Vancouver        British Columbia 168 35.56
Vancouver        British Columbia 168 36.23
Vancouver        British Columbia 168 24.26
Vancouver        British Columbia 168 32.29
Vancouver        British Columbia 168 6.47
Vancouver        British Columbia 168 14.27
Vancouver        British Columbia 168 12.91
Vancouver        British Columbia 168 17.77
Vancouver        British Columbia 168 20.67
Vancouver        British Columbia 168 18.89
Vancouver        British Columbia 168 28.96
Vancouver        British Columbia 168 25.78
Vancouver        British Columbia 168 37.54
Vancouver        British Columbia 168 20.36
Vancouver        British Columbia 168 17.58
Vancouver        British Columbia 168 22.59
Vancouver        British Columbia 168 30.74
Vancouver        British Columbia 168 19.57
Vancouver        British Columbia 168 35.13
Vancouver        British Columbia 168 32.37
Vancouver        British Columbia 168 21.19

and what I really need for it to look like is:我真正需要的是:

Boston          Massachusetts      0        0      0   
Montreal        Quebec           310    40.05  35.56  L
Toronto         Ontario          335    41.55  36.23  L                                                                      
Detroit         Michigan         237    17.05   6.41  G
Mackinaw City   Michigan         292    23.62   8.53  G
Sault Ste. MarieOntario           53     7.79   6.47  L
Wawa            Ontario          132    16.70  14.27  L
Marathon        Ontario          116    15.37  12.91  L
Thunder Bay     Ontario          169    20.38  17.77  L
Duluth          Minnesota        191    14.16   5.46  G
Minneapolis     Minnesota        156    13.17   4.99  G
Fargo           North Dakota     237    20.02   7.65  G
Winnepeg        Manitoba         218    26.94  25.78  L
Regina          Saskatchewan     357    41.80  37.54  L
Swift Current   Saskatchewan     156    23.67  20.36  L
Medicine Hat    Alberta          144    18.63  17.58  L
Calgary         Alberta          191    25.46  22.59  L
Great Falls     Montana          278    21.95   8.12  G
Butte           Montana          155    13.53   5.17  G
Spokane         Washington       318    24.68   9.28  G
Seattle         Washington       282    22.94   8.55  G
Vancouver       British Columbia 168    24.70  21.19  L

I just am not good with Python and I feel like an idiot so if someone could help me with this issue that would be fantastic.我只是不擅长 Python,我觉得自己像个白痴,所以如果有人能帮助我解决这个问题,那就太棒了。

city = ['City', '________________', 'Boston', 'Montreal', 'Toronto', 'Detroit', 'Mackinaw City', 'Sault Ste. Marie', 'Wawa', 'Marathon', 'Thunder Bay', 'Duluth', 'Minneapolis', 'Fargo', 'Winnipeg', 'Regina', 'Swift Current', 'Medicine Hat', 'Calgary', 'Great Falls', 'Butte', 'Spokane', 'Seattle', 'Vancouver']

state = ['State/Province', '________________', 'Massachusetts', 'Quebec', 'Ontario', 'Michigan', 'Michigan', 'Ontario', 'Ontario', 'Ontario', 'Ontario', 'Minnesota', 'Minnesota', 'North Dakota', 'Manitoba', 'Saskatachewan', 'Saskatachewan', 'Alberta', 'Alberta', 'Montana', 'Montana', 'Washington', 'Washington', 'British Columbia']
    
miles = [0, 310, 335, 237, 292, 53, 132, 116, 169, 191, 156, 237, 218, 357, 156, 144, 191, 278, 155, 318, 282, 168]
    
liters = [35.56, 36.23, 24.26, 32.29, 6.47, 14.27, 12.91, 17.77, 20.67, 18.89, 28.96, 25.78, 37.54, 20.36, 17.58, 22.59, 30.74, 19.57, 35.13, 32.37, 21.19]

for a,b,c,d in zip(city, state, miles, liters):
    while len(a) < 16:
        a += " "
    while len(b) < 16:
        b += " "
    kilometers = c * 1.609344

    gallons = d * 3.785411784

    print (a,b,c,d)

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

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