Lambda expression (correction) that creates an index (dictionary with members as keys) of the members of the object being collected in python

Lambda expression that creates an index (dictionary with members as keys) of the members of the object in the collection with python

import functools as f
makeindexes = lambda objects:{attr:{str(getattr(x,attr)):x for x in objects if attr in dir(x)} for attr in f.reduce(set.union,(set(y.__dict__.keys()) for y in objects if hasattr(y,"__dict__")),set())}

class A:
	def __init__(self,name):
		self.name = name
		self.id = id(self)

objs = [A(x) for x in ["tama","poti","mii"]]

indexes = makeindexes(objs)

print(indexes.keys())
#dict_keys(['id', 'name'])

print(indexes["name"]["poti"].name)
#poti

If the value is not unique as it is, it cannot be indexed, so I decided to wrap it in a list.

mMakeindexes = lambda objects:{outer:f.reduce(lambda d,o:d[getattr(o,outer)].append(o) or d,objects,{getattr(obj,outer):[] for obj in objects if outer in dir(obj)}) for outer in f.reduce(set.union,(set(y.__dict__.keys()) for y in objects if hasattr(y,"__dict__")),set())}

objs = [A("poti") for x in range(5)]

indexes = mMakeindexes(objs)

print(indexes["name"]["poti"])
#[<__main__.A object at 0x7f0b4f2d7710>, <__main__.A object at 0x7f0b4f2d7790>] [<__main__.A object at 0x7f0b4f2d77d0>, <__main__.A object at 0x7f0b4f2d7850>]

Hmmm, can't you write more beautifully?

Recommended Posts

Lambda expression (correction) that creates an index (dictionary with members as keys) of the members of the object being collected in python
The eval () function that calculates a string as an expression in python
How to know the internal structure of an object in Python
The story of creating a bot that displays active members in a specific channel of slack with python
[Python] Summary of functions that return the index that takes the closest value in the array
Get the value of a specific key in a list from the dictionary type in the list with Python
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
File overwrite confirmation with option that takes a file object as an argument with Python argparse
Destroy the intermediate expression of the sweep method with Python
Not being aware of the contents of the data in python
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
Get the value of a specific key up to the specified index in the dictionary list in Python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
Calculate the square root of 2 in millions of digits with python
Python --Find out number of groups in the regex expression
[Homology] Count the number of holes in data with Python
Get the index of each element of the confusion matrix in Python
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value