I want to print a dic from the number of "chairs" in that:
'{'VARANDA': [{'tables': [0], 'chairs': 20, 'free': 1}, {'tables': [0, 0, 0, 0, 0], 'chairs': 4}, {'tables': [0], 'chairs': 6}]}'
when I print this, it should return:
VARANDA:
5 tables de 4 chairs.
1 tables de 6 chairs.
1 tables de 20 chairs.
but it is returning:
VARANDA:
1 tables de 20 chairs.
5 tables de 4 chairs.
1 tables de 6 chairs.
i'm using a for loop to print:
for area in sorted(dicCopy):
print(f'{area}:')
for i in dicCopy[area]:
print(f' {len(i["tables"])} tables de {i["chairs"]} chairs.')
how can i print this from the number of chairs in a crescent way?
CodePudding user response:
Use key
for custom sorting of your values (I used here the number of chairs) :
d = {'VARANDA': [{'tables': [0], 'chairs': 20, 'free': 1}, {'tables': [0, 0, 0, 0, 0], 'chairs': 4}, {'tables': [0], 'chairs': 6}]}
for area, lst in d.items():
print(f'{area}:')
for d2 in sorted(lst, key=lambda x: x['chairs']):
print(f' {len(d2["tables"])} tables de {d2["chairs"]} chairs.')
output:
VARANDA:
5 tables de 4 chairs.
1 tables de 6 chairs.
1 tables de 20 chairs.