我有一个有许多元素的字典,我想编写一个函数,可以返回给定索引范围内的元素(将dict视为数组):
get_range(dict, begin, end):
return {a new dict for all the indexes between begin and end}
Run Code Online (Sandbox Code Playgroud)
怎么做?
编辑:我不是要求使用密钥过滤器...例如)
{"a":"b", "c":"d", "e":"f"}
get_range(dict, 0, 1) returns {"a":"b", "c":"d"} (the first 2 elements)
Run Code Online (Sandbox Code Playgroud)
我不关心排序......其实我正在实现服务器端分页...
将被调用函数的数据委托给另一个函数是很简单的:
def test2(a, b):
huh = locals()
print huh
def test(a, b='hoho'):
test2(**locals())
Run Code Online (Sandbox Code Playgroud)
但是,locals()包含self在调用方法时,当尝试在单行中为方法调用执行相同操作时,这会妨碍:
class X(object):
def test2(self, a, b):
huh = locals()
print huh
def test(self, a, b='hoho'):
self.test2(**locals()) # no workie
test2(**locals()) # no workie either
Run Code Online (Sandbox Code Playgroud)