简体   繁体   English

访问元组字典中所有元素的相同值

[英]Accessing same value for all elements in dictionary of tuples

I have a dictionary containing keys all with a 7-tuple as their values.我有一个字典,其中包含所有以 7 元组作为值的键。 For every 7-tuple, each of the 7 values represents a different property corresponding to the data set that key is representing.对于每个 7 元组,7 个值中的每一个都代表与该键所代表的数据集相对应的不同属性。 For example, the first element of each tuple is an array of that data sets raw data, the second value is the mean of the data, the third value is the error on that mean, the fourth value is the temperature at which that data set was taken etc. I would like to plot various different combinations of these values, for example, the mean value vs the temperature.例如,每个元组的第一个元素是该数据集原始数据的数组,第二个值是数据的平均值,第三个值是该平均值的误差,第四个值是该数据集的温度被采取等。我想绘制这些值的各种不同组合,例如,平均值与温度。

So I need to find a way to convert the dictionary values into different lists containing values all of the same property ie a list of all of the mean values and a list of all of the temperatures.所以我需要找到一种方法将字典值转换为包含所有相同属性值的不同列表,即所有平均值的列表和所有温度的列表。 I understand that dictionaries are inherently unordered which complicates matters as each value in the lists would need to have the same index as its corresponding value in the other list.我知道字典本质上是无序的,这使问题复杂化,因为列表中的每个值都需要与其在另一个列表中的对应值具有相同的索引。

dictionary={}
for i in range(len(imagefolders)):
   dictionary[slugify(imagefolders[i])]=images2(r{}\*.png".format(imagefolders[i]))

plotting=dictionary.values()

The above code shows how I inputted the keys and values to the dictionary.上面的代码显示了我如何将键和值输入到字典中。 I know that dictioanry.values() returns a list of all the values in the dictionary however when I tried the above code it didn't even store this as a variable.我知道 dictioanry.values() 返回字典中所有值的列表,但是当我尝试上面的代码时,它甚至没有将其存储为变量。

I am thinking now maybe using a dictionary to store this data wasn't the best option so if anyone has any better ideas of doing that then please do say but a method of making it work with a dictionary would be ideal.我现在在想,也许使用字典来存储这些数据并不是最好的选择,所以如果有人有更好的想法,那么请说出来,但使用字典的方法将是理想的选择。

I'm fairly inexperienced with python so would appreciate any contribution.我对 python 相当缺乏经验,所以将不胜感激任何贡献。

I would recommend you take a look into pandas .我建议你看看pandas It's made for data analysis and has a ton of super useful functions.它专为数据分析而设计,具有大量超级有用的功能。 It might have a bit of a curve to it, but there's pletnty of documentation here on stackoverflow.它可能有一些曲线,但是这里有很多关于 stackoverflow 的文档。

To show you some of the power, I created a mock dict similar to what you described.为了向您展示一些功能,我创建了一个类似于您描述的模拟字典。 I am using a very simple tuple to help demonstrate the position of the elements for later我正在使用一个非常简单的元组来帮助演示元素的位置以备后用

columns = ['data', 'mean', 'error', 'temperature', 'other1', 'other2', 'other3']
d = {}
for i in ['one', 'two', 'three', 'four', 'five']:
    d[i] = (1, 2, 3, 4, 5, 6, 7)

print(d)
# {
#   'one':   (1, 2, 3, 4, 5, 6, 7),
#   'two':   (1, 2, 3, 4, 5, 6, 7),
#   'three': (1, 2, 3, 4, 5, 6, 7),
#   'four':  (1, 2, 3, 4, 5, 6, 7),
#   'five':  (1, 2, 3, 4, 5, 6, 7)
# }

If the structrue above matches the structure of your dict, then we're in good shape.如果上面的结构与您的 dict 结构匹配,那么我们的状态很好。 To put it all into one giant table, all you need are the lines below to make all the magic accessible.要将其全部放入一张巨大的桌子中,您只需要以下几行即可让所有魔法变得触手可及。

import pandas as pd
df = pd.DataFrame.from_dict(d, orient='index', columns=columns)
print(df)
#        data  mean  error  temperature  other1  other2  other3
# three     1     2      3            4       5       6       7
# one       1     2      3            4       5       6       7
# five      1     2      3            4       5       6       7
# two       1     2      3            4       5       6       7
# four      1     2      3            4       5       6       7

To access a given field, you can index it based on the column and then perform statistics on it if you'd like.要访问给定的字段,您可以根据列对其进行索引,然后根据需要对其进行统计。

print(df['data'])
# one      1
# three    1
# four     1
# two      1
# five     1
# Name: data, dtype: int64

print(df['data'].mean())
# 1.0

Pandas also has built in support for matplotlib via the df.plot() function. Pandas 还通过df.plot()函数内置了对matplotlib支持。

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

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