Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含两部分名称的字典:
target = { "givenName": "Elvis", "middleName": "Aron", }
middleName是可选的。我需要将一个字符串映射到它们,即givenName + ' ' + middleNameifmiddleName已定义或只是firstName. 这样做的规范方法是什么glom?
middleName
givenName + ' ' + middleName
firstName
glom
到目前为止我找到的解决方案是使用lambdaand Coalesce。middleName无论是否包含以下解决方案都将起作用:
lambda
Coalesce
glom(target, Coalesce( lambda t: t["givenName"] + " " + t["middleName"], "givenName", skip_exc=KeyError), )