All Questions

Tagged with
Filter by
Sorted by
Tagged with
3 votes
2 answers
105 views

Sort a dict by keys, assuming keys are orderable

I found a suspicious code snipped in an existing project in which I am involved as a developer. The code sorts a dict by its keys, so that if iterated over the dict,...
3 votes
1 answer
88 views

Write a function that takes a list of dicts and combine them

There is an exercise in the book Python Workout: 50 Ten-minute Exercises that asks to write a function to do the following: Write a function that takes a list of dicts and returns a single dict that ...
3 votes
1 answer
150 views

Finger Exercise: Update book cipher by creating new book

I'm working my way through the finger exercises in John Guttag's book Introduction to Python Programming, third edition. The following finger exercise on page 143 describes encryption/decryption with ...
  • 133
4 votes
1 answer
39 views

Optimizing code for this function that filters a JSON file

I made a function that turns all child from a JSON file to parents (or brothers?). It adds the parent name to the key, so there are no duplicates. It works ok, but I would like to ask if there are ...
  • 41
3 votes
1 answer
65 views

Implementation of JSON schem parser to extract some attributes

I have three different types of schema possibilities as below: ...
  • 229
3 votes
1 answer
31 views

API request into a dictionary

I have the code below which currently pulls pricing data for Ethereum from CoinGecko API. From this request I am building a dictionary that contains the pieces of information and returning it to be ...
4 votes
1 answer
46 views

Implementation of building nested dictionary from list

I have a value test-1 and a list of keys as below: ...
  • 229
4 votes
1 answer
138 views

Implement a Python frozenmap

