从字典中提取类似数据并放入新列表或数组

时间:2017-05-14 14:42:24

标签: python arrays dictionary

我有一个包含5个键的字典,每个键都指向一个列表(一个包含100行和5列的CSV文件)。列表的每一行都指向一个人的数据。我想提取每个列表的类似行并放入新列表或数组。所以最后我应该有100个列表/数组,这样每个列表/数组都包含用户的数据。然后我想做机器学习等实验。

这是我的例子:

My_dict={0,1,2,3}

0={id,var1,var2,var3
    User1,med,high,low
    User2,med,low,low
    …,…,..,..,
    User100,hih,low,med}

1={id,var1,var2,var3
    User1,high,med,low
    User2,high,med,low
    …,…,..,..,
    User100,low,low,med}

2={id,var1,var2,var3
    User1,low,med,low
    User2,med,med,low
    …,…,..,..,
    User100,med,low,med}

所以我想要一个可以试验的列表或数组数组列表。像这样:

User1={id,var1,var2,var3
    User1,med,high,low
    User1,high,med,low
    User1,low,med,low
}

User2={d,var1,var2,var3
    User2,med,high,low
    User2,high,med,low
    User2,low,med,low
}

1 个答案:

答案 0 :(得分:1)

input_data = {"0":[["U1","med","low","high"],["U2","low","low","high"],["U3","high","low","high"]], "1": [["U1","med","low","high"],["U2","low","low","high"],["U3","high","low","high"]]}


# Assuming that above kind of data you have then below dict will be your output

users_dict = dict()
for key, users in input_data.iteritems():
    for user in users:
        users_dict.setdefault(user[0], []).append(user)