zoukankan      html  css  js  c++  java
  • 字典嵌套列表获取每个字典的值(转载)

    字典嵌套列表获取每个字典的值(values)

    方法一:

    x=[{'close': '0.1021',
                        'datetime': '2018-10-12 15:21:00',
                        'high': '0.1021',
                        'low': '0.1021',
                        'open': '0.1021',
                        'time': '1539328860000',
                        'volume': '1643.3757'},
                       {'close': '0.1021',
                        'datetime': '2018-10-12 15:20:00',
                        'high': '0.1021',
                        'low': '0.1021',
                        'open': '0.1021',
                        'time': '1539328800000',
                        'volume': '4232.6447'},
                       {'close': '0.1019',
                        'datetime': '2018-10-12 15:18:00',
                        'high': '0.1019',
                        'low': '0.1019',
                        'open': '0.1019',
                        'time': '1539328680000',
                        'volume': '2909.9173'},]

    首先判断x的长度

    num_ohlcvs=len(x)

    创建一个空列表

    result=[]

    i=0

    设定条件<这个长度

    while i < num_ohlcvs:
           长度下标遍历的数据

           on3=x[i]

           获取遍历字典的值,强制转换

           on4=list(on3.values())

           i=i+1

          添加到空的列表中

         result.append(on4)

    print(result)     

    [['1539328860000',
      '0.1021',
      '0.1021',
      '0.1021',
      '0.1021',
      '1643.3757',
      '2018-10-12 15:21:00'],
     ['1539328800000',
      '0.1021',
      '0.1021',
      '0.1021',
      '0.1021',
      '4232.6447',
      '2018-10-12 15:20:00'],
     ['1539328680000',
      '0.1019',
      '0.1019',
      '0.1019',
      '0.1019',
      '2909.9173',
      '2018-10-12 15:18:00']]

    方法二

    list=[]

    for it  in x:

        for key in it:

             list.append(it[key])

    n=7

    on2=[list [i:i+n] for i in range(0,len(list),  n) ]

    print(on2) 

    两个效果一样

  • 相关阅读:
    一些技术摘选及随想
    新技术学习方法如何学习一门新编程语言(Lua)?
    什么时候该用ASSERT?
    MFC是否过时?如何学习MFC?
    Windows桌面开发者的必备软件
    Comet技术选择,论Is node.js best for Comet?
    关于C/C++内存管理一些乱讲
    debug
    C语言趣味题目
    CSS之简单的双引号
  • 原文地址:https://www.cnblogs.com/master-road/p/11089786.html
Copyright © 2011-2022 走看看