飞镖逆映射 [英] Inverse mapping in dart

查看:99
本文介绍了飞镖逆映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在dart中有以下地图:

Say that I have the following map in dart:

Map f = {
  0 : 0,
  1 : 1,
  2 : 0,
  3 : 1, 
  4 : 0,
  5 : 1
};

dart中是否有东西,以便您可以轻松地处理地图的逆地图 f ?因此,例如,逆映射f⁻¹[0] (以数学符号表示)应等于集合 0、2、4

Is there something in dart so that you can easily work with the inverse map of the map f? So for example, the inverse map f⁻¹[0] (in math notation) should be equal to the set 0, 2, 4 in this case.

推荐答案



Map f = {
  0 : 0,
  1 : 1,
  2 : 0,
  3 : 1,
  4 : 0,
  5 : 1
};

main() {
  print(f.keys.where((k) => f[k] == 0));
  // or 
  print(new Map.fromIterable(f.values.toSet(), 
    key: (k) => k, 
    value: (v) => f.keys.where((k) => f[k] == v)));
}

尝试在 DartPad

这篇关于飞镖逆映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