Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data Dictionaries in Python
#11
You can make a filter-function.
Instead of jumping directly into pandas, you should know also the Python stuff.


def filter_by(data, **kwargs):
    for row in data:
         for key, value in kwargs.items():
             if row.get(key) != value:
                 break
         else:
             yield row


# if the results are saved in the list result
list(filter_by(result, Pos='4'))
In this example I do not type conversion. It's just a string comparison of equality.
The else-block of the for-loop is only then executed, if the for-loop has finished the iteration.
Breaking early out of the loop, will not execute the else-block of the for-loop.
This means only if all keys are existing and return the wanted values, it will yield a hit.
The use of dict.get() is mandatory, if you don't check if the wanted key exist.
Otherwise you'll get a KeyError.
The get method on a dict, return by default None, if the key does not exist.
You can define the default values as second argument in the get method.

The function itself return a generator, if called. The generator does nothing until
it's consumed by a for-loop or types like tuple, list, set, etc...
If you see in a function a yield statement, then it's a generator.

Generators can do funny stuff, like generating infinite sequences.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Data Dictionaries in Python - by mrsenorchuck - Nov-24-2019, 01:31 AM
RE: Data Dictionaries in Python - by Larz60+ - Nov-24-2019, 03:35 AM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-24-2019, 10:12 AM
RE: Data Dictionaries in Python - by DeaD_EyE - Nov-24-2019, 10:32 AM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-24-2019, 10:50 AM
RE: Data Dictionaries in Python - by snippsat - Nov-24-2019, 02:07 PM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-24-2019, 02:11 PM
RE: Data Dictionaries in Python - by perfringo - Nov-24-2019, 02:49 PM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-24-2019, 05:04 PM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-24-2019, 08:02 PM
RE: Data Dictionaries in Python - by DeaD_EyE - Nov-25-2019, 08:26 AM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-25-2019, 09:36 AM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-25-2019, 09:29 PM
RE: Data Dictionaries in Python - by perfringo - Nov-25-2019, 10:51 AM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-25-2019, 02:58 PM
RE: Data Dictionaries in Python - by DeaD_EyE - Nov-25-2019, 04:07 PM
RE: Data Dictionaries in Python - by mrsenorchuck - Nov-25-2019, 04:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I save Python dictionaries in Matlab? jlostinco 1 2,844 Jul-04-2019, 11:35 PM
Last Post: scidam
  creating an 'adress book' in python using dictionaries? apollo 6 14,844 May-06-2019, 12:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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