简体   繁体   English

用2位数字初始化列表

[英]initialize a list with 2 digit numbers

I have a list of other lists I am using to represent a map of my workspace. 我有一个其他列表的列表,用于表示我的工作区图。 Here is a simplified version: 这是一个简化的版本:

areaMap = [[00,00,00,00,00],[00,00,01,11,01]]

Now, when i print this onto the screen using: 现在,当我使用以下命令将其打印到屏幕上时:

for x in range(len(areaMap)):
    print(areaMap[x])

the leading zero will be chopped off: 前导零将被切掉:

[0,0,0,0,0],
[0,0,1,11,1]

I need these numbers to have 2 digits so they are visually appealing when printed. 我需要这些数字包含2位数字,以便在打印时具有视觉吸引力。 I tried using: 我尝试使用:

for y in areaMap:
    [str(item).zfill(6) for item in areaMap[y]]

but this creates unneeded quotation marks and spaces. 但这会创建不必要的引号和空格。

You need to specify the format for the entirety of each line; 您需要为每一行的整体指定格式; for example 例如

for x in areaMap:
    print("[%s]" % (",".join(["%02d"%z for z in x])))

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

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