Python 在字典中存储文件输出
我有一个包含两列的文件,如下所示Python 在字典中存储文件输出,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有一个包含两列的文件,如下所示 -34G trendmar +41G trendmar 1.4G ttpsenet 3.6G tyibahco -13M uberhelp +19M uberhelp -8.9G umljgate +9.2G umljgate 我想将其存储在字典中,以进行一些数学运算,但使用第一列作为值,第二列作为键 我怎么能这么做 with open("file.txt","r") as file: pri
-34G trendmar
+41G trendmar
1.4G ttpsenet
3.6G tyibahco
-13M uberhelp
+19M uberhelp
-8.9G umljgate
+9.2G umljgate
我想将其存储在字典中,以进行一些数学运算,但使用第一列作为值,第二列作为键
我怎么能这么做
with open("file.txt","r") as file:
print({e.split(" ")[1]:e.split(" ")[0] for e in file})
你可以使用字典理解
您可以使用字典理解功能您可以逐行读取文件,在空白处拆分,并反向使用元素创建字典:
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
现在,您的数据将如下所示:
{'trendmar': ['-34G', '+41G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G'],
'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G']}
更新2:如果要对值求和,则首先需要将它们转换为浮点,然后使用常规算术运算获得最终值,因此首先编写一个函数,将SI速记法转换为本机浮点
:
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
现在,您可以在创建数据时用列表
替换浮点
,并对值求和,而不是追加:
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
这将导致数据
如下:
{'trendmar': 7000000000.0, 'ttpsenet': 1400000000.0, 'tyibahco': 3600000000.0,
'uberhelp': 6000000.0, 'umljgate': 300000000.0}
如果要将这些值返回到SI缩短符号中,则必须编写与之相反的函数SI\u to\u float()
,然后使用它转换所有数据
值,即:
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)
这将最终导致数据
包含:
{'trendmar': '7G', 'ttpsenet': '1.4G', 'tyibahco': '3.6G',
'uberhelp': '6M', 'umljgate': '300M'}
您可以逐行读取文件,按空格拆分,并反向使用元素创建字典:
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
现在,您的数据将如下所示:
{'trendmar': ['-34G', '+41G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G'],
'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G']}
更新2:如果要对值求和,则首先需要将它们转换为浮点,然后使用常规算术运算获得最终值,因此首先编写一个函数,将SI速记法转换为本机浮点
:
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
现在,您可以在创建数据时用列表
替换浮点
,并对值求和,而不是追加:
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
这将导致数据
如下:
{'trendmar': 7000000000.0, 'ttpsenet': 1400000000.0, 'tyibahco': 3600000000.0,
'uberhelp': 6000000.0, 'umljgate': 300000000.0}
如果要将这些值返回到SI缩短符号中,则必须编写与之相反的函数SI\u to\u float()
,然后使用它转换所有数据
值,即:
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)
这将最终导致数据
包含:
{'trendmar': '7G', 'ttpsenet': '1.4G', 'tyibahco': '3.6G',
'uberhelp': '6M', 'umljgate': '300M'}
假设您希望更多的值与用于计算的键相关联,这是我的方法:
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']}
['uberhelp : -32M', 'tyibahco : 3.6G', 'ttpsenet : 1.4G', 'umljgate : -18.1G', 'trendmar : -75G']
编辑:
如果您希望查找两个值之间的差异,可以使用ast
模块中的literal\u eval
函数进行查找,如下所示:
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)
输出:
{'trendmar': ['-34G', '+41G'], 'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G']}
['uberhelp : -32M', 'tyibahco : 3.6G', 'ttpsenet : 1.4G', 'umljgate : -18.1G', 'trendmar : -75G']
在上面的例子中,我们从第二个值中减去第一个值。如果您希望相反,则交换第一个
和第二个
的值假设您希望更多的值与键关联以进行计算,这是我的方法:
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']}
['uberhelp : -32M', 'tyibahco : 3.6G', 'ttpsenet : 1.4G', 'umljgate : -18.1G', 'trendmar : -75G']
编辑:
如果您希望查找两个值之间的差异,可以使用ast
模块中的literal\u eval
函数进行查找,如下所示:
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)
输出:
{'trendmar': ['-34G', '+41G'], 'uberhelp': ['-13M', '+19M'], 'umljgate': ['-8.9G', '+9.2G'], 'ttpsenet': ['1.4G'], 'tyibahco': ['3.6G']}
['uberhelp : -32M', 'tyibahco : 3.6G', 'ttpsenet : 1.4G', 'umljgate : -18.1G', 'trendmar : -75G']
在上面的例子中,我们从第二个值中减去第一个值。如果您希望相反,那么交换first
和second
的值,重复键呢?重复键呢?这是更好的解决方案,因为它不依赖于列之间的空格字符数。我如何在一个类似“trendmar”的列表中为同一个键获得两个值:['-34G','+41G']感谢它的工作,但我有其他的要求,我可以做两个值之间的数学运算,让samekey只带值吗difference@KareemElSayed-检查上面的更新2;)感谢它的工作如此之好,如果我想做其他事情,只打印值大于1G的键,我如何做这是更好的解决方案,因为这不依赖于列之间的空格字符数。我如何在一个列表中为同一个键获得两个值,类似于'trendmar':['-34G','+41G']感谢它的工作,但我有其他的要求,我可以做两个值之间的数学运算,让samekey只带值吗difference@KareemElSayed-检查上面的更新2;)谢谢,它的工作非常好,我还有其他事情,如果我想做条件,只打印值大于1G的键,我怎么做这一个对我很好,但我试着显示这个错误索引器:列表索引超出范围你能指出你到底从哪里得到这个错误吗?我用您提供的数据测试了代码。文件的每一行都必须有值和键,否则split()
不起作用。我还有另外一个方法。我可以对同一个键的两个值进行数学运算,以获得两个值之间的值差吗?这两个值之间的值差太大了,所以也能起作用。我现在要做一些测试,我仍然有关于此案例的任何更新。我将与大家分享这一个方法。它对我有好处,但我尝试显示此错误索引器:列表索引超出范围你能指出你在哪里得到这个错误吗?我用您提供的数据测试了代码。文件的每一行都必须有值和键,否则,split()
将不起作用。我还有另外一行,我可以对同一个键的两个值进行数学运算,以获得到值之间的值差吗?hanks的值差如此之大,因此也能起作用。我现在将进行一些测试,我仍然有关于