Reputation: 59
I have 'TO-DO LIST' like below. And I want to arrange 'TO-DO LIST' following "Argent Level", "The time required" and "Incoming order" without sort() function
# "Argent Level", "The time required" and "Incoming order"
work_list = [
"Low_Level, 1, A.Login Error",
"High_Level, 3, B.User info Error",
"Nomal_Level, 1, C.Admin Error",
"Low_Level, 2, D.Icon change of User",
"High_Level, 3, E.Email export Error",
"Nomal_Level, 1, F.Photo size Error",
"High_Level, 2, G.Admin Page Error",
"Nomal_Level, 3, H.Customer Page Error",
"Low_Level, 1, I.Image box Error",
"Nomal_Level, 3, J.Border Error",
"Low_Level, 1, K.Background Color Error",
"High_Level, 2, L.SignUp Error",
]
For example, There are same "High_Level" List like below.
"High_Level, 3, B.User info Error",
"High_Level, 3, E.Email export Error",
"High_Level, 2, G.Admin Page Error",
"High_Level, 2, L.SignUp Error",
First I want to make this list like below order by "The time required" and if they have same "The time required", next order by Incoming order using a capital letter of "Incoming order"
"High_Level, 2, G.Admin Page Error",
"High_Level, 2, L.SignUp Error",
"High_Level, 3, B.User info Error",
"High_Level, 3, E.Email export Error",
And I tried to make code like this. But It was not correct.
high_level_list = []
nomal_level_list = []
low_level_list = []
for i in range(0, len(work_list)):
if "High_Level" in work_list[i]:
high_level_list.append(work_list[i])
for i in range(0, len(work_list)):
if "Nomal_Level" in work_list[i]:
nomal_level_list.append(work_list[i])
for i in range(0, len(work_list)):
if "Low_Level" in work_list[i]:
low_level_list.append(work_list[i])
for i in range(0, len(high_level_list)):
if high_level_list[i][1] >= 3:
temp = high_level_list[-1]
high_level_list[-1] = high_level_list[i]
high_level_list[i] = temp
print(high_level_list)
I wanna output like below
"High_Level, 2, G.Admin Page Error",
"High_Level, 2, L.SignUp Error",
"High_Level, 3, B.User info Error",
"High_Level, 3, E.Email export Error",
"Nomal_Level, 1, C.Admin Error",
"Nomal_Level, 1, F.Photo size Error",
"Nomal_Level, 3, H.Customer Page Error",
"Nomal_Level, 3, J.Border Error",
"Low_Level, 1, A.Login Error",
"Low_Level, 1, I.Image box Error",
"Low_Level, 1, K.Background Color Error",
"Low_Level, 2, D.Icon change of User",
Upvotes: 0
Views: 51
Reputation: 82929
First, you will want to convert your work_list
to a list of tuples, instead of just strings, so you can access the individual fields, in particular the number that you need in the last step.
>>> work_list_tuples = [tuple(s.split(", ")) for s in work_list]
Your first loops should work, but you can make them more compact as list comprehensions
>>> high = [x for x in work_list_tuples if x[0] == "High_Level"]
Your last loop is kind of a bugged insertion sort, but instead of trying to fix that you can use another list comprehension to filter the high
items where the value is 3 or higher, too:
>>> high_3 = [x for x in high if int(x[1]) >= 3]
This gives you
[('High_Level', '3', 'B.User info Error'),
('High_Level', '3', 'E.Email export Error')]
Do the same for normal
and low
level and for high
with lower than 3, and put the lists back together.
>>> high_3 + high_12 + norm + low
[('High_Level', '3', 'B.User info Error'),
...
('High_Level', '2', 'L.SignUp Error'),
...
('Nomal_Level', '1', 'C.Admin Error'),
...
('Low_Level', '1', 'K.Background Color Error')]
However, in all remotely practical application, I would strongly suggest just using sort
(the list method) or sorted
(the builtin function) with an appropriate key function, e.g. like this:
>>> sorted(work_list_tuples, key=lambda x: ("HNL".index(x[0][0]), -int(x[1])))
[('High_Level', '3', 'B.User info Error'),
('High_Level', '3', 'E.Email export Error'),
('High_Level', '2', 'G.Admin Page Error'),
('High_Level', '2', 'L.SignUp Error'),
('Nomal_Level', '3', 'H.Customer Page Error'),
('Nomal_Level', '3', 'J.Border Error'),
('Nomal_Level', '1', 'C.Admin Error'),
('Nomal_Level', '1', 'F.Photo size Error'),
('Low_Level', '2', 'D.Icon change of User'),
('Low_Level', '1', 'A.Login Error'),
('Low_Level', '1', 'I.Image box Error'),
('Low_Level', '1', 'K.Background Color Error')]
Upvotes: 1