Python: How to create a common element between a list and a dict

前端 未结 4 2151
旧时难觅i
旧时难觅i 2021-01-22 14:57

I am new to data structures in python and was wondering how do you simulate a thing like pointers in python so that multiple structures can refer and manage the same piece of da

4条回答
  •  旧时难觅i
    2021-01-22 15:31

    Simply put, there is no way to easily link your two structures.

    You could manipulate the object you point to so that it has some "deleted" state and would act as if it's deleted (while being in both containers).

    However, if all you wanted was a list from a dict, use list(the_dict.values()).

    You could make a class to achieve this, if all else fails. See https://docs.python.org/2/reference/datamodel.html#emulating-container-types for the details on what your class would have to have. Within the class, you would have your "duplicated effort," but if it's correctly implemented it wouldn't be error prone.

提交回复
热议问题