Posts: 7
Threads: 2
Joined: Nov 2023
Nov-29-2023, 01:43 AM
(This post was last modified: Nov-29-2023, 01:44 AM by gohanhango.)
I have a list containing a lot of serial numbers. Here are some of them:
hub_sn = [10001, 10002, 10003, 10004, 10005]
These serial numbers came from a monitoring database where the serial numbers would be shown as Online or Offline.
The first list is static, type-written manually. Now, from the monitoring database, I took those serial number and checked if they are online or offline.
I came up with a new list.
hub_sn_avail = [(10003, 'Online'), (10004, 'Offline'), (10002, 'Online'), (10001, 'Offline'), (10005, 'Online')]
How would I follow the order of the static list?
Posts: 453
Threads: 16
Joined: Jun 2022
Nov-29-2023, 01:45 AM
(This post was last modified: Nov-29-2023, 02:18 AM by rob101.)
Removed due to the OP having done a full edit of the first post.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 7
Threads: 2
Joined: Nov 2023
Nov-29-2023, 01:55 AM
(This post was last modified: Nov-30-2023, 04:54 PM by Gribouillis.)
for people having the same problem as me...
hub_sn = [10001, 10002, 10003, 10004, 10005]
hub_sn_avail = [(10003, 'Online'), (10004, 'Offline'), (10002, 'Online'), (10001, 'Offline'), (10005, 'Online')]
# Create a dictionary for quick lookup of availability status
avail_dict = dict(hub_sn_avail)
# Use a list comprehension to create a new list with availability status in the order of hub_sn
ordered_avail_status = [(sn, avail_dict[sn]) for sn in hub_sn]
print(ordered_avail_status) Using the dict function, this will output the hub_sn_avail list having the same order as the hub_sn list.
Gribouillis likes this post
Posts: 7
Threads: 2
Joined: Nov 2023
(Nov-29-2023, 01:45 AM)rob101 Wrote: What is unclear here is why the order is important. List objects are "random access" and as such the order should not be relevant. If (for whatever reason) the order IS important, then possibly the best course of action would be to first have the source list sorted, then once the sub-list has been assembled, also sort that sub-list. That way the order will be preserved.
In my case, it is actually important to maintain the order of the static list and follow the order of the static list.
Posts: 453
Threads: 16
Joined: Jun 2022
Nov-29-2023, 02:03 AM
(This post was last modified: Nov-29-2023, 02:14 AM by rob101.)
(Nov-29-2023, 01:57 AM)gohanhango Wrote: In my case, it is actually important to maintain the order of the static list and follow the order of the static list.
In that case, simply use the index positions of the source list as a positional argument for the sub-list.
... holdup. You've updated your postings, so what I've just posted may now look insane.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 7
Threads: 2
Joined: Nov 2023
(Nov-29-2023, 02:03 AM)rob101 Wrote: (Nov-29-2023, 01:57 AM)gohanhango Wrote: In my case, it is actually important to maintain the order of the static list and follow the order of the static list.
In that case, simply use the index positions of the source list as a positional argument for the sub-list.
My actual data actually has more than 4000 values. So if you mean searching every index of the sublist, it might not be good to search every individual index of the sublist. I'm aware that there are stuff life divide and conquer for that? What would you recommend I study with regards to those?
The solution I posted using dict function works well for me but I'm still interested in the other ways I could go about this.
Posts: 6,576
Threads: 19
Joined: Feb 2020
You could sort them.
hub_sn = [10001, 10002, 10003, 10004, 10005]
hub_sn_lookup = {sn: index for index, sn in enumerate(hub_sn)}
hub_sn_avail = [
(10003, "Online"),
(10004, "Offline"),
(10002, "Online"),
(10001, "Offline"),
(10005, "Online"),
]
hub_sn_avail.sort(key=lambda x: hub_sn_lookup.get(x[0], 0))
print(hub_sn_avail) Output: [(10001, 'Offline'), (10002, 'Online'), (10003, 'Online'), (10004, 'Offline'), (10005, 'Online')]
This makes a dictionary where you can use the serial number to get the index of the serial number in the "hub_sn" list. Using a dictionary will be much faster than using hub_sn.index(sn), though for your small list (4000 is a small list) the time savings is minimal.
hub_sn_lookup = {sn: index for index, sn in enumerate(hub_sn)} To sort using the same order as hub_sn, you need a key that converts the (sn, status) tuple to something that is ordered. The key for your sort will look up an index for the serial number in the hub_sn_lookup dictionary.
(key=lambda x: hub_sn_lookup.get(x[0], 0)
Posts: 1,026
Threads: 142
Joined: Jul 2017
This seems to do the job:
hub_sn = [10001, 10002, 10003, 10004, 10005]
hub_sn_avail = [(10003, 'Online'), (10004, 'Offline'), (10002, 'Online'), (10001, 'Offline'), (10005, 'Online')]
mylist = [(k, j[1]) for k in hub_sn for j in hub_sn_avail if k in j] Output: mylist
[(10001, 'Offline'), (10002, 'Online'), (10003, 'Online'), (10004, 'Offline'), (10005, 'Online')]
There may be disadvantages due to the double loop for very long lists.
|