随机交错显示2数组在Python [英] Randomly Interleave 2 Arrays In Python

查看:182
本文介绍了随机交错显示2数组在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个数组:

  A = [1,2,3,4]
B = [5,6,7,8,9]

我想这两个数组交错一个变量C(请注意,'a'和'B'不一定相等长度的),但我不希望他们在交错确定性方式。总之,这是不够的,只是压缩这两个数组。我不想要的:

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

相反,我想要的东西,像随意:

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

还要注意的是A和B的顺序是所得数组中pserved $ P $,C。

目前的解决方案我需要一个for循环和一些随机数生成。我不喜欢这样,我希望有人能指出我到一个更好的解决方案。

 #生成的数组
C = []#这告诉我们的元素在C放置的比率。如果有更多的元件
#在'A'这个比例会更大,因为我们遍历元素,我们会下
#从'A'到'C'更多的元素。
比=浮动(LEN(A))/浮点(LEN(A)+ LEN(B))而a和b:
    which_list = random.random()
    如果which_list<比:
        c.append(a.pop(0))
    其他:
        c.append(b.pop(0))任何额外的元素进行到底#粘性
如果一个:
    C + =一
ELIF乙:
    C + = B


解决方案

编辑:我觉得这最近一个是最好的:

  A = [1,2,3,4]
B = [5,6,7,8,9]
C = [x.pop(0)用于在random.sample×([一个] * len个(一)+ [B] * len个(b)中,LEN(一)+ LEN(b))的]

或者更有效的:

  C =图(下一个,random.sample([ITER(一)] * LEN(A)+ ITER(B)] * LEN(B),LEN(一) + LEN(b)))

请注意,上面的第一种方法修改原始列表(如您的code一样),而第二种方法则没有。 Python的3​​.x的,你需要做的列表(图(...)),因为地图返回一个迭代器

原来的答复如下:

下面是保存几行选项:

  A = [1,2,3,4]
B = [5,6,7,8,9]C = []
TMP = [α] * LEN(一)+ [B] * len个(二)
而a和b:
    c.append(random.choice(TMP).pop(0))C + = A + B

下面是另一种选择,但如果你知道你所有的元素都没有falsy它只会工作(没有 0 '',或空序列):

  A = [1,2,3,4]
B = [5,6,7,8,9]比=浮动(LEN(A))/浮点(LEN(A)+ LEN(B))
C = [(不和b.pop(0))或(未b和a.pop(0))或
     (random.random()&下,比率和b.pop(0))或a.pop(0)
     为_在范围(LEN(A)+ LEN(b))的]

Suppose I have two arrays:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8, 9]

I want to interleave these two arrays to a variable 'c' (note 'a' and 'b' aren't necessarily of equal length) but I don't want them interleaved in a deterministic way. In short, it isn't enough to just zip these two arrays. I don't want:

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

Instead, I want something random like:

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

Also notice that the order of 'a' and 'b' are preserved in the resulting array, 'c'.

The current solution I have requires a for loop and some random number generation. I don't like it and I'm hoping someone can point me to a better solution.

# resulting array
c = []

# this tells us the ratio of elements to place in c. if there are more elements 
# in 'a' this ratio will be larger and as we iterate over elements, we will place
# more elements from 'a' into 'c'.
ratio = float(len(a)) / float(len(a) + len(b))

while a and b:
    which_list = random.random()
    if which_list < ratio:
        c.append(a.pop(0))
    else:
        c.append(b.pop(0))

# tack on any extra elements to the end
if a:
    c += a
elif b:
    c += b

解决方案

edit: I think this recent one is best:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8, 9]
c = [x.pop(0) for x in random.sample([a]*len(a) + [b]*len(b), len(a)+len(b))]

Or more efficiently:

c = map(next, random.sample([iter(a)]*len(a) + [iter(b)]*len(b), len(a)+len(b)))

Note that the first method above modifies the original lists (as your code did) while the second method does not. On Python 3.x you would need to do list(map(...)) since map returns an iterator.

original answer below:

Here is an option that saves a few lines:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8, 9]

c = []
tmp = [a]*len(a) + [b]*len(b)
while a and b:
    c.append(random.choice(tmp).pop(0))

c += a + b

Here is another option, but it will only work if you know that all of your elements are not falsy (no 0, '', None, False, or empty sequences):

a = [1, 2, 3, 4]
b = [5, 6, 7, 8, 9]

ratio = float(len(a)) / float(len(a) + len(b))
c = [(not a and b.pop(0)) or (not b and a.pop(0)) or
     (random.random() < ratio and b.pop(0)) or a.pop(0)
     for _ in range(len(a) + len(b))]

这篇关于随机交错显示2数组在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