Questions tagged [dictionary]

A dictionary, in computer science, is a data structure that maps keys to values such that given a key its corresponding value can be efficiently retrieved.

Filter by
Sorted by
Tagged with
55
votes
2answers
3k views

Counting all the vowels in a passage of text

My son just started high school and in Math, he was asked to do some stats homework. The exercise was to count all the vowels in a passage of text, then answer some questions about frequency. Out of ...
24
votes
4answers
40k views

Type-safe Dictionary for various types

Assume the following situation: you have an object that can store any object based on a key (basically, IDictionary<string, object>). You want to store ...
17
votes
1answer
549 views

Python Dictionary Black Magic

I am defining a subclass of a the python dictionary object mpCmd where every item is converted to a lambda. The intended usage is that every item in the dictionary ...
15
votes
1answer
358 views

Persisted Dictionary Implementation

I'm using this code in an app that has hundreds of daily users. The code works, and passes all of the test cases I have. Occasionally some users report a problem that I don't get right to the bottom ...
14
votes
4answers
38k views

Get value from dictionary given a list of nested keys

I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
13
votes
2answers
2k views

Count the occurrence of each unique word in the file

I've been doing a task for an interview that I will have soon. The requirement is to code the following problem in C#. Write a program (and prove that it works) that: Given a text file, count the ...
13
votes
4answers
12k views

Using a Dictionary to retrieve BOOL values

I have some values pulled from a web service giving me a bool value for a key. I did it this way, but feel like it is repeating without need. How can I refactor this? Please note that the key on ...
13
votes
3answers
60k views

Set a default if key not in a dictionary, *or* value is None [closed]

Let's say I have a key 'messages' that is usually a list, but might be None, or might not be present. So these are all valid inputs: ...
12
votes
4answers
1k views

Extraction of value from a dict-tree, given a path of arbitrary length

Problem: We read a complex json-file which grants us a tree in terms of a python "dict of dicts". We're then requested to retrieve a specific path from the tree, represented by a list of elements. The ...
12
votes
3answers
3k views

Codewars: Reduce strings to one character

I am trying to solve a puzzle in a performant way. This is the puzzle. Simply explained: you have a String and you must reduce it to one character with the given rules. This is the solution to the ...
12
votes
3answers
8k views

Creating zip but for dictionaries

I found myself needed zip but for dictionaries, and wanted to know if there is any way I can make this shorter and more concise, and/or faster. Also I cannot figure out what the type hinting might be ...
12
votes
3answers
557 views

Key Order Agnostic, 2D Dictionary

Note: I am using Unity3D game engine, which uses .Net 2.0 (.Net 2.0.50727.1433 to be precise) to compile code, so I don't have access to newest features of C# 6, or even C# 5. Please keep this in ...
12
votes
1answer
21k views

Adjacency List Graph representation on python

I began to have my Graph Theory classes on university, and when it comes to representation, the adjacency matrix and adjacency list are the ones that we need to use for our homework and such. At the ...
12
votes
3answers
3k views

Create dictionary with default immutable keys

I've created a dictionary subclass with a set of default immutable keys. ...
12
votes
1answer
6k views

Simple trie implementation in JavaScript

After experimenting with a sorted trie implementation in C, I felt that I understood tries pretty well, but was having trouble explaining how they work. Since the C code was based on existing code, I ...
11
votes
3answers
4k views

Removing entries from a dictionary containing bad words

I have a dictionary with each item containing a quote in another dictionary: ...
11
votes
3answers
1k views

Implementing a rectangle class

First I've to indicate that this is my homework. An example of a given input is: s1 = {'p1': (x1, y1), 'p2': (x2, y2)} where all names and coordinations are ...
11
votes
6answers
16k views

“Multi-key” dictionary

