logo
down
shadow

Get Dictionary from another dictionary


Get Dictionary from another dictionary

By : sarduri
Date : November 16 2020, 06:23 AM
help you fix your problem I have a data type like this , Assuming:
code :
Dictionary<string , Dictionary<Key, Value>> x = ...
Dictionary<Key, Value> innerDictionary = x["outer_key"];


Share : facebook icon twitter icon
Issue with Dictionary type, If gived a specific text then get the equivalent dictionary key or dictionary value?

Issue with Dictionary type, If gived a specific text then get the equivalent dictionary key or dictionary value?


By : Mousam Saikia
Date : March 29 2020, 07:55 AM
This might help you To answer your second question, on how to improve the loop you are using, it can be significantly improved. At the moment, if you have 100 items in the list view and 10 items in the directories, your code will loop at least 1000 times and could loop up to 10 times that depending on the values that are used in directories and whether or not ShowDescriptions is flagged.
We can reduce the direct looping to 110 times by building a dictionary by the appropriate key prior to entering the assignment loop:
code :
    If ListView_Monitor.Items.Count <> 0 Then
        Dim DirectoriesByCurrentKey As New Dictionary(Of String, String)

        If ShowDescriptions Then
            ' If we are showing the descriptions, add each of the items to the new collection, keyed by the value
            For Each key In Directories_SendTo.Keys
                Dim Description As String
                Description = Directories_SendTo(key)
                ' We don't know if description is unique, so make sure that we don't get a runtime error if we 
                ' try to add the same description multiple time
                If Not DirectoriesByCurrentKey.ContainsKey(Description) Then
                    DirectoriesByCurrentKey.Add(Description, key)
                End If
            Next
        Else
            ' Just use the current collection
            DirectoriesByCurrentKey = Directories_SendTo
        End If

        For Each Item As ListViewItem In ListView_Monitor.Items
            Dim sDescription As String = ""
            ' Try to find the current description using the current list item description
            If DirectoriesByCurrentKey.TryGetValue(Item.SubItems(2).Text, sDescription) Then
                ' If we found the entry, change the description to what we found
                Item.SubItems(2).Text = sDescription
            End If
        Next
    End If
C# Sum of Values in a nested dictionary where value is a class Dictionary<string, Dictionary<Int16, CommonData>

C# Sum of Values in a nested dictionary where value is a class Dictionary<string, Dictionary<Int16, CommonData>


By : dc94
Date : March 29 2020, 07:55 AM
I hope this helps . You didn't give some example input/output but I think your looking for something like
code :
var result = yourMainDict.OrderBy(x => x.Value.Sum(v => v.Value.Delivered + v.Value.Submitted + v.Value.Failed));
How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary?

How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary?


By : 吴高升
Date : March 29 2020, 07:55 AM
around this issue Using a generator, this is fairly straight forward:
Code:
code :
def make_flat_tuples(data, ref):
    for k, v in data.items():
        if isinstance(v, dict):
            for x in make_flat_tuples(v, ref[k]):
                yield x
        else:
            yield ref[k], v

flat = dict(make_flat_tuples(nested, reference))
from collections import defaultdict

reference = defaultdict(dict)
reference['agent'] = defaultdict(dict)

reference['agent']['address'] = 'agentaddress'
reference['agent']['zone']['id'] = 'agentzoneid'
reference['eventid'] = 'eventid'
reference['file']['hash'] = 'filehash'
reference['file']['name'] = 'filename'

nested = defaultdict(dict)

nested['agent']['address'] = '172.16.16.16'
nested['eventid'] = '1234566778'
nested['file']['name'] = 'reallybadfile.exe'

print(dict(make_flat_tuples(nested, reference)))
{
    'agentaddress': '172.16.16.16', 
    'eventid': '1234566778', 
    'filename': 'reallybadfile.exe'
}
How to modify or create a dictionary based on one dictionary querying the values of another dictionary in Python

How to modify or create a dictionary based on one dictionary querying the values of another dictionary in Python


By : Irwan Abdullah AlMun
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have two dictionaries: Dictionary A has an increasing and unique Item ID as the key and it is associated to an FID value. , try this code:
code :
dict_a = dict({11111: "FID01", 22222: "FID01", 33333:"FID03", 44444: "FID01"} )
dict_b = dict({"FID01": 25, "FID02": 50, "FID03": 75, "FID04": 90})

