在字典中存储文件输出

2024-09-30 22:11:30 发布

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

我有一个两列的文件,如下所示

-34G     trendmar
+41G     trendmar
 1.4G    ttpsenet
 3.6G    tyibahco
-13M     uberhelp
+19M     uberhelp
-8.9G    umljgate
+9.2G    umljgate

我想把它存储在字典里做一些数学运算,但是用第一列作为值,第二列作为键。你知道吗

我怎么能这么做?你知道吗


Tags: 文件字典数学tyibahcotrendmaruberhelpumljgatettpsenet
4条回答
with open("file.txt","r") as file:
    print({e.split("     ")[1]:e.split("     ")[0] for e in file})

你可以用字典来理解

假设您希望在计算时将更多值与键相关联,我的方法是:

d = {}
with open("input.txt") as infile:
    lines = infile.readlines()
    keys = sorted(set(line.split()[1] for line in lines))
    for key in keys:
        tempList = []
        for line in lines:
            if line.split()[1]==key:
                tempList.append(line.split()[0])
        d.update({key:tempList})

print(d)

输出:

{'trendmar': ['-34G', '+41G'], 'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G']}

编辑:

如果您希望找到两个值之间的差异,可以使用literal_eval模块中的ast函数来完成,如下所示:

from ast import literal_eval

d = {'trendmar': ['-34G', '+41G'], 'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G']}

first = 0
second = 1

diff = []
for key in d.keys():
    if len(d[key])==2:
        diff.append(key + " : " + str(literal_eval("".join([d[key][first][:-1] ," - (", d[key][second][:-1], ")"]))) + d[key][first][-1])
    else:
        diff.append(key + " : " + str(literal_eval(str(d[key][0][:-1]))) + d[key][0][-1])

print(diff)

输出:

['uberhelp : -32M', 'tyibahco : 3.6G', 'ttpsenet : 1.4G', 'umljgate : -18.1G', 'trendmar : -75G']

在上面的例子中,我们从第二个值中减去第一个值。如果希望相反,则交换firstsecond的值。你知道吗

您可以逐行读取文件,按空格拆分,并反向使用元素来创建字典:

with open("your_file", "r") as f:  # open the file for reading
    # read it line by line, split and invert the fields to use as k:v for your dict
    data = dict(reversed(line.split()) for line in f)
    # {'trendmar': '+41G', 'ttpsenet': '1.4G', 'tyibahco': '3.6G',
    #  'uberhelp': '+19M', 'umljgate': '+9.2G'}

注意dict本质上是一个散列映射,因此它不能有重复的键-如果重复键的值出现在文件中,它们将被最新的值覆盖。你知道吗

更新:如果要保留所有值,必须将它们存储为列表,例如:

import collections

data = collections.defaultdict(list)  # initiate all fields as lists
with open("your_file", "r") as f:  # open the file for reading
    for line in f:  # read the file line by line
        value, key = line.split()  # split the line to value and key
        data[key].append(value)  # append the value to the list for its key

现在你的data看起来像:

{'trendmar': ['-34G', '+41G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G'],
 'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G']}

更新2:如果要对值求和,则首先需要将它们转换为浮点值,然后使用常规算术运算来获得最终值,因此首先编写一个函数,将SI速记符号转换为本机float

QUANTIFIER_MAP = {"p": 1e15, "t": 1e12, "g": 1e9, "m": 1e6, "k": 1e3}
def si_to_float(number):
    try:
        last_char = number[-1].lower()
        if last_char in QUANTIFIER_MAP:
            return float(number[:-1]) * QUANTIFIER_MAP[last_char]
        return float(number)
    except ValueError:
        return 0.0

现在您可以在创建data时用list替换float,并对值求和,而不是追加:

import collections

data = collections.defaultdict(float)  # initiate all fields as integers
with open("your_file", "r") as f:  # open the file for reading
    # read it line by line, split and invert the fields to use as k:v for your dict
    for line in f:  # read the file line by line
        value, key = line.split()  # split the line to value and key
        data[key] += si_to_float(value)  # convert to float and add to the value for this key

这将导致data如下:

{'trendmar': 7000000000.0, 'ttpsenet': 1400000000.0, 'tyibahco': 3600000000.0,
 'uberhelp': 6000000.0, 'umljgate': 300000000.0}

如果要将这些值返回到SI缩写符号中,则必须编写si_to_float()的相反函数,然后使用它转换所有data值,即:

QUANTIFIER_STACK = ((1e15, "p"), (1e12, "t"), (1e9, "g"), (1e6, "m"), (1e3, "k"))
def float_to_si(number):
    for q in QUANTIFIER_STACK:
        if number >= q[0]:
            return "{:.1f}".format(number / q[0]).rstrip("0").rstrip(".") + q[1].upper()
    return "{:.1f}".format(number).rstrip("0").rstrip(".")

# now lets traverse the previously created 'data' and convert its values:
for k, v in data.items():
    data[k] = float_to_si(v)

这将最终导致data包含:

{'trendmar': '7G', 'ttpsenet': '1.4G', 'tyibahco': '3.6G',
 'uberhelp': '6M', 'umljgate': '300M'}
with open("file.txt","r") as file:
    print({e.split("     ")[1]:e.split("     ")[0] for e in file})

你可以用字典来理解

相关问题 更多 >