Monkey Raptor

Friday, May 22, 2015

Python: Get the Indexes of Elements from a List (Counting Same Occurrences)

This is the "translation" of earlier JavaScript post about Counting Same Occurrences.
Here, I have Python 2.7.7.

The concept
The main idea of this function is the same as the JavaScript version (link above - so that I don't have to re-explain it[I'm quite lazy that way]).

In this snippet, I typed a function that generates a dictionary (in JavaScript: object) output from a list (in JavaScript: array) input.

The output consists of
list element: {"index": [array of indexes], "length": array of indexes length}
pairs.

The "confusing" part was when I tried to initialize the list type for the non-existing dictionary key.

The "solution" is using try-except block, with KeyError exception.

Let's take a look

Result
I'm using pprint (Pretty Print) module here, so the output can be read conveniently.

Accessing the output
To print the list of indexes, do this:
print occurrence(your_list)["the_element"]["index"]
And to print the list length (of a particular element):
print occurrence(your_list)["the_element"]["length"]
You can always store it as a variable and do further process.

List of keywords
  1. def
    A (custom) function declaration in Python: more info
  2. isinstance(the_object, class_info)
    It is a built-in function to test whether the_object is an instance of class_info argument. Like instanceof operator in JavaScript (also in PHP and Java, Ruby has some methods you can choose).
    The the_object in the snippet above is denoted by v (an item from a list) and class_info is list: more info
  3. for index, value in enumerate(iterable_input)
    A looping block which iterates over the iterable_input. With this modification, we can also access the index of a particular value. Like forEach in JavaScript or foreach in PHP (and Java has the funkiest form for that):
  4. if statement
    Just like JavaScript or any other programming languages conditional statement block: more info
  5. not operator
    Used in Boolean operation. In JavaScript, it is an exclamation mark (!). It negates an argument: more info
  6. str(the_input) function
    Convert the_input into string object. In JavaScript, it's like toString(): more info
  7. append(an_item) method
    A method to append an_item into a list (which is declared in front of the method). In JavaScript, it is equivalent to push(): more info
  8. try-except block
    It is like try-catch-[finally] error block handling in JavaScript:
  9. Other than those...
    are custom variable/parameter keywords.
    They are:
    • occurrence (the name of the function).
    • result (output).
    • v (item value).
    • i (index).
    • a (received list input).

Using pprint - data pretty printer
Read more on pprint documentation
Python: Get the Indexes of Elements from a List (Counting Same Occurrences)
https://monkeyraptor.johanpaul.net/2015/05/python-get-indexes-of-elements-from.html?m=0

No comments

Post a Comment

Tell me what you think...