dict_b_keys = dict_b.keys()

{key: dict_b[value] for (key, value) in dict_a.items() if value in dict_b_keys}
conditionally adding dictionary entries from a second dictionary to a new dictionary while preserving new dictionary key

conditionally adding dictionary entries from a second dictionary to a new dictionary while preserving new dictionary key


By : user3658473
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If I understand you correctly, you want to update dict2 if the item in list1 is in dict1 using the corresponding key in list2.
You can zip the lists and iterate over the keys in both lists, setting the value using a conditional statement.
code :
dict1 = {'a':1,'b':2,'d':4}
dict2 = {'p':'blank','q':'blank','r':'blank','s':'blank'}
list1 = ['a','b','c','d']
list2 = ['p','q','r','s']

for a, b in zip(list1, list2):
    dict2[b] = dict1[a] if a in dict1 else 'blank'

print(dict2)
{'p': 1, 'q': 2, 'r': 'blank', 's': 4}
dict2.update({b : dict1[a] if a in dict1 else 'blank' for a,b in zip(list1, list2)})
Related Posts Related Posts :
  • Can't set a date time in one of my entity framework objects, says NullReferenceException
  • GETJob() Win32 Print spooler api 64bit The parameter is incorrect, how to fix?
  • Switch Case Causing Trouble In Going To A particular Function
  • Switch Case Calling A Function Based On The User's Choice
  • Accessing WebBrowser on different Threads
  • CPU Usage Avoid 100% With Environment.ProcessorCount?
  • Select distinct categories from database
  • C# app getting Watson dialog when calling TerminateProcess
  • The underlying provider failed on Open in entity framework connection
  • Compile a library without checking references
  • Can we create and access a registry key in HKEY_LOCAL_MACHINE without running application with admin permissions in C#
  • Control light intensity with a UI slider
  • Threading (Does these threads exits or aborts itself?)
  • Change the function of a button if Logged in
  • how to accept value in date format in textbox in C# windows form?
  • remove the lines from RichtextBox?
  • How to call String.Split that takes string as separator?
  • Specify task timeout in parallel linq to objects
  • comparing two custom objects to prevent duplicates
  • How to Print Text from ComboBox into a MessageBox
  • How can I do a Func<object[],Expression<Func<T,bool>>> dynamic?
  • C# SqlDataReader No data exists for the row/column
  • My code is not giving the desired output
  • Is it possible to have multiple MVC routes point to the same controller/view?
  • Updating Listbox results in " Invalid cross-thread access."
  • Finding Elbow Angle with Kinect
  • Host a mvc web api application in the sub-folder of a website
  • How to generate a sequential unique id in a thread-safe way
  • How to add a separator to a WPF combobox that is databound?
  • optimize linq query with related entities
  • Optimize this code for large input
  • Is there any way to clear all list box short way?
  • ACR122 Device Programming sample does not find reader
  • Parallel Library: does a delay on one degree of parallelism delay all of them?
  • How to convert a var which contains Long to an Long[]
  • Quickly prune and create valid data combinations
  • Get 3 parameters out of a Dictionary<string, Dictionary<string Action>>
  • Regex IsMatch taking too long to execute
  • Shuffling divs using C# behindcode
  • ASP.NET Project and IE10 Compatibility Mode issues in Windows 7
  • Why can't I loop through this dictionary?
  • RavenDB Stream for Unbounded Results - Connection Resilience
  • How to remove the focus border of a CheckBox in C# Visual Studio?
  • Exception while using String.Format "Index (zero based) must be greater than or equal to zero and less than the siz
  • Caching an LDAP connection
  • C# How to detect kill event sender
  • How to Display Splashscreen and login form at same time using c#
  • Read .sql script with parameters
  • Why would I need to compile/generate methods/code at runtime?
  • Unable to create a constant value of type 'Project.Models.Subcategory'. Only primitive types or enumeration types are su
  • How to run possibly synchronous or possibly asynchronous code?
  • Drop SQLite tables using list of tables names
  • Can I organize Git submodules in a flat hierarchy?
  • How to make a server reply after receiving a request/data
  • Stored Procedure works in DB but not in C# code
  • MVVM WPF Programmatically add Child Elements
  • NCrunch and PostSharp Logging clash
  • Increasing memory usage in socket client
  • Multiple DropDownList but same action
  • How to get application memory usage as shown in Task Manager?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org