python基本语法练习

1 # 创建字典
2 import keras
3 out_put= {x: x**2 for x in (2,4,6)}
4 # print(out_put,type(out_put)) #{2: 4, 4: 16, 6: 36} <class 'dict'>
1 import numpy as np
2 dic  = {x: x**2 for x in range(1,4)}
3 print(dic)  #{1: 1, 2: 4, 3: 9}
4 for k,v in dic.items():# (Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。
5     print(k,v) #1 1   2 4   3 9
6 array = np.arange(1,25)
7 b= array.reshape(1,2,4,3)
1 s = ""
2 a = [1, 2, 3]
3 for i in a:
4     print(type(i)) # <class 'int'>
5     s += str(i)
6 print(type(s))# <class 'str'>
 1 # 创建自己的数据集
 2 from torch.utils.data.dataset import Dataset
 3 import numpy as np
 4 class TxtDataset(Dataset):
 5     def __init__(self):
 6         self.Data =  np.asarray([[1,2],[3,4],[2,1],[6,4],[4,5]])
 7         print(self.Data.shape)
 8         self.target = np.asarray([1,2,0,1,2]) #torch.form_numpy().type(torch.LongTensor)
 9     def __getitem__(self,index):#torch.LongTensor(self.Data[index])
10         txt = self.Data[index]
11         target = self.target[index]
12         return txt,target
13     def __len__(self):
14         return len(self.Data)
15 Txt = TxtDataset()
16 # print(Txt[1])
17 # print(Txt.__len__())
18 test_lodat = torch.utils.data.DataLoader(Txt,batch_size=2,shuffle=True,num_workers=0)
19 for i,traindata in enumerate(test_lodat):
20     print("i",i)
21     Data,Label = traindata
22     print('data',Data)
23     print('Label',Label)

 

posted on 2021-10-31 19:16  GooBo  阅读(139)  评论(0)    收藏  举报