如何在Python中随机播放多维列表 [英] How do I shuffle a multidimensional list in Python

查看:134
本文介绍了如何在Python中随机播放多维列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何通过使用随机库shuffle(myList)

I know how to shuffle a simple List in Python by using the shuffle function from the random library shuffle(myList)

但是我该如何整理多维列表?

But how do I shuffle a multidimensional list?

myList[][]

shuffle(myList)无效.

shuffle(myList) didn't work.

推荐答案

您必须重新整理顶层列表,然后才能将shuffle函数map shuffle用于子列表,如Python 2所示

You have to shuffle the top level list, then you can map the shuffle function to the sub lists like this in Python 2

from random import shuffle 

foo = []
foo.append([1, 2, 3])
foo.append([4, 5, 6])
foo.append([7, 8, 9])

shuffle(foo)
map(shuffle, foo)

print foo

请参阅python 2小提琴

在python 3中像这样

And like this in python 3

from random import shuffle 

foo = []
foo.append([1, 2, 3])
foo.append([4, 5, 6])
foo.append([7, 8, 9])

shuffle(foo)
for ii, sublist in enumerate(foo): 
    shuffle(foo[ii])

print(foo)

请参阅python 3小提琴

这篇关于如何在Python中随机播放多维列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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