Python更新字典,列表为值类型

发布于 2025-02-12 00:54:25 字数 1151 浏览 1 评论 0原文

我正在尝试通过从文件中提取的数据迭代并根据每个数据的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你拒绝所有暧昧 2025-02-19 00:54:25

尝试这样做:

for itm in example_list:
    if itm[0] in my_dict:
        my_dict[itm[0]].append(itm[1])

try doing it like so:

for itm in example_list:
    if itm[0] in my_dict:
        my_dict[itm[0]].append(itm[1])
执笔绘流年 2025-02-19 00:54:25

您的代码正在根据需要工作。为了简化,您已经用空列表实例化了dict:

for i,j in example_list:
    my_dict[i].append(j)
print(my_dict)

输出:

{'sensor': [245], 'frame': [455, 77], 'version': [], 'function': []}

Your code is working as required. To simplify, as you've already instantiated the dict with empty lists:

for i,j in example_list:
    my_dict[i].append(j)
print(my_dict)

Output:

{'sensor': [245], 'frame': [455, 77], 'version': [], 'function': []}
最冷一天 2025-02-19 00:54:25

您要做的是:

for itm in example_list:
    if itm[0] in my_dict.keys(): # have to look if keys match
        my_dict[itm[0]].append(itm[1]) # # directly access the key-value pair

您的问题是您每次运行循环时都会创建一个新列表并将其附加到其中,因此每次都会删除旧数据。

What you want to do is:

for itm in example_list:
    if itm[0] in my_dict.keys(): # have to look if keys match
        my_dict[itm[0]].append(itm[1]) # # directly access the key-value pair

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文