What is the equivalent of this python dictionary in Dart?
edges = {(1, 'a') : 2,
(2, 'a') : 2,
(2, '1') : 3,
(3, '1') : 3}
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.
As you can see, defaultdict has proven to be a useful alternative to dictionaries — especially when it comes to accumulating values.
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.
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.
You have differents way to do this
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With