Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中列表的分离_Python_List - Fatal编程技术网

python中列表的分离

python中列表的分离,python,list,Python,List,我试图在列表中存储随机组合的数字(范围为0,100)。 直截了当是没有问题的,但是,当我试图格式化长的100元素列表时,我遇到了一个问题。其格式应表示10x10矩阵 以下是我目前的代码: def random_elems_matrix(): with open('input.txt', 'w') as outfile: # Pretty much just saying hey, I want some random numbers between 0 and 1000,

我试图在列表中存储随机组合的数字(范围为0,100)。 直截了当是没有问题的,但是,当我试图格式化长的100元素列表时,我遇到了一个问题。其格式应表示10x10矩阵

以下是我目前的代码:

def random_elems_matrix():
    with open('input.txt', 'w') as outfile:
        # Pretty much just saying hey, I want some random numbers between 0 and 1000, and I want
        # 100 different numbers, and store them in an empty list so I can format it 
        new_range = []
        new_range = random.sample(range(0, 1000), 100)
        print (new_range, file = outfile)

random_elems_matrix()
理想情况下,我希望将其表示为:

[120, 622, 698, 367, 657, 929, 135, 423, 228, 941,

868, 893, 8, 840, 31, 678, 226, 599, 272, 133,

819, 662, 27, 195, 674, 2, 211, 550, 782, 165,

289, 770, 174, 827, 897, 296, 165, 38, 807, 701,

982, 706, 660, 795, 110, 799, 565, 745, 371, 77,

13, 201, 840, 961, 690, 345, 202, 79, 544, 196,

923, 386, 829, 799, 123, 45, 778, 880, 700, 302,

316, 302, 436, 145, 584, 533, 851, 542, 443, 619,

616, 692, 996, 634, 549, 105, 602, 938, 135, 468,

245, 443, 384, 987, 301, 364, 76, 599, 159, 908]
但目前它只是一个100个元素的列表

更新的解决方案:

import random
import numpy as np

# Function creates a random group of 10 elements to fit into the range 
from 0 to 1000
# Then repeats the process 10 times to create a 10x10 matrix from the 
lists.
class Matrix_Sorting():

    def random_elems_matrix():
        with open('input.txt', 'w') as outfile:
        # rows and columns to set up matrix size
        rows, cols = (10,10)
        # Since I don't know the input, I used a random method of 
generating numbers
        # to serve as the input for the 100 element matrix
        print(np.random.randfloat(1000, size=(rows, cols)), file = 
outfile)

random_elems_matrix()

一种方法是将列表转换为数组,并按如下方式重新调整其形状:

new_range=np.array(new_range).reshape(10,10)
输出:

array([[120, 622, 698, 367, 657, 929, 135, 423, 228, 941],
   [868, 893,   8, 840,  31, 678, 226, 599, 272, 133],
   [819, 662,  27, 195, 674,   2, 211, 550, 782, 165],
   [289, 770, 174, 827, 897, 296, 165,  38, 807, 701],
   [982, 706, 660, 795, 110, 799, 565, 745, 371,  77],
   [ 13, 201, 840, 961, 690, 345, 202,  79, 544, 196],
   [923, 386, 829, 799, 123,  45, 778, 880, 700, 302],
   [316, 302, 436, 145, 584, 533, 851, 542, 443, 619],
   [616, 692, 996, 634, 549, 105, 602, 938, 135, 468],
   [245, 443, 384, 987, 301, 364,  76, 599, 159, 908]])

如果您每次都随机生成列表,但没有任何条件,直接方法将生成所需形状的列表。可以这样做

new\u range=random.sample(范围(0,1000),100)。重塑(10,10)

如果您有一个列表,它是您需要进行10x10的任何操作的结果,那么请执行以下操作


res\u list=np.array(res\u list)。重塑(10,10)

只需尝试使用
new\u range=random.sample(range(0,1000),100)。重塑(10,10)
,顺便说一句,你不需要
new\u range=[]
是的,对不起,哈哈,我是在没有参考的情况下键入的。我知道事情很简单,谢谢你的帮助!我建议使用与OP相同的变量名。这使得一对一的通信更容易。我随机生成它,你的第一行是一个很好的解决方案,我很高兴你们都告诉我关于重塑的信息,并且通过扩展,numpy@ScottHolley如果这解决了您的问题,请接受并投票