[英]Trouble translating input into a dictionary
I am having trouble converting input from a file into a dictionary structure. 我在将输入从文件转换为字典结构时遇到麻烦。
The input from the file has the format: questionNo user_id response
来自文件的输入格式为: questionNo user_id response
questionNo
is the number of the question for which there is a response to. questionNo
是要响应的问题的编号。 user_id
uniquely identifies the person who made the response. user_id
唯一标识做出响应的人。 response
is the answer the user entered in response to the question. response
是用户为回答问题而输入的答案。
and in the file looks something like this: 在文件中看起来像这样:
1 67 1
1 109 1
1 23 2
1 24 1
1 67 3
1 23 5
2 23 3
3 22 4
What I am trying to do is translate this data into a structure like this: 我想做的就是将这些数据转换成这样的结构:
{user_id:{ questionNo:response, questionNo:response,.......},user_id{...}...}
with each questionNo and user_id being unique 每个问题的编号和user_id都是唯一的
My problem is that I have been totally unsuccessful in implementing this. 我的问题是我在实现这一点上一直没有成功。 It is like I have hit a mental roadbloack. 就像我遇到了精神上的路障。 I am not asking any of you to give me a coded solution, just some hints or tips that would help me solve this. 我不是要你们给我编码解决方案,只是一些提示或技巧可以帮助我解决这个问题。
Thanks. 谢谢。
I'm assuming you have read your entries from the file into an entries
list, to make things simpler. 我假设您已将文件中的entries
读入entries
列表,以简化操作。 I'll use then defaultdict
to save us some logic: 我将使用defaultdict
为我们节省一些逻辑:
from collections import defaultdict
dct = defaultdict(dict)
for questionNo, user_id, response in entries:
dct[user_id][questionNo] = response
To create a dictionary you would start 要创建字典,您将开始
mydict = {}
When adding a response for a particular user, you would say 为特定用户添加回复时,您会说
mydict[user_id] = newvalue
If user_id has not yet been put into the dictionary, you would test by 如果尚未将user_id放入字典中,则可以通过
if user_id not in mydict:
mydict[user_id] = empty value # in your example it would either be [] or {}
mydict[user_id] = newvalue # This gets done under any circumstances
If the values are themselves dictionaries keyed on the question #, you would add the newvalue in a similar way to make every question have only the latest answer. 如果这些值本身是在问题#上键入的字典,则可以类似的方式添加新值,以使每个问题仅具有最新答案。 If the question/answer are elements of a list pair, you would append a new question/answer entry and replace the answer in a question that is already in the list. 如果问题/答案是列表对的元素,则应添加新的问题/答案条目,并替换列表中已有问题的答案。
I see that you have your question/answer pairs as being in a dictionary, but I included a list reference for completeness. 我看到您的问题/答案对在字典中,但是为了完整起见,我提供了一个列表参考。
I have to go now but this should give you a start. 我现在必须走了,但这应该给您一个开始。
+1 for Ricardo's answer it seems the best solution although I wanted to give you my solution as well which I think looks a bit simpler ( and probably less efficient) 里卡多(Ricardo)的答案+1似乎是最好的解决方案,尽管我也想给您我的解决方案,我认为它看起来更简单(可能效率更低)
#----- Open file -----
f = open("file.extension","r")
#---- initialize dicts ----
dictionary = {}
#---- read first line -----#
line = f.readline()
#---- while line is not empty ----#
while line != "":
#----- split the line -----#
splitLine = line.split()
#----- Get the 3 strings your need -----#
questionNo = splitLine[0]
user_id = splitLine[1]
response = splitLine[2]
#------ Check if user_id is not registered -----#
if user_id not in dictionary:
#------- Create the new entry -----#
dictionary[user_id] = {questionNo:response}
else:
#------- Add the next questionNo along with the response -----#
dictionary[user_id][questionNo] = response
#----- read new line ------#
line = f.readline()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.