如何逐行组合两个数组

时间:2017-09-26 16:45:17

标签: arrays python-3.x numpy dxf

我在使用python进行编程时非常陌生。对于编程机器的使用,我需要使用g代码。因此,我使用Python程序从数据中读出gcode。现在我必须组合如下代码:

Code1:

  • 第1行(a,b,c)
  • line2(d,e,f)

代码2:

  • line1(g,h,i)
  • line2(j,k,l)

结果应该看起来像

  • line1(a,b,c,g,h,i)
  • line2(d,e,f,j,k,l)

我们现在的代码基本上只为两者创建代码。这不会合并它。我知道我必须和" numpy"不知怎的,但我卡住了。

import dxfgrabber
import numpy as np

left = dxfgrabber.readfile('data1')
right = dxfgrabber.readfile('data2')


def createcode(code):
    for i in code.entities:
        for p in i.points:
            mylist = np.array(p)
            print(mylist)


createcode(left)
createcode(right)

2 个答案:

答案 0 :(得分:1)

<强>鉴于

import itertools as it

import numpy as np


left = tuple("abc"), tuple("def")
right = tuple("ghi"), tuple("jkl")

<强>代码

合并可以通过简单的链接完成:

[tuple(it.chain.from_iterable(i)) for i in zip(left, right)]
# [('a', 'b', 'c', 'g', 'h', 'i'), ('d', 'e', 'f', 'j', 'k', 'l')]

这里的项目被压缩在一起,并在列表理解中展平。

扩展为打印numpy数组(或任何所需输出)的单个函数,您可以尝试以下操作:

def merge(*iterables):
    """Print merged iterables."""
    for i in zip(*iterables):
        result = tuple(it.chain.from_iterable(i))
        result = np.array(result)
        print(result)

merge(left, right)
# ['a' 'b' 'c' 'g' 'h' 'i']
# ['d' 'e' 'f' 'j' 'k' 'l']

这里可以将任意数量的iterables压缩在一起。

<强>演示

lt = [(10.0, 10.0, 0.0), (90.0, 10.0, 0.0), (90.0, 90.0, 0.0), (10.0, 90.0, 0.0)]
rt = [(20.0, 3.0, 6.0), (16.0, 6.0, 9.0), (5.0, 7.0, 7.0), (9.0, 2.0, 8.0)]

merge("abcd", lt, rt)
# ['a' '10.0' '10.0' '0.0' '20.0' '3.0' '6.0']
# ['b' '90.0' '10.0' '0.0' '16.0' '6.0' '9.0']
# ['c' '90.0' '90.0' '0.0' '5.0' '7.0' '7.0']
# ['d' '10.0' '90.0' '0.0' '9.0' '2.0' '8.0']

答案 1 :(得分:0)

这个怎么样?

class TornadoButton: UIButton{
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let pres = self.layer.presentation()!
        let suppt = self.convert(point, to: self.superview!)
        let prespt = self.superview!.layer.convert(suppt, to: pres)
        if (pres.hitTest(suppt)) != nil{
            return self
        }
        return super.hitTest(prespt, with: event)
    }

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let pres = self.layer.presentation()!
        let suppt = self.convert(point, to: self.superview!)
        return (pres.hitTest(suppt)) != nil
    }
}

您可以为这两种情况应用此功能,例如:

def createcode(code):
    return [[p for p in i.points] for i in code.entities]

在这个例子中你有两个列表。然后你可以把它们结合起来:

a = createdcode(left)
b = createdcode(right)

然后你可以打印出来:

c = [u + j for u, j in zip(a, b)]