LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python, find unique values in a tuple or dictionary (https://www.linuxquestions.org/questions/programming-9/python-find-unique-values-in-a-tuple-or-dictionary-807192/)

action_owl 05-11-2010 01:28 PM

Python, find unique values in a tuple or dictionary
 
Is there a python equivalent for the "array_unique" function in php?

I have to run a query on a very large table (1.5 million records, with no index!) and php can't beat the max execution time. So I am converting my php script to python, which I have never used before.

my php script basically does this:

gets results from database
gets the number of unique values in column "X"
counts how many times each unique value in column "X" occurs
echos the results

My php script was originally running on a smaller version of the table, I would have started in python if I would have known the "real" table was so large, python is the only language I have access to in my current environment (besides php).

Thanks

urban.yoga.journeys 05-11-2010 02:27 PM

from a spamfilter i wrote...
Code:

def word_count(s):
    """
    take a string and count the number of occurences of each word
    """
    word_rec = {}
    for word in s.split():
        word_rec[word.lower()] = 1 + word_rec.get(word.lower(), 0)
    return word_rec

the input is assumed to be a string, so if it's a list(read: array) or dictionary (hash table) values, you'll have to modify the code a bit.

the result is placed in a dictionary, the word acts as the key and the number of times it appears is the value.

hope this helps

bgeddy 05-11-2010 07:16 PM

A python set may be useful here. This little program counts unique occurrences in a tuple and puts those unique members and their corresponding counts into a dictionary.
Code:

mytuple=(10,4,2,3,10,6,8,5,2,5,4,4,10,1,9,2) #Create a sample tuple
mycntdict=dict([(i,mytuple.count(i)) for i in set(mytuple)]) #Fill dict
print "Tuple:",
print mytuple
print "Count dict:",
print mycntdict

Sets are useful for a simple way to get unique values. The list comprehension goes through the set members and counts their occurrrences in the original tuple and passes the resulting list as an argument to dict.


All times are GMT -5. The time now is 12:31 AM.