Python 从文件中的每一行制作词典

Python 从文件中的每一行制作词典,python,dictionary,key-value,Python,Dictionary,Key Value,我试图从这个文件中创建一个字典:关键字是第一个单词,值是后面的所有单词 andrew fred fred judy andrew fred george judy andrew john george 这是我的代码: follows_file = open("C:\\Users\\Desktop\\Python\\follows.txt") followers = {} for line in follows_file: #==> [Judy Andrew Fred] use

我试图从这个文件中创建一个字典:关键字是第一个单词,值是后面的所有单词

andrew fred
fred
judy andrew fred
george judy andrew
john george
这是我的代码:

follows_file = open("C:\\Users\\Desktop\\Python\\follows.txt")
followers = {}
for line in follows_file:   #==> [Judy Andrew Fred]
    users = line.split(' ')     #==> [Judy, andrew, Fred, ....]
    follower = users[0]     #==> [Judy]
    followed_by = users[1:] #==> [Andrew, Fred]

    for user in followed_by:
        # Add the 'follower to the list of followers user
        if user not in followers:
            followers[user] = []
        followers[user].append(follower)
print(followers.items())
当我打印follower并后跟variable时,它们是正确的,但是我无法将它们正确地添加到字典中;这就是输出

dict_items([('fred\n', ['andrew', 'judy']), ('andrew', ['judy']), ('judy' ['george']), ('andrew\n', ['george']), ('george', ['john'])])
我期望的输出是

(Andrew[Fred])(Fred[])(judy[Andrew Fred])(George[Judy Fred])(john[george])
非常感谢您的帮助

您可以将其用作字典工厂,只需将用户添加到某人之后,例如:

import collections

followers = collections.defaultdict(list)  # use a dict factory to save some time on checks
with open("path/to/your_file", "r") as f:  # open the file for reading
    for line in f:  # read the file line by line
        users = line.split()  # split on any white space
        followers[users[0]] += users[1:]  # append the followers for the current user
这将为您的数据生成:

{'andrew': ['fred'],
 'fred': [],
 'judy': ['andrew', 'fred'],
 'george': ['judy', 'andrew'],
 'john': ['george']}
这也将允许您在重复记录中向用户追加多个列表-否则您可以对
追随者使用普通的
dict
,并将其设置为
followers[users[0]]=users[1://code>

显示为所需输出的数据结构不是有效的Python,您真的希望它以这种方式显示吗?我的意思是,如果你坚持你可以这样做:

print("".join("({}[{}])".format(k, " ".join(v)) for k, v in followers.items()))
# (andrew[fred])(fred[])(judy[andrew fred])(george[judy andrew])(john[george])

经过编辑的答案,由于@PM2Ring和@IljaEverilä的评论而有所改进。

这是我使用字典理解的原始解决方案

followers = {line.split()[0]: line.split()[1:] for line in follows_file}
@IljaEverilä提出的一个更有效的替代方案是:

followers = {follower: followees for follower, *followees in map(str.split, follows_file)}
结果:

{'andrew': ['fred'],
 'fred': [],
 'george': ['judy', 'andrew'],
 'john': ['george'],
 'judy': ['andrew', 'fred']}
请注意,上述两种解决方案都假定您的文件不包含重复的密钥

以后不要忘记关闭您的文件:

follows_file.close()
或者更好,只需使用上下文管理器,即可为您处理文件关闭:

with open('C:\\Users\\zacan\\Desktop\\Python\\follows.txt', 'r') as follows_file:
    followers = {follower: followees for follower, *followees in map(str.split, follows_file)}

这应该行得通,但没有测试它

这是一个使用
str.split
try
/
的解决方案,除了
子句捕获只存在密钥的实例

注意:我们可以像读取文件一样读取字符串

from io import StringIO
import csv

mystr = StringIO("""andrew fred
fred
judy andrew fred
george judy andrew
john george""")

# replace mystr with open("C:\\Users\\zacan\\Desktop\\Python\\follows.txt")
with mystr as follows_file:
    d = {}
    for users in csv.reader(follows_file):
        try:
            key, *value = users[0].split()
        except ValueError:
            key, value = users[0], []

        d[key] = value

print(d)

{'andrew': ['fred'],
 'fred': [],
 'george': ['judy', 'andrew'],
 'john': ['george'],
 'judy': ['andrew', 'fred']}

使用
line.split()
去除所有空白,包括换行符。至少你忘记了去掉换行符,所以你有
('andrew',['judy'])
('andrew\n',['george'])
。。如果你继续这样做,将有自动版主标志。请回滚。在设置用户关注者的值时,绝对没有理由进行列表理解。另外,
line.split(“”)
将生成添加了
\n
的最后一个用户,或者注册一个新的后续用户,并将
\n
作为其最后一个字符。为什么要对传递给
.split()
的字符串调用
.strip()
?为什么要在每条线路上执行两次如此昂贵的操作?对
循环使用适当的
,这样你就不需要这样做了。
{follower:followeres for followers,*followeres in map(str.split,f)}
@PM2Ring-我没有意识到
split()
处理了每行的尾部
\n
,将编辑
strip()
调用我的答案@IljaEverilä,很好,我在想一种不用调用
split()
两次就能理解的方法,但我没有想到。
from io import StringIO
import csv

mystr = StringIO("""andrew fred
fred
judy andrew fred
george judy andrew
john george""")

# replace mystr with open("C:\\Users\\zacan\\Desktop\\Python\\follows.txt")
with mystr as follows_file:
    d = {}
    for users in csv.reader(follows_file):
        try:
            key, *value = users[0].split()
        except ValueError:
            key, value = users[0], []

        d[key] = value

print(d)

{'andrew': ['fred'],
 'fred': [],
 'george': ['judy', 'andrew'],
 'john': ['george'],
 'judy': ['andrew', 'fred']}