Python学习(1):torch.utils.data.Dataset类

https://blog.csdn.net/qq_36653505/article/details/83351808

PyTorch简介

demo:

class CountList:

def __init__(self, *args):

self.values = [x for x in args]

self.count = {}.fromkeys(range(len(self.values)),0)

# 这里使用列表的下标作为字典的键,注意不能用元素作为字典的键

# 因为列表的不同下标可能有值一样的元素,但字典不能有两个相同的键

def __len__(self):

return len(self.values)

def __getitem__(self, key):

self.count[key] += 1

return self.values[key]

c1 = CountList(1,3,5,7,9)

c2 = CountLIst(2,4,6,8,10)

# 调用

c1[1]  ## 3

c2[1]  ## 4

c1[1] + c2[1] ## 7

c1.count  ## {0:0,1:2,2:0,3:0,4:0}

c2.count  ## {0:0,1:2,2:0,3:0,4:0}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容