In [1]:
import random
a = random.sample(range(1, 1000), 100)
b = sorted(a)
get_fives = lambda x: [int(i) for i in str(x) if i == '5']
three_fives = []
two_fives = []
one_five = []
zero_fives = []
for num in a:
    if len(get_fives(num)) == 3:
        three_fives.append(num)
    elif len(get_fives(num)) == 2:
        two_fives.append(num)
    elif len(get_fives(num)) == 1:
        one_five.append(num)
    else:
        zero_fives.append(num)
c = sorted(three_fives) + sorted(two_fives) + sorted(one_five) + sorted(zero_fives)
In [20]:
import random
a = random.sample(range(1, 1000), 100)
b = sorted(a)
# def get_fives(num, limit):
#     if int(i) for i in str(x) if i == '5'
    
three_fives = [[num if (sum(1 for i in str(num) if i == '5') == 2)] for num in a]
# two_fives = []
# one_five = []
# zero_fives = []
# for num in a:
#     if len(get_fives(num)) == 3:
#         three_fives.append(num)
#     elif len(get_fives(num)) == 2:
#         two_fives.append(num)
#     elif len(get_fives(num)) == 1:
#         one_five.append(num)
#     else:
#         zero_fives.append(num)
# c = sorted(three_fives) + sorted(two_fives) + sorted(one_five) + sorted(zero_fives)
three_fives
  File "<ipython-input-20-a1e314544214>", line 7
    three_fives = [[num if (sum(1 for i in str(num) if i == '5') == 2)] for num in a]
                                                                      ^
SyntaxError: invalid syntax
In [17]:
# sorted(a, key=lambda x: sum(1 for i in str(x) if i == '5'), reverse=True)
In [ ]: