443,965 Members | 1,450 Online
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

# sorting

 P: 9 Hi I am new to python and I need your help on this big time ... 'm using windows my problem : What i need to do is .... I need to take all the values as inputs ... not type in the code ... if there are 'n' sources .... if 'n' is 3 .... i need to take say a1= freq1 , sampling rate1 a2= freq2, sampling rate 2 a3 = freq 3 , sampling rate 3 i need to sort (sampling rate 1, sampling rate 2 , sampling rate3) and say if sampling rate2 is the highest then i need to print out the corrresponding freq value i.e in this case freq 2 . MY CODE : a1 = [3, 2] a2 = [7, 5] a3= [56, 242] a4= [3, 146] t = zip(a1, a2, a3, a4) print t for i, intTuple in enumerate(t): t[i] = sorted(intTuple) print t #t.sort() #print t print t[:1] print t[1:2] in this example t[1:2] is sampling rate in descending order - [242 , 146 ,5 ,2] hence i need to print Just the corresponding freq value i.e 56 please guys help me Nov 29 '07 #1
4 Replies

 100+ P: 112 just store the freq as a key and sampling rate as its corresponding value in a python dictionary : Expand|Select|Wrap|Line Numbers   dic={}   dic[3]=2 dic[7]=5 dic[56]=242 dic[3]=146     # then do the following :   a=dic.keys()[dic.values().index(max(dic.values()))]   print a     Nov 29 '07 #2

 P: 13 hi... This is a very simple code which might help you. n=int(raw_input('enter no. of lists')) s=[] for i in range(n): l=[] for j in range(2): l.append(int(raw_input('enter element'))) print l s.append(l) print s s.sort() #print s x=[] for i in range(0,len(s)-1): for j in range(i+1,len(s)): if s[i][1]

 Expert Mod 2.5K+ P: 2,851 Here is one way to do it: Expand|Select|Wrap|Line Numbers a1 = [3, 2] a2 = [7, 5] a3 = [56, 242] a4 = [3, 146]   sampDict = dict(zip([item[1] for item in [a1,a2,a3,a4]], [item[0] for item in [a1,a2,a3,a4]]))   max_samp = max(sampDict.keys()) freq = sampDict[max_samp]   print "The maximum sampling rate is %d using a frequency of %d." % (max_samp, freq)   >>> The maximum sampling rate is 242 using a frequency of 56. Another: Expand|Select|Wrap|Line Numbers >>> zip(a1,a2,a3,a4) [(3, 7, 56, 3), (2, 5, 242, 146)] >>> readingsList =[list(tup) for tup in zip(a1,a2,a3,a4)] >>> max_index = readingsList[1].index(max(readingsList[1])) >>> print "The maximum sampling rate is %d using a frequency of %d." % (readingsList[1][max_index], readingsList[0][max_index]) The maximum sampling rate is 242 using a frequency of 56. >>>  Nov 29 '07 #4

 P: 9 thanx all you guys ... u've bin a great help ..... thanx a lot Nov 29 '07 #5