Python Forum
Optimise multiply for loop in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimise multiply for loop in Python
#1
Hi Folks, I have the following code, which works fine. However, as I am very new to Python, there is still space to improve. I would like you to suggest to me the best way to handle this particular case and meanwhile, I can learn from it.

            # Find all methods belonging to this header
            custom_methods_of_the_current_header = [method for method in fmt_data if
                                                    method["type"] == constants.CUSTOM_METHOD and method[
                                                        "parent_id"] == header_id]
            custom_methods_of_the_current_header_ordered = []
            for i in range(1, len(ordered_list) - 1):
                for j in range(len(custom_methods_of_the_current_header)):
                    if custom_methods_of_the_current_header[j]["name"] == ordered_list[i].gg.kind:
                        custom_methods_of_the_current_header_ordered.insert(i - 1,
                                                                            custom_methods_of_the_current_header[j])
                    else:
                        continue
The code there are 3 lists.
custom_methods_of_the_current_header list is getting every matches item and adds inside. ex ["header01", "header03", "header02"]
ordered_list is the list had the ordered item ex. ["mainHeader", "header03", "header01", "header02", "footer"] , as main header and footer are not important, that is why starts range from 1 to 3
custom_methods_of_the_current_header_ordered list I am using to add the final ordered list by comparing custom_methods_of_the_current_header with ordered_list, as the ordered_list the header03 should be the first, then find the header03 in the current header list and adding it in the first index [0, header03]

The code itself works and accomplishes what I need, however, I wonder if I can optimize it to make it look cleaner and reduce the loop and complexity. Thanks in advance
deanhystad write Feb-06-2024, 03:45 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.
Reply
#2
What are you trying to accomplish here? Is this a sort? Not knowing anything about ordered_list, or how the keys in custom_methods_of_the_current_header objects relate to the objects in ordered_list makes thsi difficult to follow.
Reply
#3
(Feb-06-2024, 03:58 PM)deanhystad Wrote: What are you trying to accomplish here? Is this a sort? Not knowing anything about ordered_list, or how the keys in custom_methods_of_the_current_header objects relate to the objects in ordered_list makes thsi difficult to follow.

I updated the question. I want to see if I can optimize the code itself, its already a working piece
Reply
#4
But what is it supposed to accomplish? I don't have fmt_data or constants or ordered_list, so I cannot run your code to see what the input values are and what the results are. Without knowing that it is difficult to offer any suggestions on how the code could be improved. Maybe your code is perfect and there is no way it could be made any better. Maybe your code doesn't really work and it is only producing the desired result by accident (happens more often than you would believe). There is no way to tell.

Generally, iterating is used instead of integer indexing in Python. I hardly ever write code that uses range(). list.insert() is also unusual. I can't tell if it is needed or not.

If you want some useful feedback you should provide a runnable example
Reply
#5
I think you want this
indexes = {item.gg.kind: index for index, item in enumerate(ordered_list)}


def sort_key(item):
    return indexes[item["name"]]


custom_methods_of_the_current_header_ordered = sorted(
    custom_methods_of_the_current_header, key=sort_key
)
rob101 likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,044 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Optimise experiment control parameters rickticktock 2 2,513 Apr-02-2019, 10:54 AM
Last Post: rickticktock

Forum Jump:

User Panel Messages

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