dictionary

How to exchange values between specific keys in a dictionary?

梦想与她 提交于 2021-02-05 08:24:34
问题 Let's say you have a dictionary like this: d = { 'A': 'content_for_A', 'B': 'content_for_B' } What is the most efficient way to swap the values between the two entries? So the result should be like this: d = { 'A': 'content_for_B', 'B': 'content_for_A' } Of course, you can create a new dictionary d2 and do d2['A'] = d['B']; d2['B'] = d['A'] but is this the recommended way or is there an efficient way without the need to create a new dictionary? 回答1: d['A'], d['B'] = d['B'], d['A'] Tuple

How to exchange values between specific keys in a dictionary?

我与影子孤独终老i 提交于 2021-02-05 08:24:31
问题 Let's say you have a dictionary like this: d = { 'A': 'content_for_A', 'B': 'content_for_B' } What is the most efficient way to swap the values between the two entries? So the result should be like this: d = { 'A': 'content_for_B', 'B': 'content_for_A' } Of course, you can create a new dictionary d2 and do d2['A'] = d['B']; d2['B'] = d['A'] but is this the recommended way or is there an efficient way without the need to create a new dictionary? 回答1: d['A'], d['B'] = d['B'], d['A'] Tuple

Find duplicates of dictionary in a list and combine them in Python

别说谁变了你拦得住时间么 提交于 2021-02-05 08:23:11
问题 I have this list of dictionaries: "ingredients": [ { "unit_of_measurement": {"name": "Pound (Lb)", "id": 13}, "quantity": "1/2", "ingredient": {"name": "Balsamic Vinegar", "id": 12}, }, { "unit_of_measurement": {"name": "Pound (Lb)", "id": 13}, "quantity": "1/2", "ingredient": {"name": "Balsamic Vinegar", "id": 12}, }, { "unit_of_measurement": {"name": "Tablespoon", "id": 15}, "ingredient": {"name": "Basil Leaves", "id": 14}, "quantity": "3", }, ] I want to be able to find the duplicates of

Create a MATLAB dictionary like in Python

≯℡__Kan透↙ 提交于 2021-02-05 07:43:50
问题 I would like to know if there exists a way in MATLAB to create a dictionary like in Python. I have several Port Name and Port Type and I would like to create a dictionary like this : dict = {PortName : PortType, PortName : PortType, ...} 回答1: The closest analogy is containers.Map: containers.Map : Object that maps values to unique keys The keys are character vectors, strings, or numbers. The values can have arbitrary types. To create the map you pass containers.Map a cell array of keys and a

Create a MATLAB dictionary like in Python

六眼飞鱼酱① 提交于 2021-02-05 07:43:16
问题 I would like to know if there exists a way in MATLAB to create a dictionary like in Python. I have several Port Name and Port Type and I would like to create a dictionary like this : dict = {PortName : PortType, PortName : PortType, ...} 回答1: The closest analogy is containers.Map: containers.Map : Object that maps values to unique keys The keys are character vectors, strings, or numbers. The values can have arbitrary types. To create the map you pass containers.Map a cell array of keys and a

Find object in list with given property value then find dictionary value

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 07:01:26
问题 I have a list of objects. Each of these objects has a Name property, and an ObservablePairCollection which is just a custom dictionary that works EXACTLY like a dictionary, has a key/value pair. Given two strings, one for name and one for key, I want to find the object that first matches the name given, and then selects the pair from that model's dictionary that matches the given key value. Example: Given the string "model1" for name, and "Latitude" for the key, an object who's name property

Search for value in list in python dictionary

余生长醉 提交于 2021-02-05 06:42:26
问题 I want to be able to get the name of a person given a nickname (all nicknames are unique). A person can have multiple nicknames. I was thinking of using a dictionary like the following nicknames = { 'lebron james': ['king james', 'lbj'], 'dwayne johnson': ['rocky', 'the rock', 'brahma bull'] } So for instance, given a string 'rocky' , I want to be able to return 'dwayne johnson' . Is this kind of data structure the most optimal way to store the name=>nicknames pairing? Or is there a better

Getting the value from key in a dictionary that is nearest to a given number using JavaScript

梦想的初衷 提交于 2021-02-05 05:51:06
问题 I have a dictionary (keys are integers, values are float). I would now ask the dictionary for the value of the key that is > than the given number but < than the next greater key. Example: dict = {100: 0.0035, 150: 0.0024, 200: 0.0019}. i give 122, it should give me 0.0035 i give 333, it should give me 0.0019 i give 200, it should give me 0.0024 Thanks! 回答1: Working example (tested under Firefox 3.6): <html> <head> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script

Is there an IdentityHashMap implementation that maintains insert order?

拜拜、爱过 提交于 2021-02-05 05:14:01
问题 I need a HashMap that (1) matches keys by Object reference, and (2) maintains insertion order when iterating These functionalities are implemented in IdentityHashMap and LinkedHashMap separately. Is there any way to get a data structure that suits my need? Either one that is present in Java standard OR 3rd party libraries (like Guava), OR by using some trick on LinkedHashMap maybe, so that it uses object reference for matching keys? 回答1: You can use Guava's Equivalence for this: Equivalence

Is there an IdentityHashMap implementation that maintains insert order?

冷暖自知 提交于 2021-02-05 05:13:54
问题 I need a HashMap that (1) matches keys by Object reference, and (2) maintains insertion order when iterating These functionalities are implemented in IdentityHashMap and LinkedHashMap separately. Is there any way to get a data structure that suits my need? Either one that is present in Java standard OR 3rd party libraries (like Guava), OR by using some trick on LinkedHashMap maybe, so that it uses object reference for matching keys? 回答1: You can use Guava's Equivalence for this: Equivalence