Python更新字典,列表为值类型
我正在尝试通过从文件中提取的数据迭代并根据每个数据的ID存储在字典中,
这些是数据的ID(str):“传感器”,“版本”,“帧”,“ function”
和数据在十六进制字符串中。
我从基地开始的是以ID和数据形式(我从文件中提取的形式)的大量列表
example_list = [("sensor", 245), ("frame", 455), ("frame", 77)] and so on
。 我想将字典用ID作为键和数据列表作为值,因此在example_list进行迭代时,我有特定ID的值列表(因此,我可以通过值列表迭代以获取特定ID的所有数据(键))
要启动,所有值(列表)将以一个空列表开始
my_dict = {"sensor": [], "frame": [], "version": [], "function": []}
,然后,当我通过示例_list迭代,如果ID在my_dict中为键,
for itm in example_list:
if itm[0] in my_dict:
tmp = my_dict[itm[0]] # since itm[0] is the id
tmp.append(itm[1])
my_dict[itm[0]] = tmp # update the list
我在尝试尝试时 将值附加到my_dict中的值列表这,似乎最终的my_dict的值列表具有最后数据的值 我的意思是,如果
example_list = [("sensor", 245), ("frame", 455), ("frame", 77)]
那时
my_dict = {"sensor": [245], "frame": [77], "version": [], "function": []}
我可能对此解释有错(因为我正在阅读的数据确实很大),但是当我在功能末尾打印my_dict时,每个值列表只有一个数据,与我期望的那样(数据列表,而不是一个),
我尝试了搜索,人们使用更新功能来更新字典,但是一个似乎也没有起作用,并给了我某种程度上的错误/警告/警告。
有什么方法可以实施我想做的事情?
I'm trying to iterate through data extracted from a file and store them in a dictionary based on each data's id
These are the id (str) for the data : "sensor", "version", "frame", "function"
And the data are in hexadecimal string.
What I bascially start with is a huge list of tuples in a form of id and data (that i extracted from a file)
example_list = [("sensor", 245), ("frame", 455), ("frame", 77)] and so on
This example_list stores all the data, so it has information of data for all the id.
I want to make a dictionary with id as key and list of data as value so when done iterating through the example_list, I have list of values for specific id (so I can iterate through the value list to get all the data for a specific id (the key))
To start, all values (list) will start with an empty list
my_dict = {"sensor": [], "frame": [], "version": [], "function": []}
Then, as I iterate through example_list, if the id is in my_dict as a key, I append the value to the values list in my_dict
for itm in example_list:
if itm[0] in my_dict:
tmp = my_dict[itm[0]] # since itm[0] is the id
tmp.append(itm[1])
my_dict[itm[0]] = tmp # update the list
When I tried this, it seems like the final my_dict's value list has the value of the lastest data
What I mean by this is if
example_list = [("sensor", 245), ("frame", 455), ("frame", 77)]
then
my_dict = {"sensor": [245], "frame": [77], "version": [], "function": []}
I may be wrong about this interpretation (since the data I'm reading is really big), but when I printed my_dict in the end of function, each value list had only one data inside, which is far off from what I expected (list of data instead of just one)
I tried searching and people used update function to update the dictionary but that one also didn't seem to work and gave me somehting unhashable error/warning.
Any way to implement what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样做:
try doing it like so:
您的代码正在根据需要工作。为了简化,您已经用空列表实例化了dict:
输出:
Your code is working as required. To simplify, as you've already instantiated the dict with empty lists:
Output:
您要做的是:
您的问题是您每次运行循环时都会创建一个新列表并将其附加到其中,因此每次都会删除旧数据。
What you want to do is:
Your problem was that you created a new list and appended your item to it each time the loop was run, therefore the old data was deleted everytime.