Heim  >  Fragen und Antworten  >  Hauptteil

Gibt es eine Möglichkeit, Python zu verwenden, um diese Datenzeichenfolge im gewünschten Format auszugeben?

[('2016-09', 20874.73, '李四'), ('2016-10', 64296.45, '李四'), ('2016-11', 58657.1, '李四'), ('2016-12', 51253.14, '李四'), ('2017-01', 57791.88, '李四'), ('2017-01', 46007.0, '张三'), ('2017-02', 67193.55, '李四'), ('2017-02', 38352.0, '张三'), ('2017-03', 83359.53, '李四'), ('2017-03', 49661.0, '张三'), ('2017-04', 39907.0, '张三')]

Ich möchte die obige Datenzeichenfolge im Format

ausgeben
[{'data': [['2013-04', 52.9], ['2013-05-01', 50.7]], 'name': '张三'},{'data': [['2013-04', 27.7], ['2013-05-01', 25.9]], 'name': '李四'}]

Gibt es bei diesem Format eine andere Möglichkeit? Ich habe lange darüber nachgedacht und kann mir keinen effektiven Weg vorstellen, es zu tun.

漂亮男人漂亮男人2686 Tage vor650

Antworte allen(3)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-06-12 09:23:18

    # python2
    # coding: utf8
    a = [('2016-09', 20874.73, '李四'), ('2016-10', 64296.45, '李四'), ('2016-11', 58657.1, '李四'), ('2016-12', 51253.14, '李四'), ('2017-01', 57791.88, '李四'), ('2017-01', 46007.0, '张三'), ('2017-02', 67193.55, '李四'), ('2017-02', 38352.0, '张三'), ('2017-03', 83359.53, '李四'), ('2017-03', 49661.0, '张三'), ('2017-04', 39907.0, '张三')]
    s = []
    for i in a:
        for dict_tmp in s:
            if dict_tmp.get('name', '') == i[2]:
                dict_tmp['data'].append([i[0], i[1]])
                break
        else:
            s.append(
                {
                    'name': i[2],
                    'data':  [[i[0], i[1]]]
                }
            )
    print s
    
    

    Antwort
    0
  • 为情所困

    为情所困2017-06-12 09:23:18

    from collections import defaultdict
    
    d = defaultdict(list)
    
    l_data = [('2016-09', 20874.73, '李四'), ('2016-10', 64296.45, '李四'), ('2016-11', 58657.1, '李四'), ('2016-12', 51253.14, '李四'), ('2017-01', 57791.88, '李四'), ('2017-01', 46007.0, '张三'), ('2017-02', 67193.55, '李四'), ('2017-02', 38352.0, '张三'), ('2017-03', 83359.53, '李四'), ('2017-03', 49661.0, '张三'), ('2017-04', 39907.0, '张三')]
    
    for x in l_data:
        d[x[2]].append([x[0], x[1]])
    
    result = [{'name': k, 'data': v} for k, v in d.iteritems()]

    Antwort
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-12 09:23:18

    這種情況應該使用pandas模塊比較永續:

    data_input = [('2016-09', 20874.73, '李四'), ('2016-10', 64296.45, '李四'), ('2016-11', 58657.1, '李四'), ('2016-12', 51253.14, '李四'), ('2017-01', 57791.88, '李四'), ('2017-01', 46007.0, '张三'), ('2017-02', 67193.55, '李四'), ('2017-02', 38352.0, '张三'), ('2017-03', 83359.53, '李四'), ('2017-03', 49661.0, '张三'), ('2017-04', 39907.0, '张三')]
    
    import pandas as pd
    df = pd.DataFrame(data_input)
    df.columns = ['month','value','name']
    d = df.set_index(['name'])
    print ( set(d.index) )                           # {'张三', '李四'}
    print ( list(d.loc['张三'].values.tolist()) )  # data變成list
    print ( [{'data':list(d.loc[x].values.tolist()) , 'name': x} for x in set(d.index) ] )   

    最後一行就是你要的結果。基本上就是用倒數第三行索引結果為列表推導基礎,產出你要的字典,內有name及data,而data有列表出的數據

    [{'data': [['2016-09', 20874.73],
       ['2016-10', 64296.45],
       ['2016-11', 58657.1],
       ['2016-12', 51253.14],
       ['2017-01', 57791.88],
       ['2017-02', 67193.55],
       ['2017-03', 83359.53]],
      'name': '李四'},
     {'data': [['2017-01', 46007.0],
       ['2017-02', 38352.0],
       ['2017-03', 49661.0],
       ['2017-04', 39907.0]],
      'name': '张三'}]
      
      

    如果有更多數據處理的需要,真的很推薦把pandas模塊學起來。

    Antwort
    0
  • StornierenAntwort