[Tutor] returning values from function.
Bala subramanian
bala.biophysics at gmail.com
Mon Mar 23 19:33:38 CET 2009
Friends,
I wrote the following code to create two different list. One containing data
points of all clusters and another containing list of individual cluster
data points. The script is as follows.
#!/usr/bin/env python
from sys import argv
# STEP 1: a) ACCUMULATING ALL DATA POINTS IN AND A LIST b) CREATING LIST
OF INDIVIDUAL CLUSTERS
print argv[1], argv[2]
data=[] # list of all data points
all=[ [] for value in range(int(argv[2])) ] # Creating an empty list of
lists of size n (No of clusters)
def cluster(infile=argv[1],n=int(argv[2])):
for index, line in enumerate(infile):
if line.startswith('#Consensus'):
line=line.split()
data.extend(line[2]) # data now should contain data points
of all clusters
for value in range(n):
for index, line in enumerate(data):
if data[index] == str(value):
zero=index+1
all[value].append(zero)
#return data, all ( I even tried by un commenting the return
statement )
print all
print data
The final print statement returns a empty list ie all and data. I have the
following queries
i) Why the print statement returns empty lists
ii) Is return really required here, if i understand the namespace well, the
function cluster actually modifies the global variables data and all.
iii) I even tried by using a return statement but still the script returns
empty list.
iv) Is there any fancy or better of way of doing this python.
Thanks,
Bala
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090323/e1188ed4/attachment.htm>
More information about the Tutor
mailing list