-2

I need to save every fourth item of a list as a key in python, then starting at 5 every fourth item as a value, and starting at 6 every fourth item as a value (the value will be a list)

I have a text file that looks like this: GeneID NumC NumL NumP ENSEMBL001 15 5 30 ENSEMBL002 60 0 10 ENSEMBL003 25 5 41 ENSEMBL004 20 7 0 ENSEMBL005 40 22 67 ENSEMBL006 81 77 9 ENSEMBL007 2 88 2 ENSEMBL008 8 62 90

and I want to make a dictionary where the "ENSEMBL" value is the key and the first two numbers are the value associated with that key

I have the code written for the first value of the dictionary:

    dict_lung = {
        file_name[4] : (file_name[5], file_name[6])
    }

When printed, this will be a dictionary with key ENSEMBL001 and value (15,5)

What I want to do is have the dictionary go through the entire text file and save these values. for example, the next key and value saved in the dictionary will be

file_name[8] : (file_name[9], file_name[10])

I have tried to make a for loop but keep getting errors, any help would be appreciated

cam
  • 31
  • 1
  • 8

1 Answers1

1

So basically this is a hard-coded problem. It based in the logic you used:

but you can use the following loop, where MAX will be the maximum number of allowed indices (Don't forget that you need to careful with exceeding the list):

for i in range(4, MAX, 4):
    dict_lung = { file_name[i] : (file_name[i+1], file_name[i+2]) }

From the commented loop:

i=4 j=5 k=6 
for i,j,k in list: 
    dict_lung = { file_name[i] : (file_name[j], file_name[k]) } 
    i+4 j+4 k+4

I'm not sure what list is but it's not a list that contain tuples of 3. please read about unpacking in python https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/

David
  • 8,113
  • 2
  • 17
  • 36