I was making a relatively large-scale project in Python as a learning experiment, and in the process I found that it would be useful to have a map (or, in Python, I guess they're called dictionaries) ...
11
votes
3answers
288 views

Conditionally set attributes in MongoDB based on form fields

I have code which gets data from a POST request (request.form dict), compares with corresponding attribute in object ...
11
votes
2answers
4k views

Invert a dictionary to a dictionary of lists

I have a dictionary whose keys are related to list of values, but any value may be related to many keys, hence be contained in various lists-attached-keys. To highlight such relation; I build (in ...
11
votes
1answer
637 views

iterating over the values of a list of ordered dictionaries

I am facing the following problem: I have a special data structure that is a dictionary whose keys are integers (dimensions). The values are also dictionaries whose keys are strings (geometric types) ...
11
votes
1answer
1k views

Enumerable Custom Collections in VBA with Dictionary features like Exists

To Collect or Hash The VBA.Collection has a number of limitations, but it is enumerable and you can refer to items by index or key. But the VBA implementation of a ...
11
votes
1answer
431 views

OOP paradigm implementation of a Dictionary data model

Here is the implementation of interface Dictionary using chained hash table class HashTableChained. Despite item 22* saying ...
10
votes
4answers
29k views

Dictionary GetValueOrDefault

I'm pretty sure there's no better way to do this but I thought a consensus on here might be nice. Let me know what you think. ...
10
votes
3answers
64k views

Convert dict entries to sets and lists

Can you please tell me if there is a better way to do this? ...
10
votes
2answers
683 views

Multi Key dictionary implementation

Description I had to create a dictionary structure where one value can be referenced by several keys. For example the keys a, b...
10
votes
5answers
2k views

Simple word counter

I've decided to do this by writing a simple word counter. The app gets all the params and outputs all the unique words, each one with a counter: "Hello world Hello" would return "Hello: 2", "world: ...
10
votes
2answers
1k views

Better way to iterate through a dictionary and comparing its keys with itself

I have a dictionary with author names as keys and values of None. I want to use fuzzywuzzy ...
10
votes
2answers
796 views

Finding roughly matching genome sequences in Python dictionary

The purpose of my code here is to play a part in genome sequencing analysis, and while functional it takes days to run, so I am looking for any way I can improve speed. The input is up to 500 million ...
10
votes
1answer
2k views

Multi-dictionary API using Python Flask-RESTful

I'm working on a simple online dictionary tool, using Python Flask-RESTful as the RESTful API backend. The dictionary tool is modular, it can handle multiple dictionaries, implemented as independent ...
10
votes
1answer
147 views

Using generators to print the tree-like structure of a project

The goal of this code is to print the entity tree of a VHDL project. There are a readme and very minimal tests on the github repo. I am trying to refactor the code to use generators in order to ...
9
votes
10answers
3k views

Writing an anagram efficiently

I know this question has been asked before now but in Python. Recently, I set out to write an anagram in C#. For instance, orchestra can be rearranged into carthorse and the anagram should not have ...
9
votes
5answers
288 views

Applying a string-identified effect on an image

Task: Given an image object and an effect identifier (string), apply the effect identified by the string on the image. The library of effects can grow as the program grows. Code 1: ...
9
votes
3answers
4k views

Dictionary of dictionary - Get 2nd keys

I have a dictionary of dictionary such as d below, and I would like to get the list of all keys but not on the first level (i.e. ...
9
votes
1answer
2k views

Extending CGPoint to conform to Hashable

For Advent of Code Day 1, I found myself needing to use a tuple as a key in a dictionary. Seeing that I can't extend tuples in Swift, I decided to extend CGPoint ...
9
votes
1answer
3k views

Product of dictionary values

Elements that smell funny: argument unpacking to itertools.product. I'm needing sorted keys (even though I don't care about key order in the final result). ...
9
votes
1answer
143 views

Convert to dictionary

Earlier I did post with full code about Prepare and export data to MS Word file. The user t3chb0t recommended me to separate some part of full code to this post. Context The task of code for review ...
8
votes
5answers
2k views

Bidirectional Dictionary

The management of bidirectional mappings is a reoccuring topic. I took the time to write an (hopefully) efficient implementation. ...
8
votes
3answers
707 views

Creating an affinity-matrix between protein and RNA sequences

I wrote an algorithm that analyzes protein-RNA interactions and I found that the following function is the bottleneck that causes performance issues: ...
8
votes
4answers
7k views

Iterate over two collections of equal length

What's the best way to iterate over two collections of equal length? Currently I'm using this: ...
8
votes
7answers
5k views

Any way of simplifying the following two short snippets regarding Dictionary?

Example 1 if (myList.ContainsKey(myKey)) { myList[myKey]++; } else { myList.Add(myKey,1); } Example 2 ...
8
votes
3answers
962 views

Python program to find the most frequent letter in a text

Given a text like "Hello World!" the program outputs the most frequent letter, in this case the "l". If there are two letters with the same frequency, it outputs the first in alphabetical order, for ...
8
votes
3answers
2k views

Word-counting script in C#

My goal is to read from standard input, break up the input into words (Unicode letters and apostrophes), case-insensitively, and produce a report to standard output, where each line is a word followed ...
8
votes
2answers
23k views

Extracting values from dictionaries where the keys match

I have two Dictionaries with around 65,000 KeyValuePairs each. I used foreach and if-else statements to compare them and get values, but it goes very slow. How could I optimize my code and gain more ...
8
votes
2answers
1k views

Brick Wall Leetcode challenge

I solved this problem on Leetcode There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to ...
8
votes
3answers
6k views

Scrabble algorithm review and performance suggestions

After finishing Project Euler 24, I decided to make a scrabble type of application using /usr/lib/dict as my dictionary what I cat that into a word file. It does take a few seconds, and the dictionary ...
8
votes
3answers
165 views

Collect and calculate average times from log, then display top 10 longest durations

Here's a novel-length summary of the issue: I'm trying to write a VB.net program to help me collect remote site statistics from system-generated logs, but I'm a little like a carpenter who only knows ...
8
votes
2answers
10k views

List all possible permutations from a python dictionary of lists

The following code takes a dictionary of lists (indexed by keyword) and converts it into a list of list of dictionaries that contains all possible permutations of those lists. However, my solution ...
8
votes
2answers
839 views

Looking for primes with all-different digits

I recently answered a question on Mathematics Stack Exchange, where I used a small Python program to check the following : For all 6-digit numbers, with all digits being different, choosing the ...
8
votes
2answers
2k views

Copying a Dictionary and Replace its Values

I have a custom dictionary where the values are numpy arrays. I would like to have a convenience function that creates a copy of its content except that the values are changed. If no value is passed, ...

1
2 3 4 5
7