Мне удалось написать свой сценарий, который использовал три списка, которые я предоставил в сценарии.
В качестве вывода (Открыть файл) я использую словари, созданные на основе этих списков.Тем не менее, я хочу написать свой сценарий так, чтобы я мог использовать любое количество списков, и поэтому любое количество словарей может быть записано в открытом файле и таким же образом.
dict1 = {'A': 'val1' ,'B': 'val1' ,'C': 'val2' , 'X': 'val2' , 'NAME': 'val5' ,'HA': 'val5' , 'gcay': 'val7' , 'nkd': 'val8'} #contain all my data
dict2 = {'Consensus1':'val1', 'Consensus2':'val2', 'Consensus3':'val5','Consensus4':'val7','Consensus5':'val8'} #contain new Keys with only single Val based on dict1 (repetitive Val# are collapse
List1 = ['A','B'] #these can be random names and not the alphabet
List2 = ['C', 'X', 'NAME']
List3 = ['HA', 'gcay', 'nkd']
List1_col = []
for i in List1:
List1_col.append(dic1[i]) #I have a dictionary that contains all the IDs
freq_List1_col = {key : List1_col.count(i) for i in List1_col for key,value in dict2.items() if value == i} #Here I switch the names for the key,value in another dictionary
List2_col = []
for i in List2:
List2_col.append(dic1[i])
freq_List2_col = {key : List2_col.count(i) for i in List2_col for key,value in dict2.items() if value == i}
List3_col = []
for i in List3:
List3_col.append(dic1[i])
freq_List3_col = {key : List3_col.count(i) for i in List3_col for key,value in dict2.items() if value == i}
with open('myfile.txt', 'w') as f:
header = 'This header appears every time I open a dictionary{\n'
f.write(header)
for key,value in freq_List1_col.items():
out1 = key + 'has ' + value
f.write(out1)
f.write('}\n')
f.write(header)
for key,value in freq_List2_col.items():
out2 = key + 'has ' + value
f.write(out2)
f.write('}\n')
f.write(header)
for key,value in freq_List3_col.items():
out3 = key + 'has ' + value
f.write(out3)
f.write('}\n')
Я в основном открываючастота ключей в dict2 в каждом списке.Вывод будет таким:
This header appears every time I open a dictionary{
Consensus1 has 2
}
This header appears every time I open a dictionary{
Consensus2 has 2
Consensus3 has 1
}
This header appears every time I open a dictionary{
Consensus3 has 1
Consensus4 has 1
Consensus5 has 1
}
Я думал, может быть, мне следует начать с «Списка списков»?Затем я могу написать любое количество списков, так как скрипт может читать любые элементы в списке. Заранее спасибо!