如何在python中正确洗牌 [英] how to shuffle a list correctly in python

查看:37
本文介绍了如何在python中正确洗牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个洗牌列表的代码.我首先将它分成两个列表,因为我有一个交错函数,可以交错 2 个列表:

def shuffle(xs, n=1):il=list()如果 len(xs)%2==0:停止= int(len(xs)//2)a=xs[:停止]b=xs[停止:]打印(一)打印(b)别的:停止= int(len(xs)//2)a=xs[:停止]b=xs[停止:]打印(一)打印(b)如果n>0:对于范围(n)中的我:洗牌=交错(a,b)别的:返回返回洗牌

当我测试它时:

<预><代码>>>>洗牌([1,2,3,4,5,6,7],1)[1, 2, 3][4, 5, 6, 7]1[7][7, 4][1, 4, 2, 5, 3, 6, 7, 4]

两次列表中的 4 是什么,为什么打印 1, [7], 7,4]??

def interleave(xs,ys):a=xsb=ysminlength=[len(a),len(b)]额外列表=列表()交错=列表()对于范围内的 i((minval(minlength))):对=a[i],b[i]interleave.append(对)平=展平(交错)c=a+b如果len(b)>len(a):余数=len(b)-len(a)对于范围内的 j(余数,-1,-1):额外=b[-j]extralist.append(额外)如果len(a)>len(b):余数=len(a)-len(b)对于范围内的 j(余数,-1,-1):额外=a[-j]extralist.append(额外)del extralist[-1]最终=平坦+极端主义者返回决赛

解决方案

为什么不直接使用标准库?

<预><代码>>>>从随机导入随机播放>>>l = 列表(范围(1,20))>>>升[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]>>>洗牌(升)>>>升[17, 15, 9, 13, 19, 7, 10, 18, 5, 1, 12, 3, 2, 16, 4, 14, 8, 6, 11]>>>

I have this code that shuffles a list. I first split it into two lists because I have a interleave function that interleaves 2 lists:

def shuffle(xs, n=1):
    il=list()
    if len(xs)%2==0:
        stop=int(len(xs)//2)
        a=xs[:stop]
        b=xs[stop:]
        print(a)
        print(b)
    else:
        stop=int(len(xs)//2)
        a=xs[:stop]
        b=xs[stop:]
        print(a)
        print(b)
    if n>0:
        for i in range(n):
            shuffle=interleave(a,b)
    else:
        return 
    return shuffle

and when I test it:

>>> shuffle([1,2,3,4,5,6,7],1)
[1, 2, 3]
[4, 5, 6, 7]
1
[7]
[7, 4]
[1, 4, 2, 5, 3, 6, 7, 4]

What is 4 in the list twice and why is it printing 1, [7], 7,4]??

EDIT:

def interleave(xs,ys):
    a=xs
    b=ys
    minlength=[len(a),len(b)]
    extralist= list()
    interleave= list()
    for i in range((minval(minlength))):
        pair=a[i],b[i]
        interleave.append(pair)
        flat=flatten(interleave)
        c=a+b
    if len(b)>len(a):
        remainder=len(b)-len(a)
        for j in range(remainder,-1,-1):
            extra=b[-j]
            extralist.append(extra)
    if len(a)>len(b):
        remainder=len(a)-len(b)
        for j in range(remainder,-1,-1):
            extra=a[-j]
            extralist.append(extra)
    del extralist[-1]
    final=flat+extralist
    return final

解决方案

Why not just use the standard library?

>>> from random import shuffle
>>> l = list(range(1,20))                                                                                                                                                                                                                       
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> shuffle(l)                                                                                                                                                                                                                                  
>>> l
[17, 15, 9, 13, 19, 7, 10, 18, 5, 1, 12, 3, 2, 16, 4, 14, 8, 6, 11]
>>> 

这篇关于如何在python中正确洗牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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