Reputation: 39
I'm trying to develop a program where the users inputs a string of nucleotides and is given the complementary string (CAGT become GTCA, for example).
I've set up a dictionary:
dicto = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
And I'm inputting user input in the form of a string to cross-reference with dictionary keys:
user = raw_input(prompt)
But, despite searching, I can't figure out how to how to use the multiple characters input by the user against the dictionary. I imagine it will be something like the below:
print dicto[x for x in user]
Any help is welcome.
Upvotes: 0
Views: 498
Reputation: 121
For this, using str.translate
would be appropriate. Like so:
#!python2
import string
trans = string.maketrans("ACTG", "TGAC")
user = raw_input()
print user.translate(trans)
#!python3
trans = str.maketrans('ACTG', 'TGAC')
user = input()
print(user.translate(trans))
Upvotes: 3
Reputation: 31
You can use
print("".join([dicto[x] for x in user]))
What this does: Create a list with the right base pairs, looked up in dicto:
[dicto[x] for x in user]
And feed this list to "".join()
which creates a single string, by inserting the string it is called on between all List elements (empty string here, space is a common option)
Upvotes: 0
Reputation: 532418
You can use operator.itemgetter
.
>>> dicto = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
>>> user = 'ACTG'
>>> from operator import itemgetter
>>> itemgetter(*user)(dicto)
('T', 'G', 'A', 'C')
>>> ''.join(_)
'TGAC'
Upvotes: 0