生成一个基于其他列表的列表的Python代码

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

Generating a list based on other lists in Python

问题

以下是翻译好的代码部分:

我有两个列表```A2``````J```。

我正在执行一个操作每当```J[0][i]=0```,```A3==[0]```。我呈现当前和期望的输出

```python
A2 = [[2, 3, 5], [3, 4, 6], [0, 3, 5], [0, 1, 2, 4, 5, 6], [1, 3, 6], [0, 2, 3, 7, 8, 10], 
      [1, 3, 4, 8, 9, 11], [5, 8, 10], [5, 6, 7, 9, 10, 11], [6, 8, 11], [5, 7, 8], [6, 8, 9]]

J=[[0, 2, 0, 6, 7, 9, 10]]
A3=[]
for i in range(0,len(J[0])): 
    if (J[0][i]==0):
        A3==[0]
    else:
        A33=A2[J[0][i]]
        A3.append(A33)
print("A3 =",A3)

当前输出是:

A3 = [[0, 3, 5], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

期望输出是:

A3 = [[0], [0, 3, 5], [0], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

请注意,期望输出中每个子列表都包含一个0,而当前输出中没有。

英文:

I have two lists A2 and J.

I am performing an operation such that whenever J[0][i]=0, A3==[0]. I present the current and expected output.

A2 = [[2, 3, 5], [3, 4, 6], [0, 3, 5], [0, 1, 2, 4, 5, 6], [1, 3, 6], [0, 2, 3, 7, 8, 10], 
      [1, 3, 4, 8, 9, 11], [5, 8, 10], [5, 6, 7, 9, 10, 11], [6, 8, 11], [5, 7, 8], [6, 8, 9]]

J=[[0, 2, 0, 6, 7, 9, 10]]
A3=[]
for i in range(0,len(J[0])): 
    if (J[0][i]==0):
        A3==[0]
    else:
        A33=A2[J[0][i]]
        A3.append(A33)
print("A3 =",A3)

The current output is :

A3 = [[0, 3, 5], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

The expected output is :

A3 = [[0], [0, 3, 5], [0], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

答案1

得分: 2

A2 = [[2, 3, 5], [3, 4, 6], [0, 3, 5], [0, 1, 2, 4, 5, 6], [1, 3, 6], [0, 2, 3, 7, 8, 10],
      [1, 3, 4, 8, 9, 11], [5, 8, 10], [5, 6, 7, 9, 10, 11], [6, 8, 11], [5, 7, 8], [6, 8, 9]]

J = [[0, 2, 0, 6, 7, 9, 10]]
A3 = []
for i in range(0, len(J[0])):
    if (J[0][i] == 0):
        A3.append([0])  # 修改这里
    else:
        A33 = A2[J[0][i]]
        A3.append(A33)
print("A3 =", A3)

# 输出
A3 = [[0], [0, 3, 5], [0], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

注意:在 A3==[0] 的情况下,请改成 A3.append([0]),因为 A3==[0] 是在比较 A30,不会对 A3 进行任何更改。

英文:
A2 = [[2, 3, 5], [3, 4, 6], [0, 3, 5], [0, 1, 2, 4, 5, 6], [1, 3, 6], [0, 2, 3, 7, 8, 10], 
      [1, 3, 4, 8, 9, 11], [5, 8, 10], [5, 6, 7, 9, 10, 11], [6, 8, 11], [5, 7, 8], [6, 8, 9]]

J=[[0, 2, 0, 6, 7, 9, 10]]
A3=[]
for i in range(0,len(J[0])): 
    if (J[0][i]==0):
        A3.append([0])  # change here
    else:
        A33=A2[J[0][i]]
        A3.append(A33)
print("A3 =",A3)

#output
A3 = [[0], [0, 3, 5], [0], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

Instead of A3==[0] please do A3.append([0]) as A3==[0] is comparing A3 with 0 and it is doing no change to A3

答案2

得分: 0

在你的代码中进行以下更改:

A3 = []
for i in J[0]:
     if i == 0:
             A3.extend([[0]])
     else:
             A3.extend([A2[i]])

现在A3的值为:

[[0], [0, 3, 5], [0], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

解释:

在你之前的代码中,A3 == 0不会将[0]插入到列表中,而是返回一个布尔值。由于你想要将0插入作为单个元素列表,你可以使用extend函数,它允许你附加带有多个元素的列表。

英文:

Make the following changes in your code:

A3 = []
for i in J[0]:
     if i == 0:
             A3.extend([[0]])
     else:
             A3.extend([A2[i]])

Now the value of A3 is:

[[0], [0, 3, 5], [0], [1, 3, 4, 8, 9, 11], [5, 8, 10], [6, 8, 11], [5, 7, 8]]

Explaination:

In your previous code A3==0 does not insert [0] to your list instead it returs a bool. Since you want to insert 0 as a single element list you can use extend function which lets you append a list with multiple elements.

huangapple
  • 本文由 发表于 2023年3月7日 18:10:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660564.html
匿名

发表评论

匿名网友

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

确定