This question already has answers here:
How to return dictionary keys as a list in Python?
                                
                                    (8个答案)
                                
                        
                                2年前关闭。
            
                    
当我做:

d = {'x': 1, 'y': 2, 'z': 'randy'}

print(d.keys())


结果是:

dict_keys(['x', 'y', 'z'])


为什么会有dict_keys?有办法摆脱它吗?

最佳答案

将其转换为列表。

>>> print(list(d.keys()))
['z', 'x', 'y']

10-08 13:04