Python Forum
Copying the order of another list with identical values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copying the order of another list with identical values
#1
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?
Reply
#2
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
Reply
#3
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 write Nov-30-2023, 04:54 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Gribouillis likes this post
Reply
#4
(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.
Reply
#5
(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
Reply
#6
(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.
Reply
#7
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)
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,265 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Copy List Not Copying BAbdulBaki 3 630 Aug-19-2023, 02:03 AM
Last Post: perfringo
  Comparing List values to get indexes Edward_ 7 1,182 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Adding values with reduce() function from the list of tuples kinimod 10 2,677 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,025 Jan-19-2022, 08:33 AM
Last Post: menator01
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,125 Jan-09-2022, 02:39 AM
Last Post: Python84
  List of dataframe values beginning with x,y or z glidecode 3 1,949 Nov-08-2021, 10:16 PM
Last Post: glidecode
  How to pass list of values to a API request URL chetansaip99 0 3,537 Sep-28-2021, 07:37 AM
Last Post: chetansaip99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020