Python3将数据文件转换为字典,每个键有多个值并显示i

2024-10-02 10:25:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处寻找答案,但找不到。我有一个包含这些键/值的数据文件:

oolong:8580.0:7201.25:8900.0 
earl grey:10225.25:9025.0:9505.0
green:6700.1:5012.45:6011.0
mint:9285.15:8276.1:8705.0
jasmine:7901.25:4267.0:7056.5

数据如下-茶_名称:store1\u Sales:存储2_销售:store3\u销售在

我需要能够显示这个输出:

^{2}$

我知道我可以使用

with open('tea.txt') as f:
   teas = f.read().splitlines()

我不知道如何将一个列表转换为一个单键具有多个值的dict。感谢任何帮助。在

编辑:我现在知道如何获取列表并转换成字典。谢谢各位!在


Tags: 数据答案名称列表数据文件withgreengrey
3条回答

获取dict的最简单方法:

with open('1.txt') as f:    
    data = {}
    for row in f:                   
        row = row.strip().split(':')
        data[row[0]] = row[1:]

for key, value in data.items():     
    print('%s %s %s' % (key, ' '.join(value), sum([float(v) for v in value])))

解决这个问题有很多不同的方法。我来教你一种读茶叶的方法。在

teas = {}
with open('tea.txt') as f:
    # step through the file, line by line,
    # so that you don't read in a huge file all at once
    for line in f:
        # split the line by your delimiter ':'
        t = line.split(':')
        # create your dictionary with a key, value pair
        teas[t[0]] = t[1:]

如果需要每个列表的数值,可以将它们映射到适当的数据类型。下面是两个示例,说明如何更改上面的最后一行以获得数字列表/数组。在

  • 您可以使用简单的内置函数来执行此操作:

    teas[t[0]] = map(float, t[1:])
    
  • 或者,您可以使用numpy数组

    import numpy
    # ....
    teas[t[0]] = numpy.array(t[1:], dtype=float)
    

最终的字典如下所示:

{'earl grey': (10225.25, 9025.0, 9505.0),
 'green': (6700.1, 5012.45, 6011.0),
 'jasmine': (7901.25, 4267.0, 7056.5),
 'mint': (9285.15, 8276.1, 8705.0),
 'oolong': (8580.0, 7201.25, 8900.0)}

您可以使用pandas轻松完成此操作:

import pandas as pd
from io import StringIO

# makes it easy to read globs of text like the data you posted above
data = StringIO('''oolong:8580.0:7201.25:8900.0 
earl grey:10225.25:9025.0:9505.0
green:6700.1:5012.45:6011.0
mint:9285.15:8276.1:8705.0
jasmine:7901.25:4267.0:7056.5''')

df = pd.read_csv(data, sep = ':', header = None)

# returns a list of column names from the string you have above
df.columns = "tea_name:store1_Sales:store2_Sales:store3_Sales".split(':')

# add up the sales for stores 1, 2, and 3 for each type of tea to get total sales for a given tea
df['total_sales'] = df[['store1_Sales', 'store2_Sales', 'store3_Sales']].sum(axis = 1)

结果如下:

^{pr2}$

编辑:要从这个pandas.DataFrame对象获取dict,只需执行以下操作:

>>> df.to_dict()
{'store1_Sales': {0: 8580.0, 1: 10225.25, 2: 6700.1000000000004, 3: 9285.1499999999996, 4: 7901.25}, 'tea_name': {0: 'oolong', 1: 'earl grey', 2: 'green', 3: 'mint', 4: 'jasmine'}, 'total_sales': {0: 24681.25, 1: 28755.25, 2: 17723.549999999999, 3: 26266.25, 4: 19224.75}, 'store3_Sales': {0: 8900.0, 1: 9505.0, 2: 6011.0, 3: 8705.0, 4: 7056.5}, 'store2_Sales': {0: 7201.25, 1: 9025.0, 2: 5012.4499999999998, 3: 8276.1000000000004, 4: 4267.0}}

Edit2:忽略pandas,您可以像这样在基本Python中完成您想要的

teas_dict = {}
for row in teas:
    row_list = row.split(':')
    tea = row_list[0] # tea name is always the first element in a row
    sales = row_list[1:] # remaining elements in row_list are sales data
    teas_dict[tea] = sales

等效地,使用dict理解:

>>> teas_dict = {row.split(':')[0]: row.split(':')[1:] for row in teas}
>>> teas_dict
{'earl grey': ['10225.25', '9025.0', '9505.0'], 'green': ['6700.1', '5012.45', '6011.0'], 'oolong': ['8580.0', '7201.25', '8900.0 '], 'mint': ['9285.15', '8276.1', '8705.0'], 'jasmine': ['7901.25', '4267.0', '7056.5']}

最后,要想在最后得到你的累计销售额:

for tea in teas_dict:
    total_sales = sum(map(float, teas_dict[tea]))
    teas_dict[tea].append(total_sales)

结果:

>>> teas_dict
{'earl grey': ['10225.25', '9025.0', '9505.0', 28755.25], 'green': ['6700.1', '5012.45', '6011.0', 17723.55], 'oolong': ['8580.0', '7201.25', '8900.0 ', 24681.25], 'mint': ['9285.15', '8276.1', '8705.0', 26266.25], 'jasmine': ['7901.25', '4267.0', '7056.5', 19224.75]}

相关问题 更多 >

    热门问题