简体   繁体   English

Python:打印不完整

[英]Python: print incomplete

I have the following problem. 我有以下问题。 I created the following list: 我创建了以下列表:

usinlist = [['strawberries',    2.05,   'rosales'],
            ['pineapples',      2.05,   'poales'],
            ['bananas',         2.05,   'zingiberales'],
            ['oranges',         2.05,   'sapindales'],
            ['apples',          2.05,   'rosales'],
            ['broccoli',        2.05,   'rosales'],
            ['cauliflower',     2.05,   'rosales'],
            ['cucumber',        2.05,   'cucurbitales'],
            ['lettuce',         2.05,   'asterales'],
            ['sprouts',         2.05,   'rosales']]

I need to print this list and sort it alphabetically using the 3 element usinlist[x][x][2]. 我需要打印此列表,并使用3个元素usinlist [x] [x] [2]按字母顺序对其进行排序。 The code I have now to sort is the following: 我现在要排序的代码如下:

print('\n'.join('{}: {}'.format(*k) for k in sorted(usinlist, key=lambda k: (k[2], k[0], k[1])))) 

After sorting the kind/type[2] on alphabetical order, it sort the fruit/vegetable names on alphabetical order, but the problem that occurs is that the it does sort, but doesn't print the list complete. 在按字母顺序对kind / type [2]进行排序后,它按字母顺序对水果/蔬菜名称进行排序,但是出现的问题是,它确实进行了排序,但是没有完整地打印列表。

The output is as following: 输出如下:

lettuce: 2.05
cucumber: 2.05
pineapples: 2.05
apples: 2.05
broccoli: 2.05
cauliflower: 2.05
sprouts: 2.05
strawberries: 2.05
oranges: 2.05
bananas: 2.05

Can anyone tell me what is wrong here :) ? 谁能告诉我这是怎么了:)?

You have only two placeholders in your format string. 您的格式字符串中只有两个占位符。 Try using 尝试使用

print('\n'.join('{}: {} ({})'.format(*k) for k in sorted(usinlist, key=lambda k: (k[2], k[0], k[1]))))

or whatever format you need. 或您需要的任何格式。

和这个?:

print('\n'.join('{}: {}: {}'.format(*k) for k in sorted(usinlist, key=lambda k: (k[2], k[0], k[1]))))

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

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