[Tutor] Finding all locations of a sequence
Luke Paireepinart
rabidpoobear at gmail.com
Wed Jun 27 23:48:42 CEST 2007
Lauren wrote:
> Firstly, I'd like to thank everyone for their help. I ended up
> throwing something together using dictionaries (because I understood
> those best out of what I had), that was a lot faster than my initial
> attempt, but have run into a different problem, that I was hoping for
> help with. So, what I have is all the subsequences that I was looking
> for in separate entries in the dictionary, and where each of them is
> found as the value. If a subsequence binds to more than one other
> item, I want to have the locations of the items all together.
> The closest I've been able to manage to get to what I want is this:
>
> dict_of_bond_location = {}
> dict1 = {'AAA':['UUU'], 'AAU':['UUG', 'UUA'], 'AAC':['UUG'],
> 'AAG':['UUC', 'UUU'], 'CCC':['GGG']}
> dict2 = {'AAA':[1], 'AAU':[2], 'AAC':[3], 'AAG':[0, 4], 'GGG':[10]}
> dict3 = {'UUU':[3, 5], 'UUG':[0], 'UUA':[1], 'UUC':[2], 'GGG':[14]}
>
>
> for key in dict2:
> if key in dict1:
> matching_subseq = dict1.get(key)
> for item in matching_subseq:
> if item in dict3:
> location = dict3.get(item)
> dict_of_bond_location.setdefault(key,
> []).append(location)
> print dict_of_bond_location
>
> which gives this:
> {'AAU': [[0], [1]], 'AAG': [[2], [3, 5]], 'AAA': [[3, 5]], 'AAC': [[0]]}
>
> but what I want is
> 'AAU':[0, 1], 'AAG':[2, 3, 5], 'AAA':[3. 5], 'AAC':[0]
>
> the setdefault(key, []).append(location) thing sort of does what I
> want, but I don't want the result to be a list of lists...just one big
> list. The production of a new dictionary is not necessary, but it made
> sense to me a few hours ago. Anyway, is there a fast and dirty way to
> add lists together, if the lists are not named (I think that's
> essentially what I want?)
Lauren,
Try this:
>>> x = ['a']
>>> y = x.extend(['b','c'])
>>> x
['a', 'b', 'c']
But note that
>>> print y
None
Because the extend method doesn't return anything.
HTH,
-Luke
More information about the Tutor
mailing list