Python: Convert list of tuples into a list of objects?

Robert

If I have a list of tuples like the following:

Data = [
        #   OP,  FLAG,   IDENT,  NAME
        ( Op.GA, Flag.GA, "GA", "Asset" ),
        ( Op.LP, Flag.LP, "LP", "Low" ),
        ( Op.HP, Flag.HP, "HP", "High" ),
        ( Op.CU, Flag.CU, "CU", "Custom" ),
]

Is there a clever way to convert this into a list of objects so that it would be like I did the following instead:

Objects = [
        #       OP,    FLAG,    IDENT,  NAME
        OBJECT( Op.GA, Flag.GA, "GA", "Asset" ),
        OBJECT( Op.LP, Flag.LP, "LP", "Low" ),
        OBJECT( Op.HP, Flag.HP, "HP", "High" ),
        OBJECT( Op.CU, Flag.CU, "CU", "Custom" ),
]

I'm hoping for something like Objects = create( OBJECT, Data )

I think I managed to figure out how to convert one element at a time using a loop and tuple unpacking (new to me). So essentially, iterate the tuple list and construct one object at a time as I append them to a second list. Is that the best way to handle this?

The reason: I'm trying to allow external code to send enum-like properties as a list of tuples, then use that to generate a list of class instances to represent enum-like data. The only reason I need classes at all is to allow external code to reference the data using named members, such as enum_ref.Ident (instead of enum_ref[2]). But maybe there is a better way to handle this type of situation?

Really appreciate any advice. I'm inexperienced with Python, so you can safely assume I don't know all of the basics.

Edit: Here is the definition of OBJECT. It is a custom class that can be changed however I need in order to make this work:

class OBJECT:

    # construct / attributes
    def __init__(self,index,value,ident,name):

        self.Index = index  # enum index or type-index
        self.Value = value  # actual value of the entry (any type)
        self.Ident = ident  # identifier string (for Blender and searching)
        self.Name = name    # display name
gst

I think you answered this to yourself already, instead of using a loop you can go with a list comprehension:

Objects = [OBJECT(*x) for x in Data]

Although I see the tuples in your Data are missing one parameter for OBJECT construction (the one corresponding to desc).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python loop through api and append multiple objects as tuples to list

分類Dev

How to convert list of pair tuples in dataframe into columns

分類Dev

convert string csv to List of objects

分類Dev

Grab unique tuples in python list, irrespective of order

分類Dev

How to group list items into sequential tuples in Python?

分類Dev

Transforming Python list of tuples to R named vector

分類Dev

Python, to pick specific elements from a list of tuples

分類Dev

Javascript: Convert list of objects to list of nested objects using groupBy

分類Dev

Convert a Python List to an Array

分類Dev

Python – Convert string to list

分類Dev

Performantly create a list of python dictionaries by combining a numpy array and a list of tuples

分類Dev

How to convert a list of dictionary values containing Timesamp objects into datetime objects in Python?

分類Dev

python sort list of objects by length of a containing list

分類Dev

Converting list of strings to list of tuples

分類Dev

Convert a list of sf objects into one sf

分類Dev

How to convert javascript array to specific list of objects

分類Dev

Python list comprehension Django objects

分類Dev

Java convert list of string with comma separated values to list of objects

分類Dev

Compare Tuple with a List of Tuples

分類Dev

Read file as a list of tuples

分類Dev

Unpair list of tuples

分類Dev

Convert String to Python List of Dictionary

分類Dev

Python: Compare list of strings to list of tuples, and create a new list based on match or no match

分類Dev

How to remove duplicate combinations tuples from python list?

分類Dev

How to check if all tuples have a specific value in a python list

分類Dev

Why does it not print a list of tuples with no repeated tuples?

分類Dev

Haskell: error taking in list of tuples -> outputting list of tuples

分類Dev

Deduping a list of tuples, but keeping collisions as a list

分類Dev

Map list of tuples into list of second elements in Groovy?

Related 関連記事

  1. 1

    Python loop through api and append multiple objects as tuples to list

  2. 2

    How to convert list of pair tuples in dataframe into columns

  3. 3

    convert string csv to List of objects

  4. 4

    Grab unique tuples in python list, irrespective of order

  5. 5

    How to group list items into sequential tuples in Python?

  6. 6

    Transforming Python list of tuples to R named vector

  7. 7

    Python, to pick specific elements from a list of tuples

  8. 8

    Javascript: Convert list of objects to list of nested objects using groupBy

  9. 9

    Convert a Python List to an Array

  10. 10

    Python – Convert string to list

  11. 11

    Performantly create a list of python dictionaries by combining a numpy array and a list of tuples

  12. 12

    How to convert a list of dictionary values containing Timesamp objects into datetime objects in Python?

  13. 13

    python sort list of objects by length of a containing list

  14. 14

    Converting list of strings to list of tuples

  15. 15

    Convert a list of sf objects into one sf

  16. 16

    How to convert javascript array to specific list of objects

  17. 17

    Python list comprehension Django objects

  18. 18

    Java convert list of string with comma separated values to list of objects

  19. 19

    Compare Tuple with a List of Tuples

  20. 20

    Read file as a list of tuples

  21. 21

    Unpair list of tuples

  22. 22

    Convert String to Python List of Dictionary

  23. 23

    Python: Compare list of strings to list of tuples, and create a new list based on match or no match

  24. 24

    How to remove duplicate combinations tuples from python list?

  25. 25

    How to check if all tuples have a specific value in a python list

  26. 26

    Why does it not print a list of tuples with no repeated tuples?

  27. 27

    Haskell: error taking in list of tuples -> outputting list of tuples

  28. 28

    Deduping a list of tuples, but keeping collisions as a list

  29. 29

    Map list of tuples into list of second elements in Groovy?

ホットタグ

アーカイブ