将文本文件中的数字数据转换为字典

时间:2018-05-08 15:05:09

标签: python python-3.x dictionary text

我有一个文本文件,其中包含:

20 23
0 16
1 2
1 6
1 7
1 8
2 11
2 16
2 17
3 14
3 16
3 17
4 7
4 13
4 17

我需要它在这样的python dict中:

{0:[16],1:[2,6,7,8],2:[11,16,17],3:[14,16,17],4:[7,13,17],20:[23]}

感谢

4 个答案:

答案 0 :(得分:1)

没有声称这是最有效的方式(这只是我脑海中的那个)我会这样做:

my_dict = {}
with open('input_file_name', 'r') as input_file:
    for line in input_file:
        line = line.strip()
        key = line.split(' ')[0]
        value = line.split(' ')[1]
        my_dict[key] = my_dict.get(key, [])
        my_dict[key].append(value)

打印字典:

for key, value in my_dict.items():
    print (key, value)

输出:

4 ['7', '13', '17']
2 ['11', '16', '17']
20 ['23']
1 ['2', '6', '7', '8']
3 ['14', '16', '17']
0 ['16']

字典没有排序。 但是,您可以在打印时对字典进行排序:

for key,value in sorted(my_dict.items(), key=lambda x: int(x[0])):
    print (key, value)

请问我是否需要说明每条线的含义!

答案 1 :(得分:0)

您可以使用collections.defaultdict

下面的完整示例。

import csv
from io import StringIO
from collections import defaultdict

mystr = StringIO("""20 23
0 16
1 2
1 6
1 7
1 8
2 11
2 16
2 17
3 14
3 16
3 17
4 7
4 13
4 17
""")

d = defaultdict(list)

# replace mystr with open('file.csv', 'r')
with mystr as f:
    for i, j in csv.reader(f, delimiter=' '):
        d[int(i)].append(int(j))

结果:

print(d)

defaultdict(list,
            {0: [16],
             1: [2, 6, 7, 8],
             2: [11, 16, 17],
             3: [14, 16, 17],
             4: [7, 13, 17],
             20: [23]})

答案 2 :(得分:0)

您还可以考虑pandas

from StringIO import StringIO
import pandas as pd

>>> z = StringIO("""20 23
0 16
1 2
1 6
1 7
1 8
2 11
2 16
2 17
3 14
3 16
3 17
4 7
4 13
4 17
""")

>>> df = pd.read_table(StringIO(z), header = None, delim_whitespace= True)
>>> {k: list(v) for k,v in df.groupby(0)[1]}

输出:

{0: [16], 1: [2, 6, 7, 8], 2: [11, 16, 17], 3: [14, 16, 17], 4: [7, 13, 17], 20: [23]}

答案 3 :(得分:0)

你最好使用熊猫,

假设您拥有Excel数据表中的数据

enter image description here

此Excel文件的目录是

path = "C:\Users\user\Desktop\python\excel.xlsx"

import pandas as pd

df = pd.read_excel(path)

dictionary = df.groupby("key")["value"].apply(list).to_dict()

print(dictionary)

{0: [16], 1: [2, 6, 7, 8], 2: [11, 16, 17], 3: [14, 16, 17], 4: [7, 13, 17], 20: [23]}