在给定列表中的大列表元素不在较小列表中的位置添加默认值

问题描述 投票:-1回答:1

我有:

  • 大量字符串
  • 较小的字符串列表,所有值都在大号中
  • 与较小列表相同大小的值列表

让我们接受这些列表,满足以上条件:

Large_list = ['a','b','c','d','e','f']
Short_list = ['b','c','f']
List_values = [2,4,3]

我想在Short_list元素不在Large_list中的List_values中添加0。

预期结果:

[0,2,4,0,0,3]

我该怎么办?

python list
1个回答
2
投票

类似这样的东西:

In [810]: for c,i in enumerate(Large_list): 
     ...:     if i not in Short_list: 
     ...:         List_values.insert(c,0) 
     ...:                                                                                                                                                                                                   

In [811]: List_values                                                                                                                                                                                       
Out[811]: [0, 2, 4, 0, 0, 3]
© www.soinside.com 2019 - 2024. All rights reserved.