【发布时间】:2022-07-11 19:46:56
【问题描述】:
我想在list 中生成一个list:
现在,我基本上有两个选择,要么我通过文本文件input list,要么我应该自己生成list。
是否可以使用嵌套循环自行生成这种类型的列表?
[[0,0,0], [0,0,0] ,[0,0,0], [0,0,0,], [0,0,0]]
我想把-1放在每个子列表的中间零的位置,比如[0, -1, 0],有5子列表所以-1应该插入5次
所以想要的结果是
[[0,-1,0], [0,0,0] ,[0,0,0], [0,0,0,], [0,0,0]]
[[0,0,0], [0,-1,0] ,[0,0,0], [0,0,0,], [0,0,0]]
[[0,0,0], [0,0,0] ,[0,-1,0], [0,0,0,], [0,0,0]]
[[0,0,0], [0,0,0] ,[0,0,0], [0,-1,0,], [0,0,0]]
[[0,0,0], [0,0,0] ,[0,0,0], [0,0,0,], [0,-1,0]]
在我的实际工作中,我有38个子列表,为了方便,我这里只展示了5个。
我的尝试-
目前我正在通过使用json.loads 并将其作为字典输入,然后使用 append 将其收集并进一步将其转换为列表,然后我将使用这些值。
但是,这种方法对我来说似乎很麻烦。
F = []
import json
with open('unitvalue.txt') as f:
f_1 = {int(key): json.loads(val) for key, val in json.loads(f.readline()).items()}
f_2 = {int(key): json.loads(val) for key, val in json.loads(f.readline()).items()}
f_3 = {int(key): json.loads(val) for key, val in json.loads(f.readline()).items()}
f_4 = {int(key): json.loads(val) for key, val in json.loads(f.readline()).items()}
f_5 = {int(key): json.loads(val) for key, val in json.loads(f.readline()).items()}
unitvalue.txt 包含在哪里
{"1":"[0,-1,0]", "2":"[0,0,0]","3":"[0,0,0]", "4":"[0,0,0]", "5":"[0,0,0]"}
{"1":"[0,0,0]", "2":"[0,-1,0]","3":"[0,0,0]", "4":"[0,0,0]", "5":"[0,0,0]"}
{"1":"[0,0,0]", "2":"[0,0,0]","3":"[0,-1,0]", "4":"[0,0,0]", "5":"[0,0,0]"}
{"1":"[0,0,0]", "2":"[0,0,0]","3":"[0,0,0]", "4":"[0,-1,0]", "5":"[0,0,0]"}
{"1":"[0,0,0]", "2":"[0,0,0]","3":"[0,0,0]", "4":"[0,0,0]", "5":"[0,-1,0]"}
【问题讨论】:
标签: python arrays list loops matrix