附加列表的元素,其索引是不同列表中某个整数的索引
Appending the elements of a list whose indices are indices of a certain integer in a different list
下面是我的错误代码:
n = int(input())
coins = list(map(int,input().split()))
workertype = list(map(int,input().split()))
a1 = []
a2 = []
a3 = []
for i in range(n):
if(workertype[i]==1):
a1.append(coins[i])
elif(workertype[i]==2):
a2.append(coins[i])
elif(workertype[i]==3):
a3.append(coins[i])
print(a1,a2,a3)
coins
和 workertype
是两个等长的列表。我希望列表 a1
具有列表 coins
的那些元素,其索引是列表 workertype
中存在 1
(全 1)的元素。
a2
和 a3
相同,但所有 2
和 3
都相同。
这是我的输出
1(#input for n)
5 6 7 5 9 10 8(#coins)
1 2 3 3 2 2 1(#workertype)
[5] [] [](a1, a2, a3)
我希望 a1、a2、a3 分别为 [5,1]、[6,9,10]、[7,5]。
如果你输入7(硬币的长度和workertype)作为n应该可以。
下面是我的错误代码:
n = int(input())
coins = list(map(int,input().split()))
workertype = list(map(int,input().split()))
a1 = []
a2 = []
a3 = []
for i in range(n):
if(workertype[i]==1):
a1.append(coins[i])
elif(workertype[i]==2):
a2.append(coins[i])
elif(workertype[i]==3):
a3.append(coins[i])
print(a1,a2,a3)
coins
和 workertype
是两个等长的列表。我希望列表 a1
具有列表 coins
的那些元素,其索引是列表 workertype
中存在 1
(全 1)的元素。
a2
和 a3
相同,但所有 2
和 3
都相同。
这是我的输出
1(#input for n)
5 6 7 5 9 10 8(#coins)
1 2 3 3 2 2 1(#workertype)
[5] [] [](a1, a2, a3)
我希望 a1、a2、a3 分别为 [5,1]、[6,9,10]、[7,5]。
如果你输入7(硬币的长度和workertype)作为n应该可以。