Python 仅考虑其他列表的元素,从ziped列表创建列表

Python 仅考虑其他列表的元素,从ziped列表创建列表,python,list,Python,List,我想从一个ziped列表(ziped_列表)创建一个列表,但只考虑另一个列表(other)的元素 两个基本列表是: base1=['A','B'],['B'],['A','B','C','D','E'],['B'],['A','B','C'],['A'],['B','C'],['A','B'], [C'、[A'、[B']、[A']、[B'、[C']] base2=[[1.0,5.0]、[3.0]、[2.0,7.0,3.0,1.0,6.0]、[3.0]、[5.0,2.0,3.0]、[1.0]、[9

我想从一个ziped列表(ziped_列表)创建一个列表,但只考虑另一个列表(other)的元素

两个基本列表是:

base1=['A','B'],['B'],['A','B','C','D','E'],['B'],['A','B','C'],['A'],['B','C'],['A','B'],
[C'、[A'、[B']、[A']、[B'、[C']]
base2=[[1.0,5.0]、[3.0]、[2.0,7.0,3.0,1.0,6.0]、[3.0]、[5.0,2.0,3.0]、[1.0]、[9.0,3.0]、[2.0,7.0],
[3.0, 6.0, 8.0], [2.0], [7.0, 9.0]]
  • ziped列表创建如下:
ziped_list=list(zip(base1,base2))
看起来是这样的:

[(['A',B'],[1.0,5.0]),
(['B'],[3.0]),
(A,B,C,D,E),(2.0,7.0,3.0,1.0,6.0),,
(['B'],[3.0]),
([A'、[B'、[C']、[5.0,2.0,3.0]),
(['A'],[1.0]),
(['B','C',[9.0,3.0]),
(['A','B',[2.0,7.0]),
([C'、[A'、[B']、[3.0,6.0,8.0]),
([A'],[2.0]),
(['B','C',[7.0,9.0])]
  • 其他(参考)列表为:
other=['A','B']
预期结果是:

[(['A',B'],[1.0,5.0]),
(['B'],[3.0]),
(['A','B',[2.0,7.0]),
(['B'],[3.0]),
(['A','B',[5.0,2.0]),
(['A'],[1.0]),
([B'],[9.0]),
(['A','B',[2.0,7.0]),
(A,B,6.0,8.0),,
([A'],[2.0]),
(['B'],[7.0])]
我尝试了以下代码,但没有按预期工作:

otherSet=set(其他)
结果=[[x代表arr中的x,如果x代表其他集合]代表ziped_列表中的arr[:][0]]
结果

如果您能详细说明如何修复我的代码或找到另一种python方式,这将非常有用。

您可以再次应用
zip
进行筛选:

base1 = [['A', 'B'], ['B'], ['A', 'B', 'C', 'D', 'E'], ['B'], ['A', 'B', 'C'], ['A'], ['B', 'C'], ['A', 'B'], ['C', 'A', 'B'], ['A'], ['B', 'C']]
base2 = [[1.0, 5.0], [3.0], [2.0, 7.0, 3.0, 1.0, 6.0], [3.0], [5.0, 2.0, 3.0], [1.0], [9.0, 3.0], [2.0, 7.0], [3.0, 6.0, 8.0], [2.0], [7.0, 9.0]]
other = ['A', 'B']
result = [list(zip(*[[j,k] for j, k in zip(a, b) if j in other])) for a, b in zip(base1, base2)]
输出:

[[('A', 'B'), (1.0, 5.0)], 
 [('B',), (3.0,)], 
 [('A', 'B'), (2.0, 7.0)], 
 [('B',), (3.0,)], 
 [('A', 'B'), (5.0, 2.0)], 
 [('A',), (1.0,)], 
 [('B',), (9.0,)], 
 [('A', 'B'), (2.0, 7.0)], 
 [('A', 'B'), (6.0, 8.0)], 
 [('A',), (2.0,)], 
 [('B',), (7.0,)]]
[(['A', 'B'], [1.0, 5.0]),
 (['B'], [3.0]),
 (['A', 'B'], [2.0, 7.0]),
 (['B'], [3.0]),
 (['A', 'B'], [5.0, 2.0]),
 (['A'], [1.0]),
 (['B'], [9.0]),
 (['A', 'B'], [2.0, 7.0]),
 (['A', 'B'], [6.0, 8.0]),
 (['A'], [2.0]),
 (['B'], [7.0])]
以下是如何:

base1 = [['A', 'B'], ['B'], ['A', 'B', 'C', 'D', 'E'], ['B'], ['A', 'B', 'C'], ['A'], ['B', 'C'], ['A', 'B'], ['C', 'A', 'B'], ['A'], ['B', 'C']]
base2 = [[1.0, 5.0], [3.0], [2.0, 7.0, 3.0, 1.0, 6.0], [3.0], [5.0, 2.0, 3.0], [1.0], [9.0, 3.0], [2.0, 7.0], [3.0, 6.0, 8.0], [2.0], [7.0, 9.0]]
other = ['A', 'B']

lst1 = [[(a,i) for i,a in enumerate(l) if a in other] for l in base1] # List of letters with index
lst2 = [[l[i] for b,i in a] for a,l in zip(lst1,base2)] # List of numbers found by using the indexes in lst1
lst1 = [[s[0] for s in l] for l in lst1] # Remove index from letters

lst = [(a, b) for a, b in zip(lst1, lst2)] # Zip up list of letters and list of numbers

print(lst)
输出:

[[('A', 'B'), (1.0, 5.0)], 
 [('B',), (3.0,)], 
 [('A', 'B'), (2.0, 7.0)], 
 [('B',), (3.0,)], 
 [('A', 'B'), (5.0, 2.0)], 
 [('A',), (1.0,)], 
 [('B',), (9.0,)], 
 [('A', 'B'), (2.0, 7.0)], 
 [('A', 'B'), (6.0, 8.0)], 
 [('A',), (2.0,)], 
 [('B',), (7.0,)]]
[(['A', 'B'], [1.0, 5.0]),
 (['B'], [3.0]),
 (['A', 'B'], [2.0, 7.0]),
 (['B'], [3.0]),
 (['A', 'B'], [5.0, 2.0]),
 (['A'], [1.0]),
 (['B'], [9.0]),
 (['A', 'B'], [2.0, 7.0]),
 (['A', 'B'], [6.0, 8.0]),
 (['A'], [2.0]),
 (['B'], [7.0])]
更新: 下面是如何使用操作符.itemgetter():


如果可以将base1更改为元组列表,则可以从压缩结果创建一个
字典。然后对
循环使用
,并检查
其他
元素是否是键的一部分。@AnnZen是的,谢谢!也许还有另一个答案。你的回答不够快。你考虑过接受答案吗?让我们知道我们是否可以改进它们。很好,唯一的问题是这个过程看起来很慢。您的列表有多大?