How to generate a dictionary of dictionaries from an array?

Related searches

I have a array of strings like this:

Array = ['ABC', 'DEF', 'GHI']

How do I map this into a dictionary of dictionaries having the following shape?

dictionary = {
'ABC': {'DEF': auxFunction(ABC,DEF), 'GHI': auxFunction(ABC,GHI)},
'DEF': {'ABC': auxFunction(DEF,ABC), 'GHI': auxFunction(DEF,GHI)},
'GHI': {'ABC': auxFunction(GHI,ABC), 'DEF': auxFunction(GHI,DEF)},
}

You can do it with two dict comprehensions

dictionary = {name: {x: auxFunction(name, x) for x in array if x != name} for name in array}

{'ABC': {'DEF': auxFunction(ABC, DEF), 'GHI': auxFunction(ABC, GHI)},
 'DEF': {'ABC': auxFunction(DEF, ABC), 'GHI': auxFunction(DEF, GHI)},
 'GHI': {'ABC': auxFunction(GHI, ABC), 'DEF': auxFunction(GHI, DEF)}}

How to use Python dictionary of dictionaries – Linux Hint, How you can declare nested dictionaries and access data from them are described in this article by using In most of the programming languages, an associative array is used to store data using key-value pairs. Create a nested dictionary Dictionary<int,object> keyed = test .Select((obj, index) => Tuple.Create(obj, index)) .Where(x => x.Item1 != null) .ToDictionary(x => x.Item2, x => x.Item1); Minor note, though - the array will be faster ;p If the array isn't too sparse, you might just keep that kicking around.

You Can Try this:

finalDict= {}
Array = ['ABC', 'DEF', 'GHI']
for i in Array:
    tempDict = {}
    for j in Array:
        if i!= j:
            tempDict[j] = "A calculation between " + i + " - " + j
    finalDict[i] = tempDict
print(finalDict)

Output:

{
'ABC': {'DEF': 'A calculation between ABC - DEF', 'GHI': 'A calculation between ABC - GHI'},
'DEF': {'ABC': 'A calculation between DEF - ABC', 'GHI': 'A calculation between DEF - GHI'},
'GHI': {'ABC': 'A calculation between GHI - ABC', 'DEF': 'A calculation between GHI - DEF'}
}

Python, Creating an empty dict. myDict = dict (). # Creating a list. valList = [ '1' , '2' , '3' ]. # Iterating the elements in list. for val in valList: for ele in range� Swiftprovides an easy way to create a dictionary by putting the key-valuepairs inside a square bracket. One colonis used to separate each keyfrom its associated value. If we put dictionariesin an array, that will make it array of dictionaries. In this post, I will show you how to create an array of dictionarywith example.

>>> auxFunction = max
>>> {Array[i]: {Array[j]: auxFunction(Array[i], Array[j]) for j in range(len(Array)) if j != i} for i in range(len(Array))}
{456: {123: 456, 789: 789}, 123: {456: 456, 789: 789}, 789: {456: 789, 123: 789}}

Python, Creating a Nested Dictionary. Dict a Nested dictionary can be created by placing the comma-separated dictionaries enclosed withing braces. Like arrays, dictionaries are created with curly brackets {} . Key value pairs are stored on separate lines followed by a comma. Keys and values can be any data type --including strings, numbers, and variable names. This dictionary uses string as keys. Player Inventory Dictionary.

Python Dictionaries with Python, To create an empty dictionary, use a pair of braces {}. room_empty = {} This dictionary is created from a list of tuples using the dict key word:. This is the simplest method for creating a dictionary. You have to just assign the string in this format {“name”:”John”,”age”:26} to the variable disc. When you print it will output in the same format. Just keep in mind that dictionary is enclosed by Curley braces {}. Method 2. Create an empty dictionary

20. Dictionaries — How to Think Like a Computer Scientist: Learning , In other languages, they are called associative arrays since they associate a key with a value. As an example, we will create a dictionary to translate English� Dictionaries are the fundamental data structure in Python and are very important for Python programmers. They are an unordered collection of data values, used to store data values like a map.

Comments
  • What is the source of the calculation values? This must be know for someone to be able to answer your question.
  • Updated the values