import random ran_list = [random.random() for _ in range(30)] print "The original random list is:" print ran_list def saveFile(mystr,myfile): filehandle = open(myfile,"w") filehandle.write(str(mystr)) filehandle.close() myfilename = "random.txt1" saveFile(ran_list,myfilename) myfilename = "random.txt2" saveFile(ran_list,myfilename) filehandle = open(myfilename,"r") s = filehandle.read() # this will not make sense because it reads everything from the file as string, now I am going to define a better saveFile function def saveFile2(mystr,myfile): filehandle = open(myfile,'w') for element in mystr: filehandle.write(str(element)+' ') filehandle.close() myfilename = "random.txt3" saveFile2(ran_list,myfilename) filehandle = open(myfilename,'r') s = filehandle.read() newlist = [] # create a new list to save all numbers for element in s.split(): # we split this line by space newlist.append(float(element)) #print newlist z = random.sample(newlist,5) print "Samples from the list in the file" print z #print s sorteds = sorted(ran_list) # this is going to sort from smallest to largest print "Sorted list from smallest to largest" print sorteds sorteds = sorted(ran_list,key=abs,reverse=True) # this is going to sort from largest to smallest print "Sorted list from largest to smallest" print sorteds