There are frozenset in Python, but no frozendict or frozenmap. I tried to implement it (Don'...
6 votes
2 answers
289 views

Porting PHP's array_push to Python

I am new to Python but not new to PHP. I've started porting PHP APIs to Python hoping to learn the Python language along the way, currently trying to grasp List, Dict, Set, Tuple. I tried writing PHP'...
1 vote
1 answer
107 views

Calculate session between in and out and total working hours of session

Is there any way to improve this code: punch:0 -> in punch:1 -> out ...
  • 29
1 vote
1 answer
295 views

Calculate working Hours in given shift time

This is simple program to calculate working hours in given shift.. It is also return user sessions and presence of user shift. while this works fine. I would like suggestion to improve it. ...
  • 29
3 votes
2 answers
612 views

Transforming a list of dictionaries into a dictionary with nested dictionaries

My code takes a list of dictionaries as an input. Each of these dictionaries contains a person identified by name and a region code ...
1 vote
1 answer
332 views

Fastest way to find common keys between many dicts and merge these dicts

I have 1000 dictionaries (grid_1, grid_2.... grid_1000) stored as pickle objects and one ...
  • 162
2 votes
2 answers
206 views

Extracting data from dict and processing it

Consider the following ...
  • 384
3 votes
1 answer
73 views

LetterDict, a dictionary whose keys must be lowercase letters [a-z]

Looking for any suggestions - alternative design, improvements to readability, improvements to the comments, etc. I implemented this letter dictionary to see if it would faster than a regular ...
  • 414
3 votes
1 answer
279 views

ZigZag Conversion Coding Challenge

This is my solution for ZigZag Conversion, a coding challenge from LeetCode. The challenge: Write a program that takes two parameters, a string and a number of rows, that interprets the string as if ...
4 votes
2 answers
292 views

Prioritise injured cats for treatment (dictionary/map search optimization Python and C++)

I am once again attempting another CP problem, and I have ONCE AGAIN run into an optimization issue because of the time limit of one second: https://open.kattis.com/problems/doctorkattis It is common ...
  • 141
3 votes
1 answer
538 views

Better way of comparing two lists of dictionaries and display unique changes

Can I get a review of my code which compares 2 dictionaries and display unique changes. ...
6 votes
1 answer
793 views

Python function to print a two-column table from a dictionary

I'm currently learning Python for a masters and am trying to improve the best I can, as I'll need to know how to code in my future career. I currently have a function that builds a table from a ...
10 votes
2 answers
5k views

Efficient renaming of dict keys from another dict's values - Python

I have a dictionary in Python (uglyDict) that has some really un-cool key names. I would like to rename them based on values from another dictionary (...
1 vote
1 answer
63 views

Hashing Table in Python

I just want your opinion about that code! :) ...
3 votes
0 answers
75 views

Compute damages and savings for motor insurance claims

I am working on some claims data, where I have features such as The descriptions of the columns are: (A) the year of claim, (B) who's the insuring company (C) whether the vehicle is driven, (D) ...
9 votes
3 answers
1k views

The cleanest way to print the max value and its key from a dictionary

I'm getting the highest value and its respective from a dictionary and printing it out for the user. Even though it's working, I'm wondering if I'm doing it in the cleanest way. Here it is: ...
  • 401
2 votes
1 answer
174 views

Implementing hash table in Python using Tuples

I am trying to implement a basic hash table using a list of tuples. The code produce the desired output. Please help me improve my code as I feel there are a lot of while loops. ...
3 votes
0 answers
507 views

3Sum - leetcode

Question: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set ...
3 votes
2 answers
112 views

Create a dictionary from a list of key and multi list of values in Python

I have a list of key: list_date = ["MON", "TUE", "WED", "THU","FRI"] I have many lists of values that created by ...
2 votes
0 answers
50 views

Global Dictionary Class

For a Django-Server I use several custom Error-Codes, error-titles and descriptions, possibly other fields might follow. I'm using right now a basic global dictionary. ...
  • 161
4 votes
1 answer
100 views

Data set with many variables in Python, many indented dictionaries?

I am working with a data set that has many variables. Currently I am storing the data in many indented dictionaries in the following way: ...
0 votes
1 answer
760 views

Nested dictionary creation with setdefault

Is there a better way of creating a nested dictionary than what I'm doing below? The result for the setdefault is because I don't know whether that particular key ...
  • 113
4 votes
1 answer
156 views

Increment value in a list of dictionaries

I have a list of dictionary let's say : l=[{'a':1,'b':2},{'a':5,'b':6},{'a':3,'b':2}] and I want to have another list l2 and add 4 to the 'a' in the all ...
4 votes
1 answer
114 views

Function for accessing and searching dictionaries using /slashed/paths (like xpath)

I have some data being returned to me in the form of a deeply nested Dict and I need to parse multiple values from it which may or may not be there. I saw the dpath ...
3 votes
0 answers
39 views

Converts Pandas and Numpy into Dictionary and Converts back to original format for data split

I'm trying to refactor some code so that it does not have so much repetition to it. What I'm trying to do is create an input for a multiple channel/input neural network. The features that are being ...
7 votes
2 answers
722 views

Number of occurrences of strings in array

This is a Hackerrank problem (https://www.hackerrank.com/challenges/sparse-arrays/problem). We're given an array of strings and an array of queries, and have to return an array of the number of ...
3 votes
2 answers
275 views

Handle missing children nodes when parsing XML into a dictionary

I work on a code-base that uses xml to set up problems and specify model parameters. I've created a script that I run in tandem with our code. This script will ...
  • 225
9 votes
2 answers
15k views

first pizza order program

Trying to learn a new skill at 40 without any prior related experience :-) Bought the book 'Python crash course 2e' and decided to freewheel a bit with after a week with what I've learned so far.. I ...
2 votes
2 answers
181 views

Finding unique keys

I made the following snippet. It finds unique keys based on their values. For all keys \$s\$ that are contained in another key \$D\$ with the same value, key \$s\$ is discarded and key \$D\$ is ...
4 votes
1 answer
333 views

Add lines from files to corresponding dictionaries

I have to iterate through multiple text files. For each file I read its contents and append each line to its corresponding dictionary to then build a JSON file. Each text file has the following ...
  • 41
5 votes
1 answer
137 views

python3 function to merge dictionaries

I have a bunch of JSON records, each record with a varying degree of completeness. In other words, record A may contain keys not in record B, and vice-versa. To get a better understanding of the data ...
5 votes
2 answers
365 views

Can it be shorter?: DNA Sequence Analyzer from CS50 written in Python

This is my first time requesting a code review. This is code I wrote in Python to create a DNA sequence analyzer. This is for the Harvard CS50 course. I was really hoping to get some constructive ...
  • 61
1 vote
1 answer
166 views

Designing a hashset with LinkedLists as buckets

I am solving a problem on Leetcode that requires me to design a hash set. I intend to solve it using linked lists where each linked list would represent a bucket. I am intending to implement this: ...
3 votes
1 answer
290 views

Add values from a dictionary to subarrays using numpy

I'm working with numpy arrays and dictionaries, the keys of the dictionary are coordinates in the numpy array and the values of the dictionary are a list of values I need to add in those coordinates, ...
2 votes
1 answer
69 views

Updating a dictionary of trajectories after a step has been made

I am using a dictionary to save possible trajectories in a game. A trajectory is defined as a list of numbers separated by _. For example ...
  • 143
3 votes
1 answer
255 views

Function takes a dictionary with list as value, returns the key of the biggest list

I created a function that takes a dictionary as parameter. This dictionary is expected to have lists as value pair. (i.e. {1: [1, 2, 3], 'a': [4, 'b', 5, 'c']} ) ...
1 vote
1 answer
69 views

Unscramble words faster in Python

So I'm currently using python to unscramble words that are retrieved from an OCR program, and checked in the 'dictionary'. The current code I am using to unscramble words is this: ...
  • 11
-2 votes
1 answer
78 views

Sorting through two dictionaries [closed]

What my code try to do here is to sort items trough 2 dictionaries. If they are similar or the same, I create a new list to append them. The test file is relatively big, around 2000 data each ...
  • 1
3 votes
1 answer
313 views

Implement hash table using linear congruential probing in python

I have just read the Chapter 5 of Data Structures and Algorithms with Python. The authors implemented hash sets using linear probing. However, linear probing may result in lots of clustering. So I ...
1 vote
2 answers
421 views

HashMap Implementation (dictionary) in Python

I attempted making a hashmap in python, and it was harder, due to some limitations but this is my version of dictionaries in python. Are there any way to simplify or do the same thing in less code ...
  • 1,204
-1 votes
1 answer
46 views

Best way of converting a dynamic number of list items into variables in Python [closed]

I have a simple scrapy spider that crawls a page and returns H1 on the pages. Since, each page is unique, one cannot know how many h1's will be on the page. Since the scrapy spider returns a list, I ...
  • 103
3 votes
2 answers
638 views

Best approach for converting list of nested dictionaries to a single dictionary with aggregate functions

I've looked through a lot of solutions on this topic, but I have been unable to adapt my case to a performant one. Suppose I have a list of dictionaries stored as: ...
3 votes
1 answer
267 views

Is this an optimal implementation for the knapsack algorithm?

I have implemented a solution for the knapsack problem while breaking an item is allowed. I've used the dictionary data structure where I used weight as a key and value per weight as a value. After ...

1
2 3 4 5 6