繁体   English   中英

遍历多个数组以执行任务?

[英]Iterate over multiple arrays to perform tasks?

我有9个数组,每个数组包含19个值。

假设它们是a1,a2,a3,a4,a5,a6,a7,a8,a9 (每个a1,a2 ... a9各自包含19个值),我们称它们a数组。

我还有9个数组,每个数组包含19个值。

假设它们是b1,b2,b3,b4,b5,b6,b7,b8,b9 (每个b1,b2 ... b9都包含19个值),我们称它们为b数组。

我现在想取每个 a数组第一个值和每个 b数组第一个值 ,将它们除(a/b) ,这将给我一个新的数组,比如说a/b具有19个值。 然后,我使用numpy.std计算这19个值的标准偏差。

然后,我想再次遍历这些数组,但是这次是每个数组的第二个值,依此类推,直到最后一个(第19个)值,然后执行上述操作。

如果我只有2个数组(例如a1b1 ),则可以使用zip

div_array = [] # The empty array that will have the divided values
for a,b in zip(a1,b1):
    div = a/b
    div_array.append(div)

std = np.std(div_array)

在冗长的情况下,如何重复以上内容?

编辑:

我最终需要19个不同的标准偏差,即我先计算第一个值,然后计算第二个值,依此类推。

如果您将numpy功能用于std为什么不使用numpy功能呢?

>>> # You can create these array in a loop if you want
>>> a = np.array([a1, a2, a3, ..., a9])
>>> b = np.array([b1, b2, b3, ..., b9]) 
>>> c = np.std(a / b, 0)

示例(有关np.std详细信息):

>>> a1 = np.array([1, 2, 3])
>>> a2 = np.array([2, 3, 4])
>>> a  = np.array([a1, a2])
>>> a
array([[1, 2, 3],
       [2, 3, 4]])
>>> b1 = np.array([10, 100, 1000])
>>> b2 = np.array([20, 200, 2000])
>>> b  = np.array([b1, b2])
>>> b
array([[10, 100, 1000], 
       [20, 200, 2000]])
>>> a/b
array([[0.1,  0.02, 0.003],
       [0.1, 0.015, 0.002]])
>>> np.std(a/b)             # The standard deviation of the whole matrix
0.04289... 
>>> np.std(a/b, 0)          # The standard deviation of each column
array([0, 0.0025, 0.0005])
>>> np.std(a/b, 1)          # The standard deviation of each row
array([0.04229263, 0.04345879])

您可以将ab数组分别放在另一个数组中。 原来如此

a[0] = a1a[1] = a2等,为同样的事情b

然后是这样的:

for i in range(length(a)):
    div_array = []
    for j in range(length(a[i])):
        div = a[i][j]/b[i][j]
        div_array.append(div)

    std.append(np.std(div_array))

然后,您将拥有一个包含所有所需值的数组std

当然,如果ab的长度相同,而a[0]b[0]等等的长度也相同,那将起作用。 在您的情况下,这是对的。

所以前一段时间,我写了一个类,基本上可以满足您的要求。 唯一的技巧是,您必须将每个数组的迭代器列表传递给该类。

column_iter_traversal.py

"""
This code will take in a iterator of iterators. You can view the first
iterator as the rows of a graph (a matrix being a specific case of graphs)
and each iterable giving you the columns (or nodes) of that graph.

so if you have a graph
[[1, 2],
[3],
[4, 5]]

we'd expect the iterator to return [1, 3, 4, 2, 5]
"""

class ColumnTraversalIter():
    """
    This is a class which is used to contain the currently travered state.
    This is the class which defines the returned object for
    column_traversal_iter. The iter that function returns is an instance of
    this class.
    """
    def __init__(self, iter_of_iters):
        # Build a list of iterators
        self.iter_list = []
        for it in iter_of_iters:
            self.iter_list.append(it)
        self.current_iter_index = 0

    def __iter__(self):
        return self

    def __next__(self):
        # Get the next value from the current iterator
        try:
            return_val = next(self.iter_list[self.current_iter_index])
            self.current_iter_index = self._increment_index(
                self.current_iter_index,
                len(self.iter_list))
            return return_val
        except StopIteration:
            # When we run into a stop iteration we know that the current
            # iterator is out of values. Remove the current iterator from
            # the iterator list.
            del self.iter_list[self.current_iter_index]
            # If we are out of iterators it's time to raise StopIteration
            if len(self.iter_list) == 0:
                raise StopIteration
            else:
                # Otherwise, set the current_iter_index and recall next
                self.current_iter_index = self._increment_index(
                    self.current_iter_index,
                    len(self.iter_list))
                return self.__next__()
        except IndexError:
            # Someone called __next__ when there aren't any iterators left in
            # the iter_list.
            raise StopIteration

    @staticmethod
    def _increment_index(iter_index, wrap_length):
        if iter_index + 1 > wrap_length:
            print("returning 0")
            return 0
        else:
            print("returning {}".format(iter_index + 1))
            return iter_index + 1

