dictionary

Python: create dictionary using dict() with integer keys?

做~自己de王妃 提交于 2021-01-20 16:15:48
问题 In Python, I see people creating dictionaries like this: d = dict( one = 1, two = 2, three = 3 ) What if my keys are integers? When I try this: d = dict (1 = 1, 2 = 2, 3 = 3 ) I get an error. Of course I could do this: d = { 1:1, 2:2, 3:3 } which works fine, but my main question is this: is there a way to set integer keys using the dict() function/constructor? 回答1: Yes, but not with that version of the constructor. You can do this: >>> dict([(1, 2), (3, 4)]) {1: 2, 3: 4} There are several

Is it possible to store functions in a dictionary?

拈花ヽ惹草 提交于 2021-01-20 15:23:43
问题 I have a message coming into my C# app which is an object serialized as JSON, when i de-serialize it I have a "Name" string and a "Payload" string[] , I want to be able to take the "Name" and look it up in a function dictionary, using the "Payload" array as its parameters and then take the output to return to the client sending the message, is this possible in C#? I've found a stack overflow answer here where the second part seems plausible but i don't know what I'm referencing with State 回答1

Invert a Map with redundant values to produce a multimap

痴心易碎 提交于 2021-01-19 06:45:44
问题 Given a map such as this where we have a frequency count per day-of-week for a year: Map.of( DayOfWeek.MONDAY , 52 , DayOfWeek.TUESDAY , 52 , DayOfWeek.WEDNESDAY, 53 , DayOfWeek.THURSDAY , 53 , DayOfWeek.FRIDAY , 52 , DayOfWeek.SATURDAY , 52 , DayOfWeek.SUNDAY , 52 ) …or as text: {MONDAY=52, TUESDAY=52, WEDNESDAY=53, THURSDAY=53, FRIDAY=52, SATURDAY=52, SUNDAY=52} …how can I invert to produce a multimap of distinct numbers each leading to a collection (list? set?) of the DayOfWeek which owned

Invert a Map with redundant values to produce a multimap

孤人 提交于 2021-01-19 06:40:07
问题 Given a map such as this where we have a frequency count per day-of-week for a year: Map.of( DayOfWeek.MONDAY , 52 , DayOfWeek.TUESDAY , 52 , DayOfWeek.WEDNESDAY, 53 , DayOfWeek.THURSDAY , 53 , DayOfWeek.FRIDAY , 52 , DayOfWeek.SATURDAY , 52 , DayOfWeek.SUNDAY , 52 ) …or as text: {MONDAY=52, TUESDAY=52, WEDNESDAY=53, THURSDAY=53, FRIDAY=52, SATURDAY=52, SUNDAY=52} …how can I invert to produce a multimap of distinct numbers each leading to a collection (list? set?) of the DayOfWeek which owned

微服务实战(二):落地微服务架构到直销系统(构建消息总线框架接口)

一笑奈何 提交于 2021-01-18 07:48:48
从上一篇文章大家可以看出,实现一个自己的消息总线框架是非常重要的内容,消息总线可以将界限上下文之间进行解耦,也可以为大并发访问提供必要的支持。 消息总线的作用: 1.界限上下文解耦:在DDD第一波文章中,当更新了订单信息后,我们通过调用经销商界限上下文的领域模型和仓储,进行了经销商信息的更新,这造成了耦合。通过一个消息总线,可以在订单界限上下文的WebApi服务(来源微服务-生产者)更新了订单信息后,发布一个事件消息到消息总线的某个队列中,经销商界限上下文的WebApi服务(消费者)订阅这个事件消息,然后交给自己的Handler进行消息处理,更新自己的经销商信息。这样就实现了订单界限上下文与经销商界限上下文解耦。 2.大并发支持:可以通过消息总线进一步提升下单的性能。我们可以将用户下单的操作直接交给一个下单命令WebApi接收,下单命令WebApi接收到命令后,直接丢给一个消息总线的队列,然后立即给前端返回下单结果。这样用户就不用等待后续的复杂订单业务逻辑,加快速度。后续订单的一系列处理交给消息的Handler进行后续的处理与消息的进一步投递。 消息总线设计重点: 1.定义消息(事件)的接口:所有需要投递与处理的消息,都从这个消息接口继承,因为需要约束消息中必须包含的内容,比如消息的ID、消息产生的时间等。 public interface IEvent { Guid Id {

Efficient way to filter a Map by value in Elixir

戏子无情 提交于 2021-01-18 05:26:25
问题 In Elixir, what would be an efficient way to filter a Map by its values. Right now I have the following solution %{foo: "bar", biz: nil, baz: 4} |> Enum.reject(fn {_, v} -> is_nil(v) end) |> Map.new This solution seems pretty inefficient to me. When called on a Map , Enum.reject/2 returns a Keywords . Since I want a Map , I need to call Map.new/1 to convert that Keywords back to me. This seems inefficient because Enum.reject/2 has to iterate over the Map once and then presumably, Map.new/1

Efficient way to filter a Map by value in Elixir

空扰寡人 提交于 2021-01-18 05:25:06
问题 In Elixir, what would be an efficient way to filter a Map by its values. Right now I have the following solution %{foo: "bar", biz: nil, baz: 4} |> Enum.reject(fn {_, v} -> is_nil(v) end) |> Map.new This solution seems pretty inefficient to me. When called on a Map , Enum.reject/2 returns a Keywords . Since I want a Map , I need to call Map.new/1 to convert that Keywords back to me. This seems inefficient because Enum.reject/2 has to iterate over the Map once and then presumably, Map.new/1

How to create a nested dictionary from a list in Python?

蹲街弑〆低调 提交于 2021-01-18 02:34:12
问题 I have a list of strings: tree_list = ['Parents', 'Children', 'GrandChildren'] How can i take that list and convert it to a nested dictionary like this? tree_dict = { 'Parents': { 'Children': { 'GrandChildren' : {} } } } print tree_dict['Parents']['Children']['GrandChildren'] 回答1: This easiest way is to build the dictionary starting from the inside out: tree_dict = {} for key in reversed(tree_list): tree_dict = {key: tree_dict} 回答2: 50 46 44 bytes Trying to golf this one: lambda l:reduce

How to create a nested dictionary from a list in Python?

廉价感情. 提交于 2021-01-18 02:33:58
问题 I have a list of strings: tree_list = ['Parents', 'Children', 'GrandChildren'] How can i take that list and convert it to a nested dictionary like this? tree_dict = { 'Parents': { 'Children': { 'GrandChildren' : {} } } } print tree_dict['Parents']['Children']['GrandChildren'] 回答1: This easiest way is to build the dictionary starting from the inside out: tree_dict = {} for key in reversed(tree_list): tree_dict = {key: tree_dict} 回答2: 50 46 44 bytes Trying to golf this one: lambda l:reduce

How to create a nested dictionary from a list in Python?

帅比萌擦擦* 提交于 2021-01-18 02:33:51
问题 I have a list of strings: tree_list = ['Parents', 'Children', 'GrandChildren'] How can i take that list and convert it to a nested dictionary like this? tree_dict = { 'Parents': { 'Children': { 'GrandChildren' : {} } } } print tree_dict['Parents']['Children']['GrandChildren'] 回答1: This easiest way is to build the dictionary starting from the inside out: tree_dict = {} for key in reversed(tree_list): tree_dict = {key: tree_dict} 回答2: 50 46 44 bytes Trying to golf this one: lambda l:reduce