Python相当于自定义类的C ++ begin()和end()

时间:2015-11-28 20:33:12

标签: python loops iteration

假设你有一个字典,其键是整数。值也是字典,其键是字符串,其值是numpy数组。 类似的东西:

custom = {1: {'a': np.zeros(10), 'b': np.zeros(100)}, 2:{'c': np.zeros(20), 'd': np.zeros(200)}}

我在代码中一直使用这种自定义数据结构,每次我需要迭代这个结构的numpy数组中的每一行时,我必须这样做:

for d, delem in custom.items():
    for k, v in delem.items():
        for row in v:
            print(row)

是否可以在函数àla C ++中封装此行为,您可以在其中实际实现自定义begin()end()?此外,迭代器还应该包含有关其相应词典中的键的信息。我想象的是:

for it in custom:
    d, e, row = *it
    # then do something with these

4 个答案:

答案 0 :(得分:4)

有多种方法可以做到这一点。 yield可能是最简单的,因为它可以为您构建一个interator课程。

def custom_dict_iter(custom):
    for d, delem in custom.items():
        for k, v in delem.items():
            for row in v:
                yield d, k, row

for d, k, row in custom_dict_iter(my_custom_dict):
    print(d, k, row)

答案 1 :(得分:2)

查找iterator protocol - 这比Java的Iterable或C#的IEnumerable更类似于C ++的开头/结尾。您可以将__iter__方法定义为generator

,从而更轻松地定义它

唯一的问题是,你需要让你的custom拥有自己的类,而不是普通的字典,但我认为在C ++中也是如此。

答案 2 :(得分:1)

import numpy as np

custom = {
    1: {'a': np.zeros(10), 'b': np.zeros(100)}, 
    2:{'c': np.zeros(20), 'd': np.zeros(200)}
}

my_gen = (
    (key, subkey, np_array) 
    for (key, a_dict) in custom.items() 
    for subkey, np_array in a_dict.items() 
)

for key, subkey, np_array in my_gen:
    print(key, subkey, np_array)

--output:--
1 b [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
1 a [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
2 d [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.]
2 c [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.]

或者,您可以将数据结构重构为对您的目的更有用的内容:

import numpy as np

custom = {
    1: {'a': np.zeros(10), 'b': np.zeros(100)}, 
    2:{'c': np.zeros(20), 'd': np.zeros(200)}
}

#Create a *list* of tuples:
converted_data = [
    (np_array, subkey, key)
    for (key, a_dict) in custom.items() 
    for subkey, np_array in a_dict.items() 
]

for np_array, subkey, key in converted_data:
    print(key, subkey, np_array)

创建自定义迭代器:

class Dog:
    def __init__(self, data):
        self.data = data
        self.max = len(data)
        self.index_pointer = 0

    def __next__(self):
        index = self.index_pointer

        if index < self.max:
            current_val = self.data[index]
            self.index_pointer += 1
            return current_val
        else:
            raise StopIteration


class MyIter:
    def __iter__(self):
        return Dog([1, 2, 3])


for i in MyIter():
    print(i)

--output:--
1
2
3

__iter__()只需返回一个实现__next__()方法的对象,因此您可以将这两个类组合起来:

class MyIter:
    def __init__(self, data):
        self.data = data
        self.max = len(data)
        self.index_pointer = 0

    def __iter__(self):
        return self  #I have a __next__() method, so let's return me!

    def __next__(self):
        index = self.index_pointer

        if index < self.max:
            current_val = self.data[index]
            self.index_pointer += 1
            return current_val
        else:
            raise StopIteration

for i in MyIter([1, 2, 3]):
    print(i)

--output:--
1
2
3

更复杂的__next__()方法:

import numpy as np

class CustomIter:
    def __init__(self, data):
        self.data = data
        self.count = 0


    def __iter__(self):
        return self

    def __next__(self):
        count = self.count
        self.count += 1

        if count == 0:  #On first iteration, retun a sum of the keys
            return sum(self.data.keys())

        elif count == 1: #On second iteration, return the subkeys in tuples
            subkeys =  [ 
                a_dict.keys()
                for a_dict in self.data.values()
            ]

            return subkeys

        elif count == 2: #On third iteration, return the count of np arrays
            np_arrays = [
                np_array
                for a_dict in self.data.values()
                for np_array in a_dict.values()
            ]

            return len(np_arrays)

        else:  #Quit after three iterations
            raise StopIteration


custom = {
    1: {'a': np.zeros(10), 'b': np.zeros(100)}, 
    2:{'c': np.zeros(20), 'd': np.zeros(200)}
}

for i in CustomIter(custom):
    print(i)


--output:--
3
[dict_keys(['b', 'a']), dict_keys(['d', 'c'])]
4

答案 3 :(得分:0)

作为一种更加pythonic的方式,您可以使用嵌套列表理解,它在解释器内以C语言速度执行:

>>> [[(i,key,t) for t in value] for i,j in custom.items() for key,value in j.items()]

如果你想获得一个迭代器,你可以使用生成器表达式而不是列表理解。

>>> ([(i,key,t) for t in value] for i,j in custom.items() for key,value in j.items())