Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of this python dictionary in Dart?

Tags:

dart

What is the equivalent of this python dictionary in Dart?

edges = {(1, 'a') : 2,
         (2, 'a') : 2,
         (2, '1') : 3,
         (3, '1') : 3}
like image 628
Ramses Aldama Avatar asked Jul 19 '17 19:07

Ramses Aldama


People also ask

What is dictionary in Dart?

In Dart programming, Maps are dictionary-like data types that exist in key-value form (known as lock-key). There is no restriction on the type of data that goes in a map data type. Maps are very flexible and can mutate their size based on the requirements.

What can I use instead of a dictionary in Python?

As you can see, defaultdict has proven to be a useful alternative to dictionaries — especially when it comes to accumulating values.

Is there a dictionary for Python?

Python provides another composite data type called a dictionary, which is similar to a list in that it is a collection of objects. Here's what you'll learn in this tutorial: You'll cover the basic characteristics of Python dictionaries and learn how to access and manage dictionary data.


2 Answers

You could use package:collection's EqualityMap to define a custom hash algorithim that uses ListEquality. For example, you could do this:

var map = new EqualityMap.from(const ListEquality(), {
  [1, 'a']: 2,
  [2, 'a']: 2,
});

assert(map[[1, 'a']] == map[[1, 'a']])

This will be a heavier weight implementation of Map, though.

like image 68
matanlurey Avatar answered Oct 04 '22 17:10

matanlurey


You have differents way to do this

1. Using a List

var edges = <List, num>{
  [1, 'a']: 2,
  [2, 'a']: 2,
  [2, '1']: 3,
  [3, '1']: 3
};

Simple to write, but you won't be able to retrieve data with

edges[[2, 'a']]; // null

Except if you use const

var edges = const <List, num>{
  const [1, 'a']: 2,
  const [2, 'a']: 2,
  const [2, '1']: 3,
  const [3, '1']: 3
};  

edges[const [2, 'a']]; // 2

2. Using Tuple package

https://pub.dartlang.org/packages/tuple

var edges = <Tuple2<num, String>, num>{
  new Tuple2(1, 'a'): 2,
  new Tuple2(2, 'a'): 2,
  new Tuple2(2, '1'): 3,
  new Tuple2(3, '1'): 3
}

edges[new Tuple2(2, 'a')]; // 2
like image 27
Hadrien Lejard Avatar answered Oct 04 '22 17:10

Hadrien Lejard