是否可以使用单行命令将[int,bool,float]转换为['int','bool','float']?

时间:2022-03-03 02:57:37

I convert [int, bool ,float] to ['int', 'bool','float'] using many lines of command.

我使用多行命令将[int,bool,float]转换为['int','bool','float']。

Numbers = [int, bool, float]
>>> [ i for i in Numbers]
[<class 'int'>, <class 'bool'>, <class 'float'>]
>>>foo = [ str(i) for i in Numbers]
>>>foo
["<class 'int'>", "<class 'bool'>", "<class 'float'>"]
>>> bar = [ i.replace('<class ','') for i in foo]
>>> bar
["'int'>", "'bool'>", "'float'>"]
>>> baz = [i.replace('>','') for i in bar]
>>> baz
["'int'", "'bool'", "'float'"]
>>> [ eval(i) for i in baz]
['int', 'bool', 'float']

How to accomplish such a task in an elegant manner?

如何以优雅的方式完成这项任务?

4 个解决方案

#1


13  

You want the __name__ attribute.

您需要__name__属性。

[i.__name__ for i in Numbers]

As an aside, if you are interested in performing introspection on Python data structures, use dir(). For example, dir(int) will return a list of all attributes and callable methods you can use on the int type.

顺便说一句,如果您对Python数据结构进行内省感兴趣,请使用dir()。例如,dir(int)将返回可在int类型上使用的所有属性和可调用方法的列表。

#2


0  

__name__ will help you to show type of it as a string,

you can find available modules for variables and function using dir(var)

你可以使用dir(var)找到变量和函数的可用模块

>>> num = [1,bool(1),1.10]
>>> [type(i).__name__ for i in num] 
['int', 'bool', 'float']
>>> 

#3


0  

There may be a more pythonic way to do this but a 1-liner that works for these types is:

可能有更多的pythonic方法可以做到这一点,但适用于这些类型的1-liner是:

>>> [ x.__name__ for x in Numbers ]
['int', 'bool', 'float']

Btw you can find out attributes an object has using dir()

顺便说一句你可以找到一个对象使用dir()的属性

For example:

>>> dir(Numbers[0])
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

#4


0  

Here you go

干得好

Numbers = [int, bool ,float] 
Labels = [x.__name__ for x in Numbers]

see : https://docs.python.org/2/library/stdtypes.html#special-attributes and https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions for further explanation.

请参阅:https://docs.python.org/2/library/stdtypes.html#special-attributes和https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions以获得进一步说明。

#1


13  

You want the __name__ attribute.

您需要__name__属性。

[i.__name__ for i in Numbers]

As an aside, if you are interested in performing introspection on Python data structures, use dir(). For example, dir(int) will return a list of all attributes and callable methods you can use on the int type.

顺便说一句,如果您对Python数据结构进行内省感兴趣,请使用dir()。例如,dir(int)将返回可在int类型上使用的所有属性和可调用方法的列表。

#2


0  

__name__ will help you to show type of it as a string,

you can find available modules for variables and function using dir(var)

你可以使用dir(var)找到变量和函数的可用模块

>>> num = [1,bool(1),1.10]
>>> [type(i).__name__ for i in num] 
['int', 'bool', 'float']
>>> 

#3


0  

There may be a more pythonic way to do this but a 1-liner that works for these types is:

可能有更多的pythonic方法可以做到这一点,但适用于这些类型的1-liner是:

>>> [ x.__name__ for x in Numbers ]
['int', 'bool', 'float']

Btw you can find out attributes an object has using dir()

顺便说一句你可以找到一个对象使用dir()的属性

For example:

>>> dir(Numbers[0])
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

#4


0  

Here you go

干得好

Numbers = [int, bool ,float] 
Labels = [x.__name__ for x in Numbers]

see : https://docs.python.org/2/library/stdtypes.html#special-attributes and https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions for further explanation.

请参阅:https://docs.python.org/2/library/stdtypes.html#special-attributes和https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions以获得进一步说明。