匹配列表中的迭代,有很大的困难

时间:2016-07-30 19:24:56

标签: python arrays iterable

对于个人项目,我有三个列表。其中一个包含一组数组(l2),其大小始终由设置l2_size确定,并始终包含l1的随机成员。另一方面,下一个数组l3将始终是数量为len(l1)的0的集合。

这意味着......

l1 = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
l2 = [[3, 4, 5, 2, 4],[3, 5, 1, 2, 4], [4, 3, 2, 1, 5]]
l3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

我需要执行搜索

  1. 如果指针l2 [0]命中l2 [0],则从l2 [0]开始,搜索l1以找到l1 [i] == l2 [0]的第一个值。

  2. 一旦l2 [0]的第一个值使用l3分配其对应的值。

  3. 由于l2 [0]中数组的大小是5个成员,我们将分配相应的值1到l3,最多五次。一旦我们达到目标,我们就会进入下一组l2 [1]。

  4. 一旦我们完成了l2 [0](依此类推到l2 [1]),我们需要分配下一个相应的值,所以2,而不是覆盖l3中的值。

    < / LI>

    插图......

    想象一下这些是棒球得分卡......我的牌组里有l1棒球卡。我想要的棒球得分卡的组成包含在l2中。这些卡将赢得我的游戏!在这种情况下,l2 = [1,2,3,4,5]。我是一个大骗子,必须找到l1中的所有l2(手牌)。在l1中找到l2我标记他们在哪里使用l3。我也用l3告诉我把它放进哪只手。

    要成为有效的解决方案,我们必须对l2中的值进行唯一配对,以便使用l3将它们唯一地标识为l1中的值。这意味着......

    l1 = [1,2,3,4,5,1,2,3,4,5]
    l2 = [[1,2,3,4,5],[1,2,3,4,5]]
    l3 = [1,1,1,1,1,2,2,2,2,2]
    

    会有效。但...

    l1 = [1,2,3,4,5,1,2,3,4,5]
    l2 = [[1,2,3,4,5],[1,2,3,4,5]]
    l3 = [1,2,1,1,1,2,2,1,2,2]
    

    无效,因为在任何顺序中都没有包含[2,1,2,4,5]的l2。

    示例

    从l2 [0]开始,我们将反复遍历l1并拾取l2 [0]中的所有对象并将它们标记为l3。这应该是(手工)......

    l1 = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
    l2 = [[3, 4, 5, 2, 4],[3, 5, 1, 2, 4], [4, 3, 2, 1, 5]]
    l3 = [0,0,1,1,1,0,1,0,1,0,0,0,0,0,0]        
    

    值l已在l3中分配,因为它们是我们遇到的相应值的第一个实例。我们现在已经完成了l2 [0],因为我们在l1中找到了它的所有项目。接下来的工作是l2 [1] ......

    l1 = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
    l2 = [[3, 4, 5, 2, 4],[3, 5, 1, 2, 4], [4, 3, 2, 1, 5]]
    l3 = [2,2,1,1,1,0,1,0,1,0,0,0,2,2,2]        
    

    这是我已经做过但却无济于事的......

    assignvar = 1
    pos = 0
    for x in l2:
        for y in x:
            for z in l1:
                while userpos < len(l1):
                    if y == z:
                        l1[pos] = assignvar
                        while l2_size == pos:
                            assignvar += 1
                            l2_size = l2_size+l2_size #stops pos going out of range, changes assignvar to 2 (so appending the next set of l3 iterables to 2 .etc)
                    userpos = userpos+1
    

    在我的代码中如何解决这个问题真的很困惑。我觉得我有正确的想法使用三个for循环,但我已经用我的扳手打了一段时间,现在完全烧坏了。

    真实世界输入数据集......

    l1 = [5005.0, 5002.5, 5003.0, 5003.0, 5003.5, 5002.5, 5003.5, 5004.0, 5004.5, 5004.0, 5002.5, 5005.0, 5004.5, 5004.0, 5005.0, 5002.5, 5003.5, 5004.0, 5002.5, 5002.5, 5004.0, 5004.0, 5003.5, 5001.5, 5001.5, 5005.0, 5003.0, 5005.0, 5003.5, 5000.5, 5002.5, 5003.5, 5005.0]
    
    l2 = [[5002.5, 5004.0], [5002.5, 5004.5], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5004.5], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5003.0, 5003.5], [5003.0, 5003.5], [5003.0, 5004.0], [5003.0, 5004.5], [5003.0, 5004.0], [5003.0, 5005.0], [5003.0, 5004.5], [5003.0, 5004.0], [5003.0, 5005.0], [5003.0, 5003.5], [5003.0, 5004.0], [5003.0, 5004.0], [5003.0, 5004.0], [5003.0, 5003.5], [5003.0, 5005.0], [5003.0, 5005.0], [5003.0, 5003.5], [5003.0, 5003.5], [5003.0, 5005.0], [5003.0, 5003.5], [5003.0, 5003.5], [5003.0, 5004.0], [5003.0, 5004.5], [5003.0, 5004.0], [5003.0, 5005.0], [5003.0, 5004.5], [5003.0, 5004.0], [5003.0, 5005.0], [5003.0, 5003.5], [5003.0, 5004.0], [5003.0, 5004.0], [5003.0, 5004.0], [5003.0, 5003.5], [5003.0, 5005.0], [5003.0, 5005.0], [5003.0, 5003.5], [5003.0, 5003.5], [5003.0, 5005.0], [5002.5, 5004.0], [5002.5, 5004.5], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5004.5], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5004.5], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5004.0], [5002.5, 5004.0], [5002.5, 5005.0], [5002.5, 5005.0], [5002.5, 5005.0], [5001.5, 5005.0], [5001.5, 5005.0], [5001.5, 5005.0], [5001.5, 5005.0], [5001.5, 5005.0], [5001.5, 5005.0], [5003.0, 5005.0], [5003.0, 5003.5], [5003.0, 5003.5], [5003.0, 5005.0], [5002.5, 5005.0]]
    
    l3=[]
    for i in range(len(l1)):
        l3.append(int(0))
    

