简体   繁体   English

从python字典访问数据

[英]Accessing data from python dictionary

import numpy as np

def unpickle(file):
   import pickle
   with open(file, 'rb') as fo:
   dict = pickle.load(fo, encoding='bytes')
   return dict

 train = []
for j in range (1,6):
train.append(unpickle('/Users/sachalabdullah/Desktop/cifar-10-batches-py/data_batch_'+str(j)))

test = unpickle ("/Users/sachalabdullah/Desktop/cifar-10-batches-py/test_batch”)

I have loaded CIFAR_10 since I am new to python I don't know how to access the data from dictionary. 由于我是python新手,所以我已加载CIFAR_10,我不知道如何从字典访问数据。

There is another thing which is confusing me that I have appended all the five batches of training data in train suppose I have access only the labels and images from data set so if I am accessing it I would be getting data from all five batches or I need to access images and labels separately for every batch ? 还有这是困惑我,我已经附加在训练数据的所有五个批次的另一件事train假设我有机会只有从数据集,所以如果我访问它,我会从所有五个批次或者我会得到的数据标签和图像是否需要为每个批次分别访问图像和标签?

Is there any Matlab equivalent to this, if I wanted to get column 1 and 2 from a matrix I would do A(:, [1,2]) , or there is not? 如果我想从矩阵中获取列1和2,我会做A(:, [1,2]) ,是否有与此等效的Matlab?

First create your dump file. 首先创建您的转储文件。

file = open('filename', 'r')
obj = file.read()
pickle.dump(obj, open('file.pickle', 'wb'))

then 然后

with open(file.pickle, 'rb') as fo:
   dict = pickle.load(fo, encoding='bytes')

To access keys in a dictionary: 要访问字典中的键:

mydict={'mykey':['value1','value2']}

#access mykey from mydict:
mydict['mykey']

to load that data properly into a dict I'd do this: 要将数据正确加载到字典中,我会这样做:

def unpickle(file):
   import pickle
   dict={}
   with open(file, 'rb') as fo:
   dict[fo] = pickle.load(fo, encoding='bytes')
   return dict

#how to access the data in your train[j] dict:
[v for v in train[j].values()]

the python equivalent to get the first two rows of a matrix: 等效于python的前两行的python:

A=np.matrix([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
A[:,[0,1]]

keep in mind that the index starts at 0 for python. 请记住,对于python,索引从0开始。 matlab index starts at 1. matlab索引从1开始。

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

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