[Tutor] Extract Field from List
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sun Aug 8 12:59:25 EDT 2021
On Sun, 8 Aug 2021 11:23:44 -0400, "Stephen P. Molnar"
<s.molnar at sbcglobal.net> declaimed the following:
>Python v-3.7.3
>
>The Script returns a one element list:
>
>0 str 99 CaffeicAcid Cannflavin-A Cannflavin-B Cannflavin-C
>Diosmetin Echinacoside Hesperetin L-CichoricAcid
>
Because, as was mentioned, your data is not a Comma SV -- it is a Tab
SV! Change the format of your CSV reader to use tab separation. Then you
will get a list of lists.
>In the next step I want to iteratively insert each name in the list in:
>
>for i in range(1,11):
> name_in = name+.i'.log'
Where is "name" defined? What is ".i" supposed to be doing -- "i" is an
integer, not a string, and you can't add integers to strings.
> data+.i = np.genfromtxt(name_in, skip_header=28, skip_footer=1)
> data+.i = data+.i[0, 1]
You are creating whatever you think "data+.i" produces using a numpy
call... and then immediately replacing it with what I interpret as the
first row, second column of itself.
>
>Most likely, it would probably be simpler to put these lines of code
>into a function.
>
>Now I'm not certain about formatting the insertion the value of i.
>Coming out of the loop I want to have a series of files, data1 to data10
>to use as arguments for np.vstack(...
If you expect a series of files "data1" through "data10", what is all
that rigmarole with "name"? If you mean you want <name>1 through <name>10,
say so. "data"x is not a file -- it is a numpy array containing the
contents of a file...
Note -- you can not dynamically create variable names in Python (at
least, not without using very advanced methods that get into how Python
itself operates).
>
>I would be most grateful for assistance in solving this problem.
<SIGH>
data = [] #main results list
for name in <whatever you call it after using tab separation>:
data_i = [] #result list for one "ligand"/name
for i in range(1,11):
full_name = "%s.%s.log" % (name, i) #create file name
data_i.append(np.genfromtxt(full_name,
skip_header=28, skip_footer=1)
#fetch contents of said file, append to result list
data.append(data_i) #append "name" results list to main results
To access the data for, say, name-2, and file 3, you would use
data[1][2] #Python lists start at 0, not one, so you have to
#adjust the indices used.
"data[1]" gets you the list for the second "name", then applying [2] to
that gets you the third numpy array.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list