By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
4 Replies


rhitam30111985
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
  1.  
  2. dic={}
  3.  
  4. dic[3]=2
  5. dic[7]=5
  6. dic[56]=242
  7. dic[3]=146
  8.  
  9.  
  10. # then do the following :
  11.  
  12. a=dic.keys()[dic.values().index(max(dic.values()))]
  13.  
  14. print a
  15.  
  16.  
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]<s[j][1]:
x=s[i]
s[i]=s[j]
s[j]=x
print s[0][0]

Is this the thing u were asking??If not,pls don mind...
Nov 29 '07 #3

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

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

Post your reply

Sign in to post your reply or Sign up for a free account.