Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的数组操作_Python_Arrays_Cycle - Fatal编程技术网

Python中的数组操作

Python中的数组操作,python,arrays,cycle,Python,Arrays,Cycle,如何在Python中执行以下操作: array_1 = [x1, x2, x3, x4, x5, x6, ....] array_2 = [y1, y2, y3] array_3 = [(x1-y1), (x2-y2), (x3-y3), (x4-y1), (x5-y2), (x6-y3)] 数组_2中的元素数始终小于数组_1中的元素数 array_1和array_2具有任意数量的元素 [numofelementsinarray\u 1]mod[numofelementsinarray\u

如何在Python中执行以下操作:

array_1 = [x1, x2, x3, x4, x5, x6, ....]
array_2 = [y1, y2, y3]

array_3 = [(x1-y1), (x2-y2), (x3-y3), (x4-y1), (x5-y2), (x6-y3)]
数组_2
中的元素数始终小于
数组_1
中的元素数

array_1
array_2
具有任意数量的元素


[numofelementsinarray\u 1]
mod
[numofelementsinarray\u 2]
=0

您可以使用
运算符。sub
映射一起使用:

array_3 = map(operator.sub,array_1,array_2+array_2)
或者,您可以使用
zip

array_3 = [x-y for x,y in zip(array_1,array2+array2)]
您可以使用

可以容纳任意大小的数组_2

这里将两个列表中的元素组合成成对的元素,并且将反复使用第二个列表来提供要配对的内容

如果不需要列表作为输出,只需迭代结果,也可以使用and:


对于较大的输入列表,这就省去了将中间结果存储在另一个列表中的麻烦。

Itertools有大量的工具来解决您的问题

理解你的问题

  • 一个数组比另一个数组短
  • 较短的数组应一直保留到较长的数组用尽为止
  • 较长数组和循环较短数组的
  • 这就是实现

    >>> arr1 = range(1,10)
    >>> arr2 = range(20,23)
    >>> from operator import sub
    >>> from itertools import izip, cycle, starmap
    >>> list(starmap(sub, izip(arr1, cycle(arr2))))
    [-19, -19, -19, -16, -16, -16, -13, -13, -13]
    

    除了标识符中有空格之外,你已经有的还有什么问题吗?@Wobbly:这大概只是一个例子,OP的列表比这些要大得多。是的,真正的数组要大得多。这只是一个简单的例子。array_2能比array_1大吗?你能描述一下你想要实现什么而不是给我们一个例子吗?-1,你将一个元组传递给
    操作符.sub
    ,它需要两个参数:)@mgilson:次要细节,很容易修复:-)我也可以使用星图,但输出需要是一个列表。-1撤销:)--我玩弄了
    map(sub,array1,cycle(array2))
    ,但这也是个坏消息--在python2.x上,它将永远消失,因为
    map
    花了两个序列中较长的时间。@mgilson:这是
    izip
    更好的主意的一个领域-是啊,我是用
    zip
    完成的——当你发布解决方案时,我正在阅读
    itertools
    文档。我永远都无法在脑海中清晰地记住
    cycle
    repeat
    )——(主要是因为我记不起
    cycle
    的名字。)@MartijnPieters:没有理由,这是个坏习惯。更正:-)
    from itertools import izip, cycle
    
    array_3 = [a - b for a, b in izip(array_1, cycle(array_2))]
    
    from itertools import imap, cycle
    import operator
    
    for result in imap(operator.sub, array_1, cycle(array_2)):
        # do something with the result
    
    >>> arr1 = range(1,10)
    >>> arr2 = range(20,23)
    >>> from operator import sub
    >>> from itertools import izip, cycle, starmap
    >>> list(starmap(sub, izip(arr1, cycle(arr2))))
    [-19, -19, -19, -16, -16, -16, -13, -13, -13]