Python Forum
finding items/comparison in/with a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding items/comparison in/with a dictionary
#1
Hello,
I have file 1 with this kind of information: Name Definition for about 2,000 names --> I have created a dictionary with this file (name:definition)

I have file 2 with this kind of information: >Name \n Information about name for the same 2,000 names

The goal is to add the definition of file 1 to the correct name in file 2: >Name Definition \n Information

I generated the code below where it is supposed to verify if the name is the same as in the dictionary:
if line(name) in dict.keys() and it doesn't seem to work. I have printed separately line(name) and dict.keys() and they are the same list of characters,except for that when I print the dictionary it adds brackets as in ['name'].

(I also know that I make my code more complicated than it should be ... I need to learn how to generate functions!)
file1= open('c:/python27/annotation.txt', 'r')
dict={}
file2= open('c:/python27/protein_file.faa', 'r')
outfile=open('c:/python27/proteinandannotation.faa', 'w')
for line in file1:
	name=line.strip().split()
	dict={name[0]:name[1:]}
	for line in file2:
		if line.startswith('>'):
#		line=line.strip
			if line[1:28] in dict.keys():
				line=line.strip('\n')
				outfile.write(line + dict.values() + '\n')
		else:
			outfile.write(line)
Reply


Messages In This Thread
finding items/comparison in/with a dictionary - by AGC - Mar-29-2018, 09:11 PM
for loop not loopng through dictionary - by AGC - Apr-02-2018, 06:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 931 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  how to assign items from a list to a dictionary CompleteNewb 3 1,686 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Calculating frequency of items in a dictionary markellefultz20 1 1,792 Nov-27-2019, 04:21 AM
Last Post: scidam
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 3,740 Mar-03-2018, 05:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020