に一列にファイルを格納:私は、このようなファイル持っている辞書
>Organism1
ETTGDMND
>Organism2
PDELMESPEER
>Organism3
YERLLRRAQ
>Organism1
EDLTEVSGIGC
を私は大文字(=アミノ酸配列)が鍵された辞書を作成したいと生物名は値です。 は、これまでのところ私が持っている:
dict1 = {}
for line in file.readlines():
line = line.rstrip()
if ">" not in line: # '>' not in the line=amino acid seq
key = line #assign the line into a variable 'key'
dict1[key] = [] #make this variable the keys of dict1
else: #if '>'is in the line = organism
value = line
dict1[key] = value
print dict1
それは「キー」が定義されていないことを示すエラーメッセージが発生します。しかし、私はそれがキー=ラインと言っていると思った..?
同じ入力ファイルを使用して関連する質問。私は(別の目的のために)、そのファイルからアミノ酸配列にのみ呼び出すしたい場合は、私がやった:
my_sequences = []
for line in file:
line = line.rstrip()
if ">" not in line:
my_sequences = [line] # add these dna sequences to the list "my_sequences"
print my_sequences
それだけですべてのシーケンスの代わりに一つのシーケンスを印刷します。 誰でも助けてくれますか? ありがとう!
最初に来たのは、キーか値ですか? –
あなたの最初の行は_> Organism1_です。つまり、コードは 'key'が定義されていない' else'ブランチに従います。 – CristiFati
ああ、意味があります! – ccaarroo