【问题标题】:python convert list of dict to list of tuplepython将dict列表转换为元组列表
【发布时间】:2020-09-23 09:07:48
【问题描述】:

如何将字典列表转换为元组列表? 输入:

[{'x': 0.4711900100474648, 'y': 0.6294374442355883}, {'x': 0.4732473419066774, 'y': 0.629306809190704}, {'x': 0.47373722332499346, 'y': 0.6274779185623242}, {'x': 0.47363924704133026, 'y': 0.6273908285324014}, {'x': 0.4731493656230142, 'y': 0.6261715681134813}, {'x': 0.4722349203088243, 'y': 0.6252571227992915}, {'x': 0.47210428526394, 'y': 0.62521357778433}, {'x': 0.4709285698599815, 'y': 0.6253442128292143}, {'x': 0.47024273587433907, 'y': 0.62612802309852}, {'x': 0.4706019822477708, 'y': 0.6283052738465912}]

我想要这个:

[(0,47..., 0.62...),(...,...)]

我试过了:

tupleList = [tuple(val['x'], val['y']) for dic in listOfDict for key,val in dic.items()]

我收到错误 TypeError: 'float' object is not subscriptable

【问题讨论】:

  • 您可以将问题简化为小块。例如。尝试for dic in listOfDict: for key,val in dic.items(): print(val) 看看val 是一个数字,你不能这样做val["x"]

标签: python tuples list-comprehension


【解决方案1】:
list_of_points = [{'x': 0.4711900100474648, 'y': 0.6294374442355883}, {'x': 0.4732473419066774, 'y': 0.629306809190704}, {'x': 0.47373722332499346, 'y': 0.6274779185623242}, {'x': 0.47363924704133026, 'y': 0.6273908285324014}, {'x': 0.4731493656230142, 'y': 0.6261715681134813}, {'x': 0.4722349203088243, 'y': 0.6252571227992915}, {'x': 0.47210428526394, 'y': 0.62521357778433}, {'x': 0.4709285698599815, 'y': 0.6253442128292143}, {'x': 0.47024273587433907, 'y': 0.62612802309852}, {'x': 0.4706019822477708, 'y': 0.6283052738465912}]

points_tuples = [(p['x'], p['y']) for p in list_of_points] 

【讨论】:

    【解决方案2】:

    如果您使用的是 python 3.7+ 并以 x,y 顺序插入键(就像这里所做的那样),您可以简单地使用每个内部字典的 dict.values()。 values() 也将按键 (==input) 顺序排列:

    data = [{'x': 0.4711900100474648, 'y': 0.6294374442355883}, 
            {'x': 0.4732473419066774, 'y': 0.629306809190704}, 
            {'x': 0.47373722332499346, 'y': 0.6274779185623242}, 
            {'x': 0.47363924704133026, 'y': 0.6273908285324014}, 
            {'x': 0.4731493656230142, 'y': 0.6261715681134813}, 
            {'x': 0.4722349203088243, 'y': 0.6252571227992915}, 
            {'x': 0.47210428526394, 'y': 0.62521357778433}, 
            {'x': 0.4709285698599815, 'y': 0.6253442128292143}, 
            {'x': 0.47024273587433907, 'y': 0.62612802309852}, 
            {'x': 0.4706019822477708, 'y': 0.6283052738465912}]
    
    tup = [tuple(d.values()) for d in data]
    

    输出:

    [(0.4711900100474648, 0.6294374442355883), (0.4732473419066774, 0.629306809190704), 
     (0.47373722332499346, 0.6274779185623242), (0.47363924704133026, 0.6273908285324014), 
     (0.4731493656230142, 0.6261715681134813), (0.4722349203088243, 0.6252571227992915), 
     (0.47210428526394, 0.62521357778433), (0.4709285698599815, 0.6253442128292143), 
     (0.47024273587433907, 0.62612802309852), (0.4706019822477708, 0.6283052738465912)]
    

    【讨论】:

    • 是否保证x 出现在y 之前?
    • @Jan 与 pyhton 3.8 字典中键的顺序保证为插入顺序 - 因此,如果您将它们输入为 x 则 y 如果您使用该字典的 values(),它们将保持这种状态
    猜你喜欢
    • 2018-05-28
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多