如何合并两个列表并将结果存储在字典(Python)中?

2024-06-02 06:32:19 发布

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

我正试着把两个单子合并起来,放进字典里。这是我目前的代码:

    from string import*

    book_ratings=open("ratings.txt","r")
    usernames=[]
    ratings=[]
    for i in book_ratings.readlines(): #i=index
        i=split(i) #splits usernames from ratings
        rating=i[:] #allows whole ratings list to be stored in ratings 
        ratings.append(rating) #appends rating to ratings list
        usernames.append(i) #appends usernames to usernames list

    books=open("books.txt","r")
    book_list=[]
    i=books.readlines()
    book_list.append(i) #appends books to book_list list

    user_book_ratings={}
    for ratings in book_ratings:
        user_book_ratings.join(book_list,ratings)

不幸的是,最后三行只打印出一个空列表。如何将收视率和图书列表组合起来,并将它们放在字典中,以用户名作为键,以新列表作为值?你知道吗

先谢谢你。你知道吗

*编辑

文件评级.txt显示如下:

"Ben 5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 Moose 5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0..." (There are more users, these are the first two)

使用上述代码打印时,显示为:

"[['Ben'], ['5', '0', '0', '0', '0', '0', '0', '1', '0', '1', '-3', '5', '0', '0', '0', '5', '5', '0', '0', '0', '0', '5', '0', '0', '0', '0', '0', '0', '0', '0', '1', '3', '0', '1', '0', '-5', '0', '0', '5', '5', '0', '5', '5', '5', '0', '5', '5', '0', '0', '0', '5', '5', '5', '5', '-5'], ['Moose'], ['5', '5', '0', '0', '0', '0', '3', '0', '0', '1', '0', '5', '3', '0', '5', '0', '3', '3', '5', '0', '0', '0', '0', '0', '5', '0', '0', '0', '0', '0', '3', '5', '0', '0', '0', '0', '0', '5', '-3', '0', '0', '0', '5', '0', '0', '0', '0', '0', '0', '5', '5', '0', '3', '0', '0']..."

文件书籍.txt显示如下:

Douglas Adams,The Hitchhiker's Guide To The Galaxy

Richard Adams,Watership Down

Mitch Albom,The Five People You Meet in Heaven

Laurie Halse Anderson,Speak

Maya Angelou,I Know Why the Caged Bird Sings

... (There are more books; these are the first five)

使用上述代码打印时,看起来像:

"[["Douglas Adams,The Hitchhiker's Guide To The Galaxy\n", 'Richard Adams,Watership Down\n', 'Mitch Albom,The Five People You Meet in Heaven\n', 'Laurie Halse Anderson,Speak\n', 'Maya Angelou,I Know Why the Caged Bird Sings\n'..."

最终,词典应显示为:

{Ben:["Douglas Adams,The Hitchhiker's Guide to The Galaxy","5"],["Richard Adams,Watership Down","0"]... Moose:["Douglas Adams, The Hitchhiker's Guide to the Galaxy","5"]...} and so on (or something similar to that).

其思想是,每本书都由相关用户指定一个等级。你知道吗

我希望我把事情说清楚了。你知道吗


Tags: thetointxtbooksarelistguide
2条回答

下面是如何组合两个列表:

def combine(listone, listtwo):
    out = {}
    for k in range(0, len(listone)):
        out[listone[k]] = listtwo[k]
    return out

其运行方式如下:

>>> combine(['hello', 'is', 'a', 'word'], [5, 2, 1, 4])
{'a': 1, 'is': 2, 'word': 4, 'hello': 5}

顺便说一句,这些列表基本上得到了每个字符串的长度

首先得到你的书名,因为你想在读用户名的时候知道这些,然后你就可以把所有的数据直接放到最后的目录中,中间的列表就更少了。不需要附加任何内容,因为readlines已经提供了一个列表-所以,只需执行以下操作:

with open('books.txt', 'r') as books:
    book_titles = books.readlines()

使用with语句可以确保在读取完文件后立即将其关闭。你知道吗

现在,你有了这些,你得到用户名和评分的方式基本上和你的一样,尽管它可以简化一点:

with open('ratings.txt', 'r') as ratings:
   for line in ratings:
      username, *ratings = line.split()

这个语法告诉Python拆分字符串并将第一个(最左边的)组件分配给username(作为字符串),并将其余的分组到一个列表中并放入分级。你知道吗

你想要的字典(你需要在这个循环之前的某个时候将它初始化为一个空的dict)有用户名作为键,值作为来自分级和书名的对应对的列表。Python有一个名为zip的内置项,它可以为您获取这些对-尽管它没有在Python 3中提供对的列表,所以您需要通过list传递结果:

ratings_dict = {}
with open('ratings.txt', 'r') as ratings:
   for line in ratings:
      username, *ratings = line.split()
      ratings_dict[username] = list(zip(book_titles, ratings))

相关问题 更多 >