def column_traversal_iter(iter_of_iters):
    """

    args:
        iterator: a iterator of iterators. If there aren't any iterators or
                  there are non iterator elements this will explode.
    returns a COlumnTraversalIter
    """
    return ColumnTraversalIter(iter_of_iters)

tests.py

import unittest
from column_traversal import column_traversal_iter

class TestBruteforceImplemetation(unittest.TestCase):

    def test_no_iters(self):
        test_iter = iter([])
        column_iter = column_traversal_iter(test_iter)
        with self.assertRaises(StopIteration):
            next(column_iter)

    def test_iter_of_one_empty_iter(self):
        """
        One empty iter and many empty iters should hit one stop iteration.
        """
        test_iter = iter([iter([])])
        column_iter = column_traversal_iter(test_iter)
        with self.assertRaises(StopIteration):
            next(column_iter)

    def test_iter_of_many_empty_iter(self):
        """
        One empty iter and many empty iters should hit one stop iteration.
        """
        test_iter = iter([iter([]), iter([]), iter([])])
        column_iter = column_traversal_iter(test_iter)
        with self.assertRaises(StopIteration):
            next(column_iter)

    def test_iter_simple_one_by_one_matrix(self):
        """
        One empty iter and many empty iters should hit one stop iteration.
        """
        test_iter = iter([iter([1]), iter([2]), iter([3])])
        column_iter = column_traversal_iter(test_iter)
        expected_traversal = [1, 2, 3]
        for actual_value, expected_value in zip(column_iter, expected_traversal):
            self.assertEqual(actual_value, expected_value)

        # Check to make sure there's a stop iteration.
        with self.assertRaises(StopIteration):
            next(column_iter)

    def test_iter_simple_jagged_graph(self):
        """
        One empty iter and many empty iters should hit one stop iteration.
        """
        test_iter = iter([iter([1]), iter([2, 4]), iter([3])])
        column_iter = column_traversal_iter(test_iter)
        expected_traversal = [1, 2, 3, 4]
        for actual_value, expected_value in zip(column_iter, expected_traversal):
            self.assertEqual(actual_value, expected_value)

        # Check to make sure there's a stop iteration.
        with self.assertRaises(StopIteration):
            next(column_iter)

    def test_iter_simple_two_by_two_matrix(self):
        """
        One empty iter and many empty iters should hit one stop iteration.
        """
        test_iter = iter([iter([1, 4]), iter([2, 5]), iter([3, 6])])
        column_iter = column_traversal_iter(test_iter)
        expected_traversal = [1, 2, 3, 4, 5, 6]
        for actual_value, expected_value in zip(column_iter, expected_traversal):
            self.assertEqual(actual_value, expected_value)

        # Check to make sure there's a stop iteration.
        with self.assertRaises(StopIteration):
            next(column_iter)

    def test_iter_one_iter_is_blank(self):
        """
        One empty iter and many empty iters should hit one stop iteration.
        """
        test_iter = iter([iter([1, 3]), iter([2, 4]), iter([])])
        column_iter = column_traversal_iter(test_iter)
        expected_traversal = [1, 2, 3, 4]
        for actual_value, expected_value in zip(column_iter, expected_traversal):
            self.assertEqual(actual_value, expected_value)

        # Check to make sure there's a stop iteration.
        with self.assertRaises(StopIteration):
            next(column_iter)

您将通过以下方式使用此代码

divisor_iter_list = []
for a_list in a_lists:
    divisor_iter_list.append(iter(a_list))

dividend_iter_list = []
for b_list in b_lists:
    divident_iter_list.append(iter(b_list))


divisor_iter = ColumnTraversalIter(divisor_iter_list)
dividend_iter = ColumnTraversalIter(divident_iter_list)
for divisor, dividend in zip(divisor_iter, dividend_iter):
    # Do calculations.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM