Python 逗号分隔列表格式
我想在一个列表中创建一个包含20个随机数的列表 但要打印这些数字的结果,请使用逗号分隔。 当我想格式化Prinicipal列表时出错 下面 我怎样才能解决这个问题Python 逗号分隔列表格式,python,list,format,Python,List,Format,我想在一个列表中创建一个包含20个随机数的列表 但要打印这些数字的结果,请使用逗号分隔。 当我想格式化Prinicipal列表时出错 下面 我怎样才能解决这个问题 def inventory(i,j): import random Amount = random.choices([x for x in range(i,j+1) if x%25 == 0],k=20) return Amount def main(): Principal = inventory(
def inventory(i,j):
import random
Amount = random.choices([x for x in range(i,j+1) if x%25 == 0],k=20)
return Amount
def main():
Principal = inventory(100000, 1000000)
res = ('{:,}'.format(Principal))
print('\nInventory:\n', + str(res))
minAmt = min(Principal)
maxAmt = max(Principal)
res1 = ('{:,}'.format(minAmt))
res2 = ('{:,}'.format(maxAmt))
print('\nMin amount:' + str(res1))
print('\nMax amount:' + str(res2))
if __name__ == '__main__':
main()
您正在寻找
str.join
。例如
res = ','.join(map(str, Principal))
此外,以下语法无效:
print('\nInventory:\n', + str(res))
那里的
+
是非法的。本金
是金额列表,,
不是列表的格式说明符。单独打印每个金额:
import random
def inventory(i,j):
return random.choices([x for x in range(i,j+1) if x%25 == 0],k=20)
amounts = inventory(100000, 1000000)
print('\nInventory:\n')
for amount in amounts:
print(f'{amount:,}')
print(f'\nMin amount: {min(amounts):,}')
print(f'\nMax amount: {max(amounts):,}')
输出:
Inventory:
283,250
904,600
807,800
297,850
314,000
557,450
167,550
407,475
161,550
684,225
787,025
513,975
252,750
217,500
394,200
777,475
621,575
888,625
895,525
846,650
Min amount: 161,550
Max amount: 904,600
Inventory:
283,250
904,600
807,800
297,850
314,000
557,450
167,550
407,475
161,550
684,225
787,025
513,975
252,750
217,500
394,200
777,475
621,575
888,625
895,525
846,650
Min amount: 161,550
Max amount: 904,600