简体   繁体   English

需要创建一个包含 100 个子列表的列表,每个子列表包含 3 个随机值

[英]Need to create a list of 100 sublists that contain 3 random values each

I'm currently trying to create a list that is 100 items long, with each item being a sublist with 3 values.我目前正在尝试创建一个长度为 100 个项目的列表,每个项目都是一个具有 3 个值的子列表。

These values are being generated by randint(0,1000) , with each value of each sublist being unique.这些值由randint(0,1000)生成,每个子列表的每个值都是唯一的。 For example, if x1list = [1,2,3], x2list = [4,5,6], and x3list = [7,8,9]... I want testlist to contain [[1,4,7], [2,5,8], [3,6,9]] .例如,如果 x1list = [1,2,3]、x2list = [4,5,6] 和 x3list = [7,8,9]...我希望testlist包含[[1,4,7] , [2,5,8], [3,6,9]]

Here is my code so far, I'm new to programming so I apologize in advance if it is inefficient/basic.到目前为止,这是我的代码,我是编程新手,所以如果它效率低下/基本,我提前道歉。

import random
from random import randint

x1list = []
x2list = []
x3list = []
sublist = []

for i in range(100):
    x1list.append(randint(0,1000))
    x2list.append(randint(0,1000))
    x3list.append(randint(0,1000))

n = 0
for i in range(100):
    sublist.append(x1list[n])
    sublist.append(x2list[n])
    sublist.append(x3list[n])

    n += 1

testlist = [[value] for value in sublist]

The goal is to get the testlist to be the final 100 length list containing 100 sublists of length 3, as described previously.目标是使测试列表成为包含 100 个长度为 3 的子列表的最终 100 个长度列表,如前所述。 However, currently the list comprehension I have with testlist gives me a list of length 300, with each item in testlist being a random integer value that is it's own list .但是,目前我对 testlist 的列表理解给了我一个长度为 300 的列表, testlist中的每个项目都是一个随机整数值,它自己的 list

Another solution I tried with list comprehension got me somewhat closer to my end goal, but with a different problem...我尝试使用列表理解的另一个解决方案让我更接近我的最终目标,但有一个不同的问题......

Replacing the testlist list comprehension line with this instead:用这个替换testlist列表理解行:

testlist = [[x, y, z] for x in x1list for y in x2list for z in x3list]

Yielded testlist containing sublists of 3 values each, however testlist was 1000000 items in length and was seemingly only changing 1 value (x, y or z, not all 3) per each sublist.生成的测试列表包含每个包含 3 个值的子列表,但是测试列表的长度为1000000 个项目,并且每个子列表似乎只更改 1 个值(x、yz,并非全部为 3)。

Any idea on how to get the desired result for this?关于如何获得所需结果的任何想法? I've checked some other stackoverflow posts and they've helped me somewhat in getting to this point, but I've sort of hit a wall on this.我检查了一些其他的 stackoverflow 帖子,他们在某种程度上帮助了我达到这一点,但我在这方面遇到了麻烦。

Here are the posts I viewed to get me to this point:以下是我查看的帖子,让我达到这一点:

How do you turn a list of strings into a list of sublists with each string in each sublist? 如何将字符串列表转换为每个子列表中的每个字符串的子列表列表?

Python Create a list of sublists from a list Python从列表中创建子列表列表

Creating Sublists from a “List” - This last one did not help me very much as I could not get zip() to work. 从“列表”创建子列表- 最后一个对我没有多大帮助,因为我无法让zip()工作。

Thanks in advance for any help you're able to provide!预先感谢您提供的任何帮助!

Try list comprehension尝试列表理解

from random import randint
mylist = [[randint(0,1000),randint(0,100),randint(0,1000)] for x in range(100)]
print(mylist)

You can zip the 3 lists together:您可以将 3 个列表zip在一起:

import random
from random import randint

x1list = []
x2list = []
x3list = []

for i in range(100):
    x1list.append(randint(0,1000))
    x2list.append(randint(0,1000))
    x3list.append(randint(0,1000))

sublist = list(map(list, zip(x1list, x2list, x3list)))
print(sublist)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM