c#中的python默认字典等效

时间:2017-06-24 21:29:12

标签: c# python dictionary

这相当于Python字典,但在C#中

=INFO REPORT==== 24-Jun-2017::00:57:37 ===
accepting AMQP connection <0.3846.0> (127.0.0.1:57753 -> 127.0.0.1:5672)

=INFO REPORT==== 24-Jun-2017::00:57:37 ===
connection <0.3846.0> (127.0.0.1:57753 -> 127.0.0.1:5672): user 'guest' authenticated and granted access to vhost '/'

=INFO REPORT==== 24-Jun-2017::00:58:08 ===
accepting AMQP connection <0.3871.0> (127.0.0.1:57821 -> 127.0.0.1:5672)

=INFO REPORT==== 24-Jun-2017::00:58:08 ===
connection <0.3871.0> (127.0.0.1:57821 -> 127.0.0.1:5672): user 'guest' authenticated and granted access to vhost '/'

=INFO REPORT==== 24-Jun-2017::01:10:49 ===
accepting AMQP connection <0.3940.0> (127.0.0.1:57861 -> 127.0.0.1:5672)

=INFO REPORT==== 24-Jun-2017::01:10:49 ===
connection <0.3940.0> (127.0.0.1:57861 -> 127.0.0.1:5672): user 'guest' authenticated and granted access to vhost '/'

=WARNING REPORT==== 24-Jun-2017::01:10:49 ===
closing AMQP connection <0.3940.0> (127.0.0.1:57861 -> 127.0.0.1:5672):
client unexpectedly closed TCP connection

我认为这是简单的字典,但后来我看到了这样的陈述并且感到困惑; V是图中节点的整数:

from collections import defaultdict

graph = defaultdict(list) # default dictionary to store graph

这个if语句正在检查什么?

1 个答案:

答案 0 :(得分:2)

本身没有任何等价物。 这是你能得到的最接近的。

Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>();

不同之处在于,在Python中,您将获得每个键免费的默认空列表。 在c#中,如果密钥不存在,则必须新建一个新列表。