dictionary

Python - Most efficient way to find how often each possible pair of words occurs in the same line in a text file?

Hi all, This particular problem is easy to solve, but I'm not so sure that the solution I'd arrive at would be computationally efficient. So I'm asking the experts! What would be the best way to go through a large file, collecting stats (for the entire file) on how often two words occur in the same line? For instance, if the text cont...

Searching a python list quickly?

I have a dictionary and a list. The list is made up of values. The dictionary has all of the values plus some more values. I'm trying to count the number of times the values in the list show up in the dictionary per key/values pair. It looks something like this: for k in dict: count = 0 for value in dict[k]: if value in list: ...

General purpose open source taxonomy database

I am looking for a database which could help me group thousands of English-language keywords to few general disciplines. For example: I HAVE THIS => I WANT TO HAVE THIS cat => animal chair => household wine => drink deer => animal beer => drink glass => household, drink total 50 000 keywords => total <100 disciplines I guess that orga...

Rotate dictionary keys in python

I have a dictionary with several values that I want to keep constant, but I need to rotate them throughout the different keys. Is there a built in function or external library that would be able to do this or would I be better off just writing the entire thing myself? Example of what I am trying to do: >>> firstdict = {'a':'a','b':'b',...

Multi Value Dictionary?

Anyone know of a good implementation of a MultiValueDictionary? Basically, I want want something that allows multiple values per key. I want to be able to do something like dict.Add(key, val); And if the key doesn't already exist, it will add it, if it does, it will just add another value to that key. I'm just going to iterate over it...

LINQ Query on ConcurrentDictionary of 19710 items slow

Hi All I have the following method: /// <summary> /// Calculates the last trade date. /// </summary> /// <param name="tradesDictionary">The trades dictionary.</param> /// <returns>The last trade date.</returns> public DateTime CalculateLastTradeDate(ConcurrentDictionary<Guid, TradeRecord> tradesDictionary) {...

How to distinguish between a sequence and a mapping

I would like to perform an operation on an argument based on the fact that it might be a map-like object or a sequence-like object. I understand that no strategy is going to be 100% reliable for type-like checking, but I'm looking for a robust solution. Based on this answer, I know how to determine whether something is a sequence and I ...

Reversing dictionary by key doesn't work

I have a following dictionary : {2009: [12, 11, 10, 9], 2010: [1]} I'm trying to reverse-sort it, so that 2010 comes first. Here's the code : def dictSort(dict): items = dict.items() items.sort(reverse=True) dict = {} for item in items: dict[item[0]] = item[1] return dict But in return I get the same dicti...

Can I safely use hashes of tuples containing 64 bit integers (longs) as unique keys in a python dictionary?

I have to store objects that have two attributes (ida and idb) inside a dict. Both attributes are 64 bit positive integers and I can only store one object for a unique arrangement(combination in which the order matters) of ida and idb. For example: obj1 = SomeClass(ida=5223372036854775807, idb=2) obj2 = SomeClass(ida=2, idb=522337203685...

Adding or merging python dictionaries without loss

I'm trying to count up ip addresses found in a log file on two servers and then merge the dictionary stats together without loosing elements or counts. I found a partial solution in another stack overflow question but as you can see it drops the '10.10.0.1':7 pair. >>> a = {'192.168.1.21':23,'127.0.0.1':5,'12.12.12.12':5,'55.55.55.55':1...

How to discover what a Postgresql table's usage is?

Our team is working on a Postgresql database with lots of tables and views, without any referential constraints. The project is undocumented and there appears to be a great number of unused/temporary/duplicate tables/views dirtying the schema. We need to discover what database objects have real value and are actually used and accessed. ...

how do I create a dictionary with keys from a list and values defaulting to (say) zero?

I have a = [1,2,3,4] and I want d = {1:0, 2:0, 3:0, 4:0} d = dict(zip(q,[0 for x in range(0,len(q))])) works but is ugly. what's a cleaner way? ...

Combinations from dictionary with list values using Python

I have the following incoming value: variants = { "debug" : ["on", "off"], "locale" : ["de_DE", "en_US", "fr_FR"], ... } I want to process them so I get the following result: combinations = [ [{"debug":"on"},{"locale":"de_DE"}], [{"debug":"on"},{"locale":"en_US"}], [{"debug":"on"},{"locale":"fr_FR"}], [{"debug":"off"},{...

Determine if a dice roll contains certain combinations?

I am writing a dice game simulator in Python. I represent a roll by using a list containing integers from 1-6. So I might have a roll like this: [1,2,1,4,5,1] I need to determine if a roll contains scoring combinations, such as 3 of a kind, 4 of a kind, 2 sets of 3, and straights. Is there a simple Pythonic way of doing this? I've...

Parse dict of dicts to string

I have a dictionary with following structure : {1: {'message': u'test', 'user': u'user1'}, 2: {'message': u'test2', 'user': u'user2'}} I'd like to create a string containing values from the inner dictionary in this form : string = "<span>test1</span><span>user1</span><br /> <span>test2</span>..." I've tried everything fro...

Python - Fastest way to find the average value over entire dict each time it gets modified?

I'm trying to find the fastest/most efficient way to extract the average value from a dict. The task I'm working on requires that it do this thousands of times, so simply iterating over all the values in the dict each time to find the average would be entirely inefficient. Hundreds and hundreds of new key,value pairs get added to the dic...

Dictionaries in Python

I have a problem. I want to make a dictionary that translates english words to estonian. I started, but don't know how to continue. Please, help. Dictionary is a text document where tab separates english and estonian words. file = open("dictionary.txt","r") eng = [] est = [] while True : lines = file.readline() if lines == "...

Check if value already exists within list of dictionaries?

I don't think this question has been asked in this form on SO before. I've got a Python list of dictionaries, as follows: a = [{ 'main_color': 'red', 'second_color':'blue'}, { 'main_color': 'yellow', 'second_color':'green'}, { 'main_color': 'yellow', 'second_color':'blue'}] I'd like to check whether a dictionary with a particular ke...

arabic dictionary database

hi! who can give me a way to get a database of an arabic dictionary? thks ...

What is better in python, a Dictionary or Mysql ?

What would be faster ? Query mysql to see if the piece of information i need is there, OR load a python dictionary with all the information then just check if the id is there If python is faster, then whats the best what to check if the id exists? Im using python 2.4.3 Im searching for data which is tagged to a square on a board, im s...