简体   繁体   English

行分割后要添加字典键和值吗?

[英]Adding dictionary keys and values after line split?

If I have for instance the file: 例如,如果我有文件:

;;;
;;;
;;;
A  1 2 3
B  2 3 4
C  3 4 5

And I want to read it into a dictionary of {str: list of str} : 我想将其读入{str:str的列表}的字典中:

{'A': ['1', '2', '3'], 'B': ['2', '3', '4'], 'C': ['3', '4', '5']

I have the following code: 我有以下代码:

d = {}
with open('file_name') as f:
    for line in f:
        while ';;;' not in line:
            (key, val) = line.split('  ')
        #missingcodehere    
return d

What should I put in after the line.split to assign the keys and values as a str and list of str? 我应该在line.split之后输入什么,以将键和值分配为str和str列表?

To focus on your code and what you are doing wrong. 专注于您的代码和您在做什么错。

You are pretty much in an infinite loop with your while ';;;' not in line 您的while ';;;' not in line几乎处于无限循环中while ';;;' not in line while ';;;' not in line . while ';;;' not in line So, you want to change your logic with how you are trying to insert data in to your dictionary. 因此,您想更改尝试将数据插入字典的方式的逻辑。 Simply use a conditional statement to check if ';;;' 只需使用条件语句来检查if ';;;' is in your line. 在你的行中。

Then, when you get your key and value from your line.strip().split(' ') you simply just assign it to your dictionary as d[key] = val . 然后,当您从line.strip().split(' ')获取键和值时,只需将其分配为d[key] = val However, you want a list, and val is currently a string at this point, so call split on val as well. 但是,您需要一个列表,并且val目前是一个字符串,因此在val上也调用split。

Furthermore, you do not need to have parentheses around key and val . 此外,你不需要有大约括号key and val It provides unneeded noise to your code. 它为您的代码提供了不必要的噪音

The end result will give you: 最终结果将为您提供:

d = {}
with open('new_file.txt') as f:
    for line in f:
        if ';;;' not in line:
            key, val = line.strip().split('  ')
            d[key] = val.split()

print(d)

Using your sample input, output is: 使用样本输入,输出为:

{'C': ['3', '4', '5'], 'A': ['1', '2', '3'], 'B': ['2', '3', '4']}

Finally, to provide an improvement to the implementation as it can be made more Pythonic . 最后,对实现进行改进,使其可以使用更多Pythonic We can simplify this code and provide a small improvement to split more generically, rather than counting explicit spaces: 我们可以简化此代码,并提供较小的改进以更通用地进行拆分,而不是计算显式空格:

with open('new_file.txt') as fin:
    valid = (line.split(None, 1) for line in fin if ';;;' not in line)
    d = {k:v.split() for k, v in valid}

So, above, you will notice our split looks like this: split(None, 1) . 因此,在上面,您将注意到我们的拆分看起来像这样: split(None, 1) Where we are providing a maxsplit=1 . 我们在哪里提供maxsplit=1

Per the docstring of split , it explains it pretty well: 根据split的文档字符串,它可以很好地解释它:

Return a list of the words in S, using sep as the delimiter string. 使用sep作为分隔符字符串,返回S中单词的列表。 If maxsplit is given, at most maxsplit splits are done. 如果给出了maxsplit,则最多完成maxsplit分割。 If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. 如果未指定sep或为None,则任何空格字符串都是分隔符,并且从结果中删除空字符串。

Finally, we simply use a dictionary comprehension to obtain our final result. 最后,我们仅使用字典理解即可获得最终结果。

Why not simply: 为什么不简单:

def make_dict(f_name):
    with open(f_name) as f:
        d = {k: v.split() 
             for k, v in [line.strip().split('  ') 
                          for line in f 
                          if ';;;' not in line]}

    return d

Then 然后

>>> print(make_dict('file_name'))
{'A': ['1', '2', '3'], 'B': ['2', '3', '4'], 'C': ['3', '4', '5']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM