仅考虑其他列表的元素,从压缩列表创建列表

时间:2020-07-07 20:40:12

标签: python list

我想从一个压缩列表(ziped_list)创建一个列表,但仅考虑另一个列表(其他)的元素。

两个基本列表是:

import { Component, OnInit } from '@angular/core';
import { HttpClientModule, HttpClient, HttpBackend } from '@angular/common/http';
import { Observable, observable } from 'rxjs';


@Component({
  selector: 'app-post',
  templateUrl: './post.component.html',
  styleUrls: ['./post.component.css']
})
export class PostComponent implements OnInit {
  
  post: any[];
  
  
   constructor(private http: HttpClient) { 
      http.get('https://jsonplaceholder.typicode.com/posts').subscribe(responce => {

          this.post = responce

      })

     }

 
  ngOnInit() {
  }

}
  • 创建的压缩列表如下:
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_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])]

如果您可以详细说明如何修复我的代码或找到另一种pythonic方式,将会很有帮助。

2 个答案:

答案 0 :(得分:1)

方法如下:

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])]

更新:

您可以在这里使用operator.itemgetter()

from operator import itemgetter as ig

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']

idx = [[i for i,a in enumerate(l) if a in other] for l in base1] # Nested list of indexes

lst1 = [ig(*i)(l) for l, i in zip(base1, idx)] # List of letters
lst2 = [ig(*i)(l) for l, i in zip(base2, idx)] # List of numbers

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

答案 1 :(得分:0)

您可以再次应用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,)]]