python 自定义数据结构_自定义索引Python数据结构(Custom Indexing Python Data Structure)

自定义索引Python数据结构(Custom Indexing Python Data Structure)

我有一个包含python deque的类来自collections 。 当我去创建一个deque x=deque() ,我想引用第一个变量....

In[78]: x[0]

Out[78]: 0

我的问题是如何在下面的示例包装器中使用[]进行引用

class deque_wrapper:

def __init__(self):

self.data_structure = deque()

def newCustomAddon(x):

return len(self.data_structure)

def __repr__(self):

return repr(self.data_structure)

即,继续上面的例子:

In[75]: x[0]

Out[76]: TypeError: 'deque_wrapper' object does not support indexing

我想自定义我自己的引用,这可能吗?

I have a class that wraps around python deque from collections. When I go and create a deque x=deque(), and I want to reference the first variable....

In[78]: x[0]

Out[78]: 0

My question is how can use the [] for referencing in the following example wrapper

class deque_wrapper:

def __init__(self):

self.data_structure = deque()

def newCustomAddon(x):

return len(self.data_structure)

def __repr__(self):

return repr(self.data_structure)

Ie, continuing from above example:

In[75]: x[0]

Out[76]: TypeError: 'deque_wrapper' object does not support indexing

I want to customize my own referencing, is that possible?

原文:https://stackoverflow.com/questions/25716808

2020-02-28 23:08

满意答案

class DequeWrapper:

def __init__(self):

self.data_structure = deque()

def newCustomAddon(x):

return len(self.data_structure)

def __repr__(self):

return repr(self.data_structure)

def __getitem__(self, index):

# etc

每当你执行my_obj[x] ,Python实际上会调用my_obj.__getitem__(x) 。

您可能还需要考虑实现__setitem__方法 (如果适用)。 (当你写my_obj[x] = y ,Python实际上会运行my_obj.__setitem__(x, y) 。

有关Python数据模型的文档将包含有关在Python中创建自定义数据结构时需要实现哪些方法的更多信息。

You want to implement the __getitem__ method:

class DequeWrapper:

def __init__(self):

self.data_structure = deque()

def newCustomAddon(x):

return len(self.data_structure)

def __repr__(self):

return repr(self.data_structure)

def __getitem__(self, index):

# etc

Whenever you do my_obj[x], Python will actually call my_obj.__getitem__(x).

You may also want to consider implementing the __setitem__ method, if applicable. (When you write my_obj[x] = y, Python will actually run my_obj.__setitem__(x, y).

The documentation on Python data models will contain more information on which methods you need to implement in order to make custom data structures in Python.

2014-09-08

相关问答

根据MDN,splice确实支持负索引。 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice 开始更改数组的索引(原点为0)。 如果大于数组的长度,实际的起始索引将设置为数组的长度。 如果为负数,将从数组末尾开始那么多元素(带原点1)。 所以,这就是删除作品的原因。 要在更新中启用否定索引,您可以检查提供的参数是否为负数。 如果是,则使用array.length + i...

列表对象被实现为数组。 它们针对快速的固定长度操作进行了优化,并导致pop(0)和insert(0,v)操作的O(n)内存移动成本,这会改变底层数据表示的大小和位置。 另见: http : //docs.python.org/library/collections.html#collections.deque Btw,有趣的是Python数据结构教程建议使用pop(0)来模拟队列,但不提O(n)或deque选项。 http://docs.python.org/tutorial/datastruct...

这是set 。 也有使用您提到的方法的在线示例 。 下一次你想知道,我建议你只是打印这种类型,如下所示: print(type(explored));

It's a set. There are also online examples that use the methods you mentioned. Next time you wonder, I suggest you just print the type, like this: print(type(explored));

您需要实现__getitem__ 。 请注意, 单个索引将作为自身传递 ,而多个索引将作为元组传递 。 通常,您可以选择以下列方式处理此问题: class indexed_array:

def __getitem__(self, indices):

# convert a simple index x[y] to a tuple for consistency

if not isinstance(indices, tuple):

in...

在上面的文章中,索引和访问是相同的,它们都描述了在元素上访问所需的渐近复杂性。 进一步来说: 在数组中,当谈论访问例如第3个元素时,索引或访问当然是O(1),并且由于在最坏情况下需要线性传递,搜索是O(n)。 在Hashtables中,很明显我们不能参考索引,例如什么是第三元素 - 这个问题没有意义。 第一篇文章说N / A考虑到上述通知。 第二篇文章考虑了Hashtable - 索引是要找到一个元素(你知道元素的哈希键)。 如果你有一个好的哈希表(好的长度和最重要的好哈希函数),那么找到一个元素...

您可以稍微修改一下您的想法:保留一个字典将源与(键,值)对关联,另一个关联键与值集关联。 这应该是快速构建/更新(添加一个条目需要两个dict查找和一个列表/集插入),并且不需要太多的内存开销。 然后,您想要的两个查找操作中的每一个只需要一个字典命中。 请注意,这只会使指向实际数据的指针数量增加一倍; 如果值很大,那么内存使用量将远远小于两倍。 但是,如果这是一个问题,并且您不介意使键值查找的源ID慢得多,那么您只能存储从键到(源,值)对的字典。 然后,您可以获取给定键的所有值 vals_for_...

当您使用切片索引对象时,Python会使用您提供的输入创建slice对象。 例如,如果您执行c[0.2:0.4] ,则传递给c.__getitem__的参数将为slice(0.2, 0.4) c.__getitem__ slice(0.2, 0.4) 。 所以你可以在你的__getitem__方法中有这样的代码: def __getitem__(self, x):

if isinstance(x, slice):

start = x.start

stop =...

df = pd.DataFrame(np.arange(27).reshape(3,9) ,

columns = [

['x' , 'y' , 'z' , 'Group1' , 'Group1' , 'Group1' , 'Group2' , 'Group2' , 'Group2'] ,

[ 'x' , 'y' , 'z' , 'prop1' , 'prop2' , 'prop3' , 'prop1' , 'prop2' , 'prop3']

]

)

...

这是一个完整的(我认为)基于1的列表的实现,正确处理切片(包括扩展切片),索引,弹出等等。比你想象的要好一些,特别是切片和负索引,这有点棘手。 事实上,我仍然不能100%确定它的工作原理应该如此,因此需要注意。 class list1(list):

"""One-based version of list."""

def _zerobased(self, i):

if type(i) is slice:

return slice(self....

您想要实现__getitem__方法 : class DequeWrapper:

def __init__(self):

self.data_structure = deque()

def newCustomAddon(x):

return len(self.data_structure)

def __repr__(self):

return repr(self.data_structure)

def __get...

相关文章

Data Week: Becoming a data scientist Data Pointed,

...

Index Data Author: David Smiley Eric Pugh 译者:Koala+

...

清明假期翻以前的笔记发现有一些NoSQL相关的内容,比较零散,是之前读《Big Data Glossa

...

mod_python: the long story - Grisha Trubetskoy

...

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重

...

Mapping entities to the index structure 4.1. 映射一个实

...

Hi Pythonistas! 测试和调试 Testing & Debuggi

...

http://engineering.linkedin.com/distributed-systems

...

Spring Data: a new perspective of data operations

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值