1
vote
1answer
70 views

Modifying nested dictionaries in Python

My task was to take a configuration specified as a dictionary and run some function with it (the actual function is not relevant here). After implementing that, I was asked to allow some "sweeping" of ...
7
votes
1answer
81 views

Is this implementation of persistence OK?

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 ...
1
vote
2answers
284 views
-2
votes
1answer
44 views

Optimizimg ugly if else loop in python [closed]

So, I`m getting some values from large nested dictionary. s_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions'] and f_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions'] ...
1
vote
2answers
60 views

Validate fields in Python dictionary using dictionary of lambdas

I would love some feedback on this. I use it to validate web-form objects before persisting them in the dangerously-lazy MongoDb. I have a class and many validators for each of my collections. Is ...
0
votes
1answer
34 views

For and if loops in Python math game [closed]

I'm trying to get my program to iterate through a loop after a person gets the correct answers to the first question. I feel like there is a better way to do things syntactically. ...
13
votes
1answer
185 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 ...
2
votes
3answers
70 views

How can I optimize this hash string function?

I'm writing a function which gets two strings and a number k as an input, and outputs a string with length k which is present in ...
2
votes
2answers
53 views

Managing and searching objects using tags

I wonder Is it appropriate to hide imported classes (collections and UserDict in this case) from Python IDE (e.g. IPython)? Is there a more efficient algorithm/implementation? Please feel free to ...
5
votes
3answers
189 views

Is there a simpler way to find winners when there is a tie?

Background: I have a Python script which parses a complicated CSV generated on election nights. Each row of the CSV represents a race. As I loop through the races, I store the candidates for each ...
3
votes
1answer
52 views

Building a dict/list of lists

I have code which is matching features within regions. The first thing it does it matches features from one region to an entire database of region features. I have a mapping from query feature index ...
4
votes
1answer
61 views

Output of 'ldd' to dictionary

I want to use the output of the terminal command ldd: ...
5
votes
2answers
91 views

Geometry table for use in regexes

I have been teaching myself Python 3. I watched a video on YouTube entitled "stop writing classes". The Gtable class doesn't even have an init function, so I can't help but wonder if I should have ...
3
votes
2answers
106 views

Word analysis on input from a file

How do I improve the below code, which takes input from a file and does some word analysis? I find a lot of redundancy in it. I want to know a better way of implementing it. ...
2
votes
1answer
53 views

Scraping thefreedictionary.com

Scrape results from thefreedictionary.com ...
2
votes
1answer
109 views

Readability concerns with literal dictionary lookup vs if-else

I'm having concerns about readability of this piece of code: messages_dict = {'error':errors, 'warning':warnings}[severity] messages_dict[field_key] = message ...
10
votes
3answers
133 views

Refactoring if-else structure with elements of dict

I have code which gets data from a POST request (request.form dict), compares with corresponding attribute in object ...
5
votes
1answer
82 views

Load drinks and their ingredients into dictionaries

I'm working on a web application using bottle, and it is, at this point, functional. The gist of it is that I have a database of drinks, with associated IDs, and associated sets of ingredients. the ...
6
votes
5answers
943 views

Holding records using dictionary

Can you please help me with the following script? At the moment the script is taking up to 20 mins to execute, depending on the amount of data being processed (each time the script is executed, it ...
2
votes
2answers
86 views

Special dictionary

I need a special dictionary that would do as follows: if key is set to more than once, the values are a list of all set values. Such a list is returned as value on get. if key is set only once, it ...
3
votes
1answer
309 views

Recursively convert a list of lists into a dict of dicts

As a challenge to myself, I wrote a solution to this Stack Overflow question. For completeness, the bulk of the question is as follows: Problem Input: ...
3
votes
1answer
140 views

Prime number dictionary

I attempted to write a read-only dictionary, containing primes: ...
0
votes
1answer
555 views

opening a text file and building a dictionary

Need to write a function that takes an open file as the only parameter and returns a dictionary that maps a string to a list of strings and integers. each line in the text will have a username, ...
2
votes
2answers
175 views

Python code snippet could probably be more elegant

I'm looking to let people define arbitrarily nested lists of lists and return a random json document for them. Right now the assumption is that if you have a structure like this: ...
2
votes
3answers
3k views

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

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: ...
1
vote
2answers
219 views

Making a 2D dict readable

I am building a 2d dictionary with an altered list at the end Afterwords I want to find the min of each list. Is there a better way to do it? Better being faster or at least less of an eyesore. ...
1
vote
3answers
181 views

A Better For Loop

I'm very new to Python, and have a feeling that there's a much better way to implement the following code. It works, I'd just like to clean it up a bit. The purpose is just to create a dictionary with ...
1
vote
3answers
103 views

Updating list of Dictionaries from other such list

Basically I have created a function which takes two lists of dicts, for example: oldList = [{'a':2}, {'v':2}] newList = [{'a':4},{'c':4},{'e':5}] My aim is to ...
1
vote
3answers
82 views

update list based on other values

Basically I have a 2d list containing movies. Movies are specified by name, version and description. For example ['Jaws', 1, 'Movie about sharks'] - where ...
2
votes
1answer
218 views

Iterating X times (where X > Y) over Y element dictionary - is there a nicer way of doing this?

I need to write a procedure, that will take a dictionary consisting of server names and their available slots and a number of slots required, and redispatch it over the servers, so that every server ...
3
votes
2answers
570 views

Refactor deeply nested if-else

I found a code on my computer that i wrote a while ago. It is based on an exercise from O'Reilly book Programming the Semantic Web. There is a class, that stores RDF triples, that is data in the form ...
5
votes
2answers
537 views

Convert dictionary of lists of tuples to list of lists/tuples in python

I've got dictionary of lists of tuples. Each tuple has timestamp as first element and values in others. Timestamps could be different for other keys: ...