需要编写一个函数来接收颜色列表,并输出它们的负色(来自字典的对)

我已经使用列表理解来做到这一点,但是我的代码无论如何都不令人满意。如何使其更短,更有效?请注意,我无法更改(check-expect (sort-list (list "ABD" "ABC" "BCE" "ADE")) (list "ABC" "ABD" "ADE" "BCE")) (check-expect (sort-list (list "ABF" "ABC" "BCE" "ADE")) (list "ABC" "ABF" "ADE" "BCE")) (check-expect (sort-list (list "ABD" "ABC" "BCE" "EFG")) (list "ABC" "ABD" "BCE" "EFG")) (check-expect (sort-list (list "ABC" "ABC" "BCG" "ADE")) (list "ABC" "ABC" "ADE" "BCG")) 字典,最好不要使用任何库。

cols

中:

def RevertCol(L):
    cols = {'white': 'black','red' : 'green','yellow': 'blue'}
    negative = []
    keys = cols.keys()
    values = cols.items()

    for col in L:
        if col in keys:
            negative.append(cols.get(col))
        else:
            for m,t in values:
                if t == col:
                    negative.append(m)
    return negative

退出:

RevertCol(['red','white']) 
rickylf2lee 回答:需要编写一个函数来接收颜色列表,并输出它们的负色(来自字典的对)

有没有理由不将所有颜色都作为字典上的键?例如:

cols = {'white': 'black','red' : 'green','yellow': 'blue','black': 'white','green': 'red','blue': 'yellow'}

然后您只需检查字典中每种颜色的存在并返回值即可得到答案。

如果您不想手动扩展字典,则可以通过编程方式进行:

inverse_dict = { v:k for (k,v) in cols.items() }

此外,您无需获取cols.keys()即可检查dict中是否存在键。只需检查

if col in cols

效率更高,因为它是哈希表检查(〜O(1)),而不是列表检查(O(n))。

所有这些加在一起,您可以得到一个像这样的解决方案:

def revert_color(colors):
    colors = {'white': 'black','yellow': 'blue'}
    inverse_colors = { v:k for (k,v) in cols.items() }

    complete_inverse = {**colors,**inverse_colors}

    # If color exists in the dict,return the inverse,else return the color itself.
    return [ complete_inverse[color] if color in complete_inverse else color for color in colors ]
,

为使您的方法更快速,更易于阅读,您可以将所有颜色取反存储在一张地图中

def RevertCol(L):
    cols = [('white','black'),('red','green'),('yellow','blue')]
    col_to_negative = {}
    col_to_negative.update(cols)
    col_to_negative.update((value,key) for key,value in cols)

    return [col_to_negative[col] for col in L]

您也可以预先计算一次col_to_negative,然后在每次调用时使用它

,

将第一个字典倒置,将它们组合起来,然后仅仅抓住您需要的东西吗?

def RevertCol2(L):
    cols = {'white': 'black','red': 'green','yellow': 'blue'}
    cols.update({value: key for key,value in cols.items()})
    return [cols[i] for i in L if i in cols.keys()]

(对我来说)它读起来更清晰,并消除了for循环。

,

复制字典并添加还原的条目。然后只需使用列表推导即可:

@Component({
  selector: 'app-root',template: `
    <mat-progress-bar mode="indeterminate"
                      *ngIf="loadingModules"
                      style="position: absolute; z-index: 10;">
    </mat-progress-bar>
    <router-outlet></router-outlet>
  `
})
export class AppComponent implements OnInit {
  public loadingModules: boolean;

  construction(private router: Router) { }

  public ngOnInit(): void {
     let loadingModules: number = 0;

     this.router.events.subscribe((event: Event) => {
        if (event instanceof RouteConfigLoadStart) {
          loadingModules++;
        }
        if (event instanceof RouteConfigLoadEnd) {
          loadingModules--;
        }
        this.isLoadingModules = !!loadingModules;
     });
  }
}
本文链接:https://www.f2er.com/3169979.html

大家都在问