eliminate '\n' in dictionary - python

I have a dictionary looks like this, the DNA is the keys and quality value is value:
{'TTTGTTCTTTTTGTAATGGGGCCAGATGTCACTCATTCCACATGTAGTATCCAGATTGAAATGAAATGAGGTAGAACTGACCCAGGCTGGACAAGGAAGG\n':
'eeeecdddddaaa`]eceeeddY\\cQ]V[F\\\\TZT_b^[^]Z_Z]ac_ccd^\\dcbc\\TaYcbTTZSb]Y]X_bZ\\a^^\\S[T\\aaacccBBBBBBBBBB\n',
'ACTTATATTATGTTGACACTCAAAAATTTCAGAATTTGGAGTATTTTGAATTTCAGATTTTCTGATTAGGGATGTACCTGTACTTTTTTTTTTTTTTTTT\n':
'dddddd\\cdddcdddcYdddd`d`dcd^dccdT`cddddddd^dddddddddd^ddadddadcd\\cda`Y`Y`b`````adcddd`ddd_dddadW`db_\n',
'CTGCCAGCACGCTGTCACCTCTCAATAACAGTGAGTGTAATGGCCATACTCTTGATTTGGTTTTTGCCTTATGAATCAGTGGCTAAAAATATTATTTAAT\n':
'deeee`bbcddddad\\bbbbeee\\ecYZcc^dd^ddd\\\\`]``L`ccabaVJ`MZ^aaYMbbb__PYWY]RWNUUab`Y`BBBBBBBBBBBBBBBBBBBB\n'}
I want to write a function so that if I query a DNA sequence, it returns a tuple of this DNA sequence and its corresponding quality value
I wrote the following function, but it gives me an error message that says list indices must be integers, not str
def query_sequence_id(self, dna_seq=''):
"""Overrides the query_sequence_id so that it optionally returns both the sequence and the quality values.
If DNA sequence does not exist in the class, return a string error message"""
list_dna = []
for t in self.__fastqdict.keys():
list_dna.append(t.rstrip('\n'))
self.dna_seq = dna_seq
if self.dna_seq in list_dna:
return (self.dna_seq,self.__fastqdict.values()[self.dna_seq + "\n"])
else:
return "This DNA sequence does not exist"
so I want something like if I print
query_sequence_id("TTTGTTCTTTTTGTAATGGGGCCAGATGTCACTCATTCCACATGTAGTATCCAGATTGAAATGAAATGAGGTAGAACTGACCCAGGCTGGACAAGGAAGG"),
I would get
('TTTGTTCTTTTTGTAATGGGGCCAGATGTCACTCATTCCACATGTAGTATCCAGATTGAAATGAAATGAGGTAGAACTGACCCAGGCTGGACAAGGAAGG',
'eeeecdddddaaa`]eceeeddY\\cQ]V[F\\\\TZT_b^[^]Z_Z]ac_ccd^\\dcbc\\TaYcbTTZSb]Y]X_bZ\\a^^\\S[T\\aaacccBBBBBBBBBB')
I want to get rid of "\n" for both keys and values, but my code failed. Can anyone help me fix my code?

The newline characters aren't your problem, though they are messy. You're trying to index the view returned by dict.values() based on the string. That's not only not what you want, but it also defeats the whole purpose of using the dictionary in the first place. Views are iterables, not mappings like dicts are. Just look up the value in the dictionary, the normal way:
return (self.dna_seq, self.__fastqdict[self.dna_seq + "\n"])
As for the newlines, why not just take them out when you build the dictionary in the first place?

To modify the dictionary you can just do the following:
myNewDict = {}
for var in myDict:
myNewDict[var.strip()] = myDict[var].strip()

You can remove those pesky newlines from your dictionary's keys and values like this (assuming your dictionary was stored in a variable nameddna):
dna = {k.rstrip(): v.rstrip() for k, v in dna.iteritems()}

Related

Use custom sort key properly with list of file strings

