Nov-25-2019, 09:29 PM
See in your example below, are you defining a function "def_filter" that uses the data dictionary and then passing the team in position 4 to it to return the values for the team in position four?
(Nov-25-2019, 08:26 AM)DeaD_EyE Wrote: 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 ofdict.get()
is mandatory, if you don't check if the wanted key exist.
Otherwise you'll get aKeyError
.
Theget
method on adict
, return by defaultNone
, 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.