List in Python help needed: how to Avoid calling repetitive code

The Humble Coder

I have a function shown below:

def _extract_parent(matched_list, json_data, info_type):
    return [json_data[match_lst][info_type] for match_lst in matched_list]

parent_lst = list(map(lambda x: _extract_parent(x,json_data, "parent_info"), flashtext_matched_pattern))

family_lst = list(map(lambda x: _extract_parent(x, json_data, "family_info"),flashtext_matched_pattern))

This code works for me. But I wish to optimize it I keep calling the map from the outside instead I want to do the mapping inside the _extract_parent function and call the function like

res = _extract_parent(flashtext_matched_pattern, json_data, "parent_info")  

My JSON looks like this:

{ "power series": {
        "parent_info" : "abc",
        "family_info" : "xyz",
        "base_info" : "pqr"
  }
}

flashtext_matched_pattern is a list of list which looks like below. It can have empty list as well as values shown in the example

[[],
 ['power series'],
 [],
 [],
 [],
 []]

So if the power series is matched it will give me the info I requested example parent_info or family_info.

Now I wish to optimize the code. I want to augment _extract_parent function to include mapping part too and call the _extract_parent function like

parent_lst = _extract_parent(flashtext_matched_pattern, json_data, "parent_info")
family_lst = _extract_parent(flashtext_matched_pattern, json_data, "family_info")

How can I do the same? Any help in this would be highly appreciated

Or am I thinking in the wrong direction would it be a better solution? If for each matching entry, return a tuple containing metadata parent, family and base info. (If yes how to do that)

Blckknght

Your long statements where you call your code via map and a lambda are just an inconvinient way of doing another list comprehension:

parent_lst = [_extract_parent(x,json_data, "parent_info")
              for x in  flashtext_matched_pattern]

If you want to do everything in to the function, you can make that happen with a nested list comprehension:

def _extract_parent(list_of_match_lists, json_data, info_type):
    return [
             [json_data[match][info_type] for match in match_list]
             for match_list in list_of_match_lists
           ]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to avoid repetitive code in redux (ducks approach)?

Avoid repetitive xml code

Loop to avoid repetitive code

How to avoid repetitive naming in python packages?

Avoid repetitive answer in python

Error in code, help needed debugging, Python

How to avoid repetitive XAML?

How to define WINDOWING function in Spark SQL query to avoid repetitive code

How do I avoid repetitive code in this event handler?

Python: How can I improve the repetitive code?

How to condense nontrivial repetitive code in Python

How to simplify repetitive list comprehensions in python?

Safe code or not? (Help needed)

How to avoid repetitive codes in cypress

How to avoid repetitive JUnit tests

calling help(MyClass) also shows base class attributes: how to avoid that?

C# Avoid Repetitive code and use Generic

Using loops or alternative to avoid repetitive lines of code

Avoid repetitive CMake code with multiple targets

How to loop over hundreds of images in Qulatrics - help needed to implement code

how to avoid calling callback function in children list

Python - How can I make this repetitive code shorter by using loop?

How can i make this Python code less repetitive?

How can I avoid writing repetitive code in javascript for my Tic-Tac-Toe game?

C++ Get/Set accessors - how do I avoid typing repetitive code?

How to avoid repetitive code when different classes use the same methods but have different constructors?

Eliminate or Simplify repetitive Python code

How to avoid repetitive function parameters in clojure?

Postgres how to avoid repetitive if then in select statement