I have a function that I found that sorts a list of files strings by number. I found this on the internet and it works great. But, I want to simply it so it's not all done on one line, and it's more readable for other people. But, I'm having a difficult time doing this because I don't understand the sorted function key, and lambda's very well. Any help would be greatly appreciated.
Input
input_list = ['file_stuff_1','file_stuff_23','file_stuff_4','file_stuff_6']
Code
output = sorted(input_list, key=lambda x: int("".join([i for i in x if i.isdigit()])))
Output
['file_stuff_1','file_stuff_4','file_stuff_6','file_stuff_23']
Attempt
This was my first attempt at it. But I'm not sure how to fix digits so it works as a key.
def sort_list(input_list):
for item in input_list:
if (item.isdigit()):
digits = int(''.join(item))
output = sorted(input_list,digits)
return output
def get_nums_from_filename(file_name):
digits = [char for char in file_name if char.isdigit()]
return int(''.join(digits))
input_list = ['file_stuff_1','file_stuff_23','file_stuff_4','file_stuff_6']
sorted(input_list, key=get_nums_from_filename)
https://docs.python.org/3/howto/sorting.html#key-functions
the "key" param in sorted() specifies a function that should be applied to each item to get a resulting value, then sorting of the original values is done based on the corresponding resulting values.

How to get the key element based on matching the key element in a dictionary?

I have a dictionary which looks like this:
dictionary={
"ABC-6m-RF-200605-1352": "s3://blabla1.com",
"ABC-3m-RF-200605-1352": "s3://blabla2.com",
"DEF-6m-RF-200605-1352": "s3://blabla3.com"
}
Now, I want to do a matching which takes input such as helper="ABC-6m" and tries to match this string to the key of the dictionary and returns the key (not the value)!
My code currently looks like this but it is not robust, i.e. sometimes it works and sometimes it does not:
dictionary_scan = dict((el, el[:7]) for el in dictionary)
#swapping key and value
dictionary_scan = dict((v, k) for k, v in dictionary.items())
#concat string components in helper variable
helper = 'ABC'+'-'+'6m'
out=list(value for key, value in dictionary_scan.items() if helper in key)
The expected output is: 'ABC-6m-RF-200605-1352'. Sometimes this works in my code but sometimes it does not. Is there a better and more robust way to do this?
If you make a dictionary that maps prefixes to full keys, you'll only be able to get one key with a given prefix.
If there can be multiple keys that start with helper, you need to check them all with an ordinary list comprehension.
out = [key for key in dictionary if key.startswith(helper)]

How to compare array entries to dictionary keys and return a dictionary value

I am trying to make a python program that takes user input text or a file and changes each character into a value and then returns the result.
I have a user string that being read into a list.
I am trying to have a for loop go through that list and check each character against a dictionary key and then return the value in the dictionary. How would i go about doing that?
Thanks
Code so far:
for i in range (0, len(text)):
for j in alphabet.keys():
if text[i].upper() == alphabet.values():
j+=1
print(alphabet.items())
i+=1
for item in list_:
try:
print(d[item])
except KeyError as e:
print("{} not in d".format(e.args[0]))
Without seeing your code, I can't offer anything more relevant
You probably want to use string.maketrans and string.translate
>>> import string
>>> table = string.maketrans('abc', 'xyz')
>>> string.translate('the cat is bad', table)
'the zxt is yxd'
Most of the code below is simply to create the dictionary that translates letters of an input into randomised corresponding values in a dict (i.e. each letter maps to another random letter). Points on your code:
1) range() automatically defaults to starting at 0 so range(0, n) is better just written as range(n)
2) You don't need to use range() at all here. for letter in string will take an input string and go through it, letter by letter. for elephant in string will do the same, each letter is being assigned to the name elephant in turn, so the fact that I chose to use letter instead is simply for readability.
3) Using keys(), values() and items() is not the way to query a dictionary. You have two standard approaches; I could use translation_dict[letter] which will throw KeyError if the value of letter is not a key in the dictionary, or translation_dict.get(letter) which will return None if the key doesn't exist. In the below example, I used get() but also added another parameter ("not in dict") which replaces None as the default value if the letter isn't found as a key.
import string # For setup of example data
import random # For setup of example data
# Just creating the translation dictionary and fake user input
alphabet = list(string.uppercase)
translated = random.sample(alphabet, len(alphabet))
translation_dict = {i: j for i, j in zip(alphabet, translated)}
user_input = 'Hello'
# The loop you're trying
for letter in user_input:
corresponding_value = translation_dict.get(letter.upper(), 'Not in dict')
print(corresponding_value)

