如何成对合并多个数组

时间:2014-05-29 15:15:42

标签: python arrays

我遇到了问题"配对"数组为一(按索引)。这是一个例子:

INPUT:

inputArray = [[0, 1, 2, 3, 4], [2, 3, 5, 7, 8], [9, 6, 1]]

预期输出:

outputArray = 
[[0,2,9],
[1,3,6],
[2,5,1],
[3,7,chooseRandom()],
[4,8,chooseRandom()]]

问题:

  1. 如何避免"超出范围" "索引错误"问题
  2. 如何编写chooseRandom()来选择N邻居
  3. 数目:

    1. [求助] @jonrsharpe&提供的解决方案@Christian& @Decency的作用如下 预期
    2. 澄清:

      由N邻居我的意思是: enter image description here

      我使用python但随时可以用任何语言分享您的想法。

4 个答案:

答案 0 :(得分:3)

我认为以下内容可以满足您的需求:

from itertools import izip_longest # 'zip_longest' in Python 3.x
from random import choice

# Step 1
outputArray = list(map(list, izip_longest(*inputArray)))
# Step 2
for index, arr in enumerate(outputArray):
    if any(item is None for item in arr):
        valid = [item for item in arr if item is not None]
        outputArray[index] = [choice(valid) if item is None else item 
                              for item in arr]

这有两个步骤:

  1. inputArray的所有子列表与最长子阵列的长度相结合,填充None[[0, 2, 9], [1, 3, 6], [2, 5, 1], [3, 7, None], [4, 8, None]];和
  2. 完成outputArray,查找包含None的所有子列表,并从子列表中的其他项目中随机选择替换None。 t None
  3. 示例输出:

    [[0, 2, 9], [1, 3, 6], [2, 5, 1], [3, 7, 3], [4, 8, 8]]
    

答案 1 :(得分:2)

这是我在Python 3.4中解决问题的方法。我真的不知道你的意思"选择N邻居"但是在下面的上下文中你应该很容易写出来。

inputArray = [[0, 1, 2, 3, 4], [2, 3, 5, 7, 8], [9, 6, 1]]

import itertools

zipped = itertools.zip_longest(*inputArray, fillvalue=None)
outputArray = [list(item) for item in zipped]
# [[0, 2, 9], [1, 3, 6], [2, 5, 1], [3, 7, None], [4, 8, None]]

# Now replace the sentinel None in our sublists
for sublist in outputArray:
    for i, element in enumerate(sublist):
        if element is None:
            sublist[i] = chooseRandom()

print(outputArray)

答案 2 :(得分:1)

不是最pythonic方式,但您可以尝试使用此代码剪切,阅读下面代码中的注释:

import itertools, random

inputArray = [ [0, 1, 2, 3, 4], [2, 3, 5, 7, 8], [9, 6, 1] ]
outputArray = []

max_length = max(len(e) for e in inputArray) # maximum length of the sublists in <inputArray>
i = 0 # to keep the index of sublists of <outputArray>

for j in range(max_length):
    outputArray.append([]) # add new sublist
    for e in inputArray: # iterate through each element of <inputArray>
        try:
            outputArray[i].append(e[j]) # try to append the number, if an exception is raised
                                        # then the code in the <except> clause will be executed
        except IndexError as e:
            outputArray[i].append(random.randint(0, 10)) # add the random number
    i += 1 # increase the sublists index on each iteration

print outputArray
# [[0, 2, 9], [1, 3, 6], [2, 5, 1], [3, 7, 3], [4, 8, 7]]

注意:

您可能想要更改部分

random.randint(0, 10)

获取“N邻居”

答案 3 :(得分:0)

让我知道您是否喜欢这段代码:

import random

array = [[0, 1, 2, 3, 4], [2, 3, 5, 7, 8], [9, 6, 1]]

max_len = max([len(l) for l in array])
dictionary = {}

for l in array:
    for i in range(0,len(l)):
        if dictionary.has_key(i):
            dictionary[i].append(l[i])
        else:
            dictionary[i] = [l[i]]
    for i in range(len(l),max_len):
        if dictionary.has_key(i):
            dictionary[i].append(random.choice(l))
        else:
            dictionary[i] = [random.choice(l)]

print dictionary.values()