Concatenating dictionary values and keys, and further operations

Gerard Flanagan grflanagan at yahoo.co.uk
Mon Jun 5 15:02:06 EDT 2006


Girish Sahani wrote:
> I wrote the following code to concatenate every 2 keys of a dictionary and
> their corresponding values.
> e.g if i have tiDict1 = tiDict1 = {'a':[1,2],'b':[3,4,5]} i should get
> tiDict2={'ab':[1,2][3,4,5]} and similarly for dicts with larger no. of
> features.
> Now i want to check each pair to see if they are connected...element of
> this pair will be one from the first list and one from the second....e.g
> for 'ab' i want to check if 1 and 3 are connected,then 1 and 4,then 1 and
> 5,then 2 and 3,then 2 and 4,then 2 and 5.
> The information of this connected thing is in a text file as follows:
> 1,'a',2,'b'
> 3,'a',5,'a'
> 3,'a',6,'a'
> 3,'a',7,'b'
> 8,'a',7,'b'
> .
> .
> This means 1(type 'a') and 2(type 'b') are connected,3 and 5 are connected
> and so on.
> I am not able to figure out how to do this.Any pointers would be helpful


Girish

It seems you want the Cartesian product of every pair of lists in the
dictionary, including the product of lists with themselves (but you
don't say why ;-)).

I'm not sure the following is exactly what you want or if it is very
efficient, but maybe it will start you off.  It uses a function
'xcombine' taken from a recipe in the ASPN cookbook by David
Klaffenbach (2004).

(It should give every possibility, which you then check in your file)

Gerard

-------------------------------------------------------------------------

def nkRange(n,k):
    m = n - k + 1
    indexer = range(0, k)
    vector = range(1, k+1)
    last = range(m, n+1)
    yield vector
    while vector != last:
        high_value = -1
        high_index = -1
        for i in indexer:
            val = vector[i]
            if val > high_value and val < m + i:
                high_value = val
                high_index = i
        for j in range(k - high_index):
            vector[j+high_index] = high_value + j + 1
        yield vector

def kSubsets( alist, k ):
    n = len(alist)
    for vector in nkRange(n, k):
        ret = []
        for i in vector:
            ret.append( alist[i-1] )
        yield ret

data = { 'a': [1,2], 'b': [3,4,5], 'c': [1,4,7] }

pairs =  list( kSubsets(data.keys(),2) ) + [ [k,k] for k in
data.iterkeys() ]
print pairs
for s in pairs:
    for t in xcombine( data[s[0]], data[s[1]] ):
        print "%s,'%s',%s,'%s'" % ( t[0], s[0], t[1], s[1] )


-------------------------------------------------------------------------

1,'a',1,'c'
1,'a',4,'c'
1,'a',7,'c'
2,'a',1,'c'
2,'a',4,'c'
2,'a',7,'c'
1,'a',3,'b'
1,'a',4,'b'
1,'a',5,'b'
2,'a',3,'b'
2,'a',4,'b'
2,'a',5,'b'
1,'c',3,'b'
1,'c',4,'b'
1,'c',5,'b'
4,'c',3,'b'
4,'c',4,'b'
4,'c',5,'b'
7,'c',3,'b'
7,'c',4,'b'
7,'c',5,'b'
1,'a',1,'a'
1,'a',2,'a'
2,'a',1,'a'
2,'a',2,'a'
1,'c',1,'c'
1,'c',4,'c'
1,'c',7,'c'
4,'c',1,'c'
4,'c',4,'c'
4,'c',7,'c'
7,'c',1,'c'
7,'c',4,'c'
7,'c',7,'c'
3,'b',3,'b'
3,'b',4,'b'
3,'b',5,'b'
4,'b',3,'b'
4,'b',4,'b'
4,'b',5,'b'
5,'b',3,'b'
5,'b',4,'b'
5,'b',5,'b'




More information about the Python-list mailing list