切片表示法作为值? [英] slice notation as values?

查看:92
本文介绍了切片表示法作为值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在切片是python中的对象,我想知道切片

符号将来是否可以在订阅之外使用。


是否有可能写一些类似的东西:


a = 4:9

表示关键,tree.items中的值(''alfa。'':''beta。' '):


-

Antoon Pardon

Now slices are objects in python, I was wondering if slice
notation will be usable outside subscribtion in the future.

Will it ever be possible to write things like:

a = 4:9
for key, value in tree.items(''alfa.'': ''beta.''):

--
Antoon Pardon

推荐答案

Antoon Pardon写道:
Antoon Pardon wrote:
现在切片是python中的对象,我想知道切片
符号将来是否可以在订阅之外使用。

是否有可能写下这样的东西:

a = 4:9
for key,value in tree.items(''alfa。'':''beta。''):
Now slices are objects in python, I was wondering if slice
notation will be usable outside subscribtion in the future.

Will it ever be possible to write things like:

a = 4:9
for key, value in tree.items(''alfa.'': ''beta.''):



你的意思是


for key,value in tree.items()[''alfa。'':''beta。'']:


这是什么意思?


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon /


Do you mean

for key, value in tree.items()[''alfa.'': ''beta.'']:

What would this mean?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


Op 2005-12-09,Steve Holden schreef< st *** @ holdenweb.com>:
Op 2005-12-09, Steve Holden schreef <st***@holdenweb.com>:
Antoon Pardon写道:
Antoon Pardon wrote:
现在切片是python中的对象,我想知道切片
符号将来是否可以在订阅之外使用。

是否有可能写出类似的东西:

a = 4:9
用于key.items中的键值(''alfa。'':''beta。'') :
Now slices are objects in python, I was wondering if slice
notation will be usable outside subscribtion in the future.

Will it ever be possible to write things like:

a = 4:9
for key, value in tree.items(''alfa.'': ''beta.''):


对于key.items()[''alfa。'':''beta。'']中的键值,你的意思是


Do you mean

for key, value in tree.items()[''alfa.'': ''beta.'']:




不,切片是方法的参数。

这将是一个更方便的写作方式:


for key,value in tree.items(slice(''alfa。'',''beta。''))


-

Antoon Pardon



No, the slice is meant to be a parameter to the method.
It would be a more convenient way to write:

for key, value in tree.items(slice(''alfa.'', ''beta.''))

--
Antoon Pardon


Antoon Pardon写道:
Antoon Pardon wrote:
Will它有可能写出类似的东西:

a = 4:9
用于键,树中的值。项目(''alfa。'':''beta。''):
Will it ever be possible to write things like:

a = 4:9
for key, value in tree.items(''alfa.'': ''beta.''):




第一个工作正常,除了你需要使用正确的语法:



The first of these works fine, except you need to use the correct syntax:

a = slice(4,9)
范围(10)[a]
[4,5,6,7,8]


第二个也工作正常,提供树是一种支持它的类型和

你将该调用重写为tree.items(slice(''alfa'',''beta。 ''))或者也许

tree [''alfa'':''beta。'']。items()。为了支持直接在字典上切片

你可以这样做:

class sliceable(dict):
def __getitem __(self,item):

if isinstance(item,slice):

返回self .__ class __((k,v)for(k,v)in self.iteritems()

if item.start< = k< item.stop)

返回dict .__ getitem __(self,item)

d = sliceable({''alpha'':1, ''aaa'':2,''beta'':3,''bee'':4})
d [''alpha'':''beta'']
{'' alpha'':1,''bee'':4} d [''alpha'':''beta。'']
{''alpha'':1,''beta'':3, ''bee'':4}表示密钥,值为d [''alpha'':''beta。'']。items():
a = slice(4,9)
range(10)[a] [4, 5, 6, 7, 8]
The second also works fine, provide tree is a type which supports it and
you rewrite the call as tree.items(slice(''alfa'',''beta.'')) or perhaps
tree[''alfa'':''beta.''].items(). To support slicing directly on a dictionary
you could do:
class sliceable(dict): def __getitem__(self, item):
if isinstance(item, slice):
return self.__class__((k,v) for (k,v) in self.iteritems()
if item.start <= k < item.stop)
return dict.__getitem__(self, item)
d = sliceable({''alpha'': 1, ''aaa'': 2, ''beta'': 3, ''bee'': 4 })
d[''alpha'':''beta''] {''alpha'': 1, ''bee'': 4} d[''alpha'':''beta.''] {''alpha'': 1, ''beta'': 3, ''bee'': 4} for key, value in d[''alpha'':''beta.''].items():



打印键,价值

alpha 1

beta 3

bee 4


It这似乎不太可能使它成为内置的dict类型,但是你永远不知道的是


print key, value
alpha 1
beta 3
bee 4

It seems unlikely that this will make it into the builtin dict type, but
you never know.


这篇关于切片表示法作为值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