3 个答案:

答案 0 :(得分:2)

首先获取子列表,然后使用子列表元素获取l1的索引。根据l1的索引

为l3元素增加1
l1 = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
l2 = [[3, 4, 5, 2, 4],[3, 5, 1, 2, 4], [4, 3, 2, 1, 5]]
l3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

def combinList( lst ):
    ''' put list of list together based on index
        for example:
        a = [1,3,5,7]
        b = [2,4,6,8]
        combined list should be [1,2,3,4,5,6,7,8] '''
    step = len(lst)
    res= [None]*len(  reduce(lambda x,y:x+y, lst))
    for i,item in enumerate(lst):
        res[i::step] = sorted(item)
    return res


for value,line in enumerate(l2):
    counter = 0 
    record = {} 
    # count how many time this element appeared 
    for i in line:
        record[ i -1 ] = record.get( i - 1,0) + 1
    newList = combinList( [ [ i for i,j in enumerate(l1) if item == j] for item in line ] )
    for idx in newList:
        # if this element of l3 hasn't been change and there is at least one associated element in l2,put the value to l3 and reduce the number of l2
        if not l3[idx] and record.get(idx%5,0):
            l3[idx] = value + 1
            counter+=1
            record[idx%5] = record[idx%5] -1
        if counter >=5:
            break 
    print l3
print l3

输出:

#first iteration 
[0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
#second iteration 
[2, 1, 1, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, 2, 0]
#third iteration 
[2, 1, 1, 1, 1, 3, 2, 2, 1, 2, 0, 3, 3, 2, 3]
#final iteration 
[2, 1, 1, 1, 1, 3, 2, 2, 1, 2, 0, 3, 3, 2, 3]

答案 1 :(得分:0)

规格仍有点模糊,但试试这个 - 它适用于l1 = [1,2,3,4,5,1,2,3,4,5]l2 = [[1,2,3,4,5],[1,2,3,4,5]]的简单案例。

used_indices = set()

def get_next_index(card, l1 = l1, used_indices = used_indices):
    try:
        # find the next index that has not been used
        index = l1.index(card)
        while index in used_indices:
            # if the index has already been used, find the next one
            index = l1.index(card, index + 1)
    except ValueError as e:
        # this card cannot be found
        print('hand:{}, card:{} not found'.format(hand_no, card))
        index = None
    return index


for hand_no, hand in enumerate(l2, 1):
    for card in hand:
        index = get_next_index(card)
        if index:
            l3[index] = hand_no
            used_indices.add(index)

答案 2 :(得分:0)

我通过创建两个数组额外数组(称为“配置空间”)来简化解决方案,这些数组被赋予布尔值true(由整数1表示),具体取决于已将一个数字(一张卡)分配给一个手或一个成员子集(一只手)已经分配给一张卡。

此解决方案在l2之后进行一次旅行,然后通过l1进行跳闸,并且仅当手和卡都未分配时(如配置空间中的每个阵列中指示为0),才将牌分配给l3中的牌。

import { Component, Input } from '@angular/core';


export interface MyWidgetModel {
  title:string;
  description:string;
}


@Component({
  selector: 'my-widget',
  template: `
    <h4 *ngIf="!editing">{{data.title}}</h4>
    <input *ngIf="editing" type="text" name="title" [(ngModel)]="data.title">

    <p *ngIf="!editing">{{data.description}}</p>
    <textarea *ngIf="editing" name="description" [(ngModel)]="data.description" (ngModelChange)="customValidator($event)"></textarea>

    <button (click)="clickEditing()">{{editing ? 'save' : 'edit'}}</button>

  `
  styles: [
    ':host, :host > * { display: block; margin: 5px; }',
    ':host { margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #eee; }',
    '.ng-invalid { background-color: #FEE; }'
  ]

})
export class MyWidgetComponent {
  @Input() data:MyWidgetModel;

  constructor() {
    this.editing = false;
  }

  clickEditing() {
    this.editing = !this.editing;
  }

  customValidator(value:string) {
    console.log(this, value); //should be: MyWidgetComponent
    //How to set 'invalid' state here?
  }

}