简体   繁体   English

如何循环遍历嵌套在字典中的列表中的索引?

[英]How to loop through indexes from lists nested in a dictionary?

I created the following dictionary below ( mean_task_dict ).我在下面创建了以下字典 ( mean_task_dict )。 This dictionary includes three keys associated with three lists.该词典包括与三个列表关联的三个键。 Each lists includes 48 numeric values.每个列表包括 48 个数值。

mean_task_dict = {
            "Interoception": task_mean_intero,
            "Exteroception": task_mean_extero,
            "Cognitive": task_mean_cognit,
            }

I would like to plot the values contained in each list inside a scatterplot where the x-axis comprises three categories ( ROI_positions = np.array([1, 2, 3]) ).我想要 plot 散点图中每个列表中包含的值,其中 x 轴包含三个类别( ROI_positions = np.array([1, 2, 3]) )。 Each of the respective lists in the dictionary has to be linked to one of the categories from ROI_positions above.字典中的每个相应列表都必须链接到上面ROI_positions中的类别之一。

Here is my current attempt or code for this task:这是我当前对此任务的尝试或代码:

import numpy as np
import matplotlib.pyplot as plt

task_mean_intero = [-0.28282956438352846, -0.33826908282117457, -0.23669673649758388]
task_mean_extero = [-0.3306686353702893, -0.4675910056474869, -0.2708033871055369]
task_mean_cognit = [-0.3053766849270014, -0.41698707094527254, -0.35655464189810543]

mean_task_dict = {
    "Interoception": task_mean_intero,
    "Exteroception": task_mean_extero,
    "Cognitive": task_mean_cognit,
    }

for value in mean_task_dict.values():
    ROI_positions = np.array([1, 2, 3])
    
    data_ROIs = np.array([
                         mean_task_dict["Interoception"][1],
                         mean_task_dict["Exteroception"][1],
                         mean_task_dict["Cognitive"][1]
                         ])
    
    plt.scatter(ROI_positions, data_ROIs)

My problem is that I am only able to compute and plot the data for one value by paradigmatically selecting the second index value of each list [1] .我的问题是,我只能通过范式选择每个列表[1]的第二个索引值来计算和 plot 一个值的数据。

How can I loop through all values inside the three lists nested in the dictionary, so that I can plot them all together in one plot?我如何遍历嵌套在字典中的三个列表中的所有值,以便我可以 plot 将它们全部放在一个 plot 中?

Do you want something like this?你想要这样的东西吗?

ROI_positions = np.array([1, 2, 3])
for i in range(len(mean_task_dict)):
    data_ROIs = np.array([
                         mean_task_dict["Interoception"][i],
                         mean_task_dict["Exteroception"][i],
                         mean_task_dict["Cognitive"][i]
                         ])
    plt.scatter(ROI_positions, data_ROIs)
plt.show()

在此处输入图像描述

To be independent of dict size, you could do this,为了独立于 dict 大小,你可以这样做,

ROI_positions = np.arange(len(mean_task_dict))
data_ROIs = np.array(list(zip(*mean_task_dict.values())))
for i in range(len(mean_task_dict)):
    plt.scatter(ROI_positions, data_ROIs[i])
plt.show()

Use a nested loop that iterates over each element of the dictionary and then iterates over each list item of that dictionary element.使用嵌套循环迭代字典的每个元素,然后迭代该字典元素的每个列表项。

for value in mean_task_dict.values():
    for item in value:
        #do stuff here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM