reduce list of functions with keyword argument on dictionary in python

Tytire Recubans

:)

I want to build a very tiny functional pipeline in my code to reduce a bunch of functions on a single dictionary. Every function does 1 thing, but on different keys, here's a simplified example:

I have this dictionary

d = {"me": "gab", "you": "cita"}

and suppose a silly function like this:

def make_caps(dic, on=None):
    if on is not None:
        to_cap = dic.get(on, None)
    if to_cap is not None:
        dic[on] = to_cap.upper()
    return dic

I would like to have this working:

def reduce_fns_on_dict(fns, dic):
    from functools import reduce
    return reduce(lambda d, f: f(d), fns, dic)

new_dic = reduce_fns_on_dict(fns=[make_caps(on="me"), make_caps(on="you")], 
                             dic=d)

print(new_dic)

on console

{'me': 'GAB', 'you': 'CITA'}

But when I run the above I get:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: make_caps() missing 1 required positional argument: 'dic'
FiddleStix

make_caps(on="me") calls the make_caps function. What you want is a new function which behaves like make_caps(some_arg, on='me'). You can make such a function with functools.partial. The line will look something like fns=[partial(make_caps, on="me")....

EDIT: Alternatively, you could curry the function.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

List of Dictionary - How to combine a list of dictionary Python

Python: Dictionary with list of dictionaries

python : save dictionary into list

Converting a list to a dictionary in python

List elements as argument in decorator (Python)

What are the rules for mixing keyword argument and * tuple in python function call

How to merge dictionary in list with Python

Sort python dictionary with list as value

Python vlookup between list of dictionary

Selecting a subset of functions from a list of functions in python

Python : javascript error : missing) after argument list

Efficiency: 2D-list to dictionary in python

python list comprehension get dictionary by key

How to get a value from a list of dictionary Python?

Write dictionary values (list) to output file - Python

Python: Convert a list into a bidimensional mapping dictionary

python run list of functions untill one passes

Python: Loop a list of functions and asset result

Populate dictionary with decorator functions

Haskell argument application through '$x' in 'map' to a list of inflix operator functions. How does it work?

RetrieveAPIView must be called with a URL keyword argument

__init__() got unexpected keyword argument 'y'

Invalid Keyword Argument error in Django-MongoEngine

Count occurrences of assorted items in a List in a Dictionary Value in Python

Is Python has any english dictionary that returns a list of words with the same meaning?

How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

Flutter: Functions with Argument in Raised Button

Template argument functions as class members

Read a dictionary list, within another dictionary list