How can I change the first element of a list?

huangapple go评论89阅读模式
英文:

How can I change the first element of a list?

问题

我有一个元素列表:

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2]]

我想按以下方式增加它:

[[1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1].................] 然后是2:

[[2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1].................] 以此类推,直到5。

我不想硬编码,所以我尝试使用itertools,但我只能达到2,像这样:

first2 = list(map(list, itertools.product(list(range(3)), repeat = 3)))

非常感谢任何帮助。

英文:

I have this list of elements:

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2]]

and I would like to increase it like this:

[[1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1].................] then with 2:

[[2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1].................] and so on until 5.

I dont want to hard code so I was trying itertools but I could only reach up to 2 like this:

first2 = list(map(list, itertools.product(list(range(3)), repeat = 3)))

Any help is highly appreciated.

答案1

得分: 1

使用itertools.product()函数创建第二个和第三个元素,并使用外部循环将0到5添加到它们之前。

import itertools

result = []
for i in range(6):
    result.extend([i] + list(x) for x in itertools.product(range(3), repeat=2))
英文:

Use itertools.product() to create the 2nd and 3rd elements, and an outer loop to prepend 0 through 5 to them.

result = []
for i in range(6):
    result.extend([i] + list(x) for x in itertools.product(range(3), repeat = 2))

答案2

得分: 0

我不明白你所说的“不想硬编码”的意思。

你不能像这样做吗?

for _ in range(5):
    for item in mylist:
        item[0] += 1
英文:

I don't understand what you mean by "dont want to hard code".

Can't you just do something like this?

for _ in range(5):
    for item in mylist:
        item[0] += 1

答案3

得分: 0

尝试:

lst = [
    [0, 0, 0],
    [0, 0, 1],
    [0, 0, 2],
    [0, 1, 0],
    [0, 1, 1],
    [0, 1, 2],
    [0, 2, 0],
    [0, 2, 1],
    [0, 2, 2],
]

out = []
for i in range(1, 6):
    out.append([[i, *subl[1:]] for subl in lst])

print(out)

输出结果:

[
    [
        [1, 0, 0],
        [1, 0, 1],
        [1, 0, 2],
        [1, 1, 0],
        [1, 1, 1],
        [1, 1, 2],
        [1, 2, 0],
        [1, 2, 1],
        [1, 2, 2],
    ],
    [
        [2, 0, 0],
        [2, 0, 1],
        [2, 0, 2],
...

一行代码:

out = [[[i, *subl[1:]] for subl in lst] for i in range(1, 6)]
print(out)
英文:

Try:

lst = [
    [0, 0, 0],
    [0, 0, 1],
    [0, 0, 2],
    [0, 1, 0],
    [0, 1, 1],
    [0, 1, 2],
    [0, 2, 0],
    [0, 2, 1],
    [0, 2, 2],
]

out = []
for i in range(1, 6):
    out.append([[i, *subl[1:]] for subl in lst])

print(out)

Prints:

[
    [
        [1, 0, 0],
        [1, 0, 1],
        [1, 0, 2],
        [1, 1, 0],
        [1, 1, 1],
        [1, 1, 2],
        [1, 2, 0],
        [1, 2, 1],
        [1, 2, 2],
    ],
    [
        [2, 0, 0],
        [2, 0, 1],
        [2, 0, 2],
...

One-liner:

out = [[[i, *subl[1:]] for subl in lst] for i in range(1, 6)]
print(out)

huangapple
  • 本文由 发表于 2023年8月9日 00:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定