How to iteratively replace list items with dictionary values in python?

I'm trying to create an encoding/decoding function which takes an input string and switches each letter to the corresponding value in the dictionary Key by matching letter to a key.It's basically a ROT-13 cipher.
def Encode_Decode(A):
A=list(A)
for n in range(0,len(A),+1):
if A[n]==Key.keys:
map(Key.values(),A[n])
print("This translates to: "+"".join(A))
Encode_Decode("Hello there")
I use a dictionary which assigns a key value in the alphabet to a letter 13 letters up
Key= {'a':'n','b':'o','c':'p','d':'q','e':'r','f':'s',
'g':'t','h':'u','i':'v',
'j':'w','k':'x','l':'y','m':'z','n':'a',
'o':'b','p':'c','q':'d','r':'e'
,'s':'f','t':'g','u':'h','v':'i','w':'j',
'x':'k','y':'l','z':'m','A':'N',
'B':'O','C':'P','D':'Q','E':'R','F':'S',
'G':'T','H':'U','I':'V','J':'W',
'K':'X','L':'Y','M':'Z','N':'A','O':'B',
'P':'C','Q':'D','R':'E','S':'F',
'T':'G','U':'H','V':'I','W':'J',
'X':'K','Y':'L','Z':'M'}
Can anyone tell me why my function doesn't return a translated sentence?
This is not the only problem, but ... I think you want:
if A[n] in Key:
instead of
if A[n]==Key.keys:
Key is a confusing name for a dict, by the way.
I know this is merely a comment, not a complete answer, but I don't know how to format comments :)
If all possible characters were in Key, you could just write this:
def Encode_Decode(A):
A = map(Key.get, A)
print("This translates to: "+"".join(A))
However, in your test string, there is a space, and Key.get(' ') returns None. "".join(A) will fail if there is a None in the sequence
You can fix that by passing a default argument to Key.get
def Encode_Decode(A):
A = (Key.get(x, x) for x in A)
print("This translates to: "+"".join(A))
Key.get(x, x) means if x isn't in the dictionary, just use x instead, so spaces and other characters will be unchanged

Print data in a dictionary nested in a dictionary, in a nice way?

Trying to display data in a structure that is a dictionary contained in a defaultdict
dictionary=defaultdic(dict)
Example
defaultdict = {key1} :
{subkey1}:
(val1,
val2,
val3)
{subkey2}:
(val4,
val5,
val6)
{key2} :
{subkey3}:
(val7,
val8,
val9),
{subkey4}:
(val10,
val11,
val12)
I tried to do
for key in dictionary.iterkeys():
print key # This will return me the key
for items in dictionary[key]:
print items # This will return me the subkey
for values in dictionary[key][items]:
print values #this return the values for each subkey)
The problem is that I just get printed out a flat list of items; which is almost impossible to follow when you have too many items and keys.
How do you properly print such complex structures, to present them in a way that does not make you rip your eyes out? I tried with pprint and json.dumps but neither was making the situation better. Ideally I would like to have it printed as in my example, but I can't see a simple way to do so, without going trough complex string manipulation to format the print output.
Python has the PrettyPrint module just for this purpose. Note that defaultdicts won't print nicely, but if you convert back to a regular dict first it'll do fine.
from pprint import pprint
pprint(dict(dictionary))
Use indentation to print them out so your eye can visually see the structure.
for key in dictionary.iterkeys():
print key # This will return me the key
for items in dictionary[key]:
print(" %s" % items) # This will return me the subkey
for values in dictionary[key][items]:
print(" %s" % values) #this return the values for each subkey)
You can also substitute the space characters for \t to use a tab character; both will work fine. You may also have to use repr(values) or str(values) to explicitly get a string representation of them, if Python complains about the objects not being able to be formatted as a String.

Categories

Resources