使用Python脚本通过遍历CSV文件数据来创建词典列表

时间:2018-08-24 02:08:58

标签: python json dictionary csvtoarray

我的数据格式为

from        to
Location1   Location2
Location1   Location3
Location1   Location4
Location1   Location5

Location2   Location1
Location2   Location3

Location3   Location1
Location3   Location2
Location3   Location4

在一个csv文件中。该数据映射了从一个车站到另一个车站的自行车旅行,并取自芝加哥的自行车租赁公司的网站。

现在,我有基本的代码可以将每一行添加到列表中,但是并没有如我所愿地在第二个索引中创建字典。我的脚本如下:

import csv
li = []
with open('Desktop/test_Q4_trips.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for name, imports in reader:
    li.append({
        "name": name,
        "imports": imports,
    })
del li[0]

这是输出,

[{"from": "Location1", "to": "Location2"}, {"from": "Location1", "to": "Location3"},
{"from": "Location1", "to": "Location4"}, {"from": "Location1", "to": "Location5"}, 
...]

我想将此数据转换为这种格式,

[{"from": "Location1", "to": ["Location2", "Location3", "Location4", "Location5"]},
    {"from": "Location2", "to": ["Location1", "Location3"]},
    {"from": "Location3", "to": ["Location1", "Location2", "Location4"]}, ...
].

换句话说,我想创建一个字典列表,其中每个字典在第一个索引中都有一个值,在第二个索引中有一个(可变的)值列表。特别是,输出应在第二个索引的列表中列出自行车租赁行程接收端的所有车站。为此,我想我将不得不创建一个带for循环的脚本,该脚本循环遍历左侧的“ from”值,并将与每个“ from”对应的每个“ to”位置附加到列表中。

我希望我的数据采用我要提到的特定格式,以便使用我拥有的数据可视化代码。我确定创建自己想要的格式需要思想上的飞跃,但是我不确定到底该怎么做才能满足此要求。我也不确定我需要的输出类型是列表还是数组,并且希望对此进行澄清。

请帮助我解决此问题,谢谢。

2 个答案:

答案 0 :(得分:2)

collections.defaultdict可能是解决此问题的好方法。

from collections import defaultdict


d = defaultdict(list)

a = [{"from": "Location1", "to": "Location2"}, {"from": "Location1", "to": "Location3"},
     {"from": "Location1", "to": "Location4"}, {"from": "Location1", "to": "Location5"}]


for o in a:
    d[o['from']].append(o['to'])

print(d)

答案 1 :(得分:0)

我认为这应该有效

import numpy as np
l = [{"from": "Location1", "to": "Location2"}, {"from": "Location1", "to": "Location3"},
 {"from": "Location1", "to": "Location4"}, {"from": "Location1", "to": "Location5"}]

from_to = np.array(([d['from'] for d in l],[d['to'] for d in l])).T
froms = set(from_to[:,0])

out = []
for f in froms: 
    d = {}
    mask = from_to[:,0]==f
    d['from']=f
    d['to'] = from_to[:,1][mask]
    out.append(d)