列表是有序且可更改的集合。在Python中,列表用方括号括起来。您通过引用索引号访问列表项。负索引表示从末尾开始,-1表示最后一项。您可以通过指定范围的起点和终点来指定索引范围。指定范围时,返回值将是包含指定项目的新列表。
# List initialization Input = [100.7689454, 17.232999, 60.98867, 300.83748789] # Using list comprehension Output = ["%.2f" % elem for elem in Input] # Printing output print(Output) # List initialization Input = [100.7689454, 17.232999, 60.98867, 300.83748789] # Using map Output = map(lambda n: "%.2f" % n, Input) # Converting to list Output = list(Output) # Print output print(Output) # List initialization Input = [100.7689454, 17.232999, 60.98867, 300.83748789] # Using forrmat Output = ['{:.2f}'.format(elem) for elem in Input] # Print output print(Output) # List initialization Input = [100.7689454, 17.232999, 60.98867, 300.83748789] # Using forrmat Output = ['{:.2f}'.format(elem) for elem in Input] # Print output print(Output)
输出结果
['100.77', '17.23', '60.99', '300.84'] ['100.77', '17.23', '60.99', '300.84'] ['100.77', '17.23', '60.99', '300.84'] ['100.77', '17.23', '60.99', '300.84']