0

I got two question?

  • I need to check whether a[0] greater than a[1] and so on till a[n]

  • I need to check Whether a[n] lesser than a[n-1] and so on till a[0]

For Example, values in list:

['0.00', '0.00', '0.00', '7.79', '-1.63', '-0.37', '-1.42', '-0.20', '0.16', '0.25']
jOSe
  • 687
  • 11
  • 22
  • 2
    what is the output you would like to see? what have you tried? please show the code of what you have tried or you are very unlikely to get an answer here. – hiro protagonist Sep 08 '15 at 06:51
  • I've tried to check by comparing a[0] to a[1], then a[1] to a[2] manually. Recursive function will be great. – jOSe Sep 08 '15 at 06:52
  • http://stackoverflow.com/questions/3755136/pythonic-way-to-check-if-a-list-is-sorted-or-not may help – Bathsheba Sep 08 '15 at 06:54
  • 1
    Are you saying it needs to be sorted in reverse order, or just that the first value is the maximum and the last value the minimum? – Peter Wood Sep 08 '15 at 07:23
  • In case 1, 1st value should be lesser compared to others. In case 2, reverse the list and first value should be higher compared to others – jOSe Sep 08 '15 at 07:24
  • I just wanted to say. All value are increment in nature. – jOSe Sep 08 '15 at 07:26
  • Can you rephrase the question as: "Are the value numerically sorted?" ? – wap26 Sep 08 '15 at 08:45

2 Answers2

0
x=['0.00', '0.00', '0.00', '7.79', '-1.63', '-0.37', '-1.42', '-0.20', '0.16', '0.25']
y=zip(x,x[1:])

print [(i,j) for i,j in enumerate(y) if j[0]>j[1]]

You can use zip and enumerate to achieve the same.

For second part you can use

x=['0.00', '0.00', '0.00', '7.79', '-1.63', '-0.37', '-1.42', '-0.20', '0.16', '0.25']
x=x[::-1]
y=zip(x,x[1:])

print [(i,j) for i,j in enumerate(y) if j[0]<j[1]]
vks
  • 67,027
  • 10
  • 91
  • 124
  • I've tried your 1st part of code, and last two value are not display. If every value is greater than, printing out like "Increment" can be good. – jOSe Sep 08 '15 at 07:03
  • I'm trying something like this `for i in range(len(list)): if i < len(list)-1: if list[i] > list[i+1]: print "Yes" if list[i] == list[i+1]: print "Equal" else: print "no"` – jOSe Sep 08 '15 at 07:24
  • If all value are increment in nature, just printing out "Increment" will be good. – jOSe Sep 08 '15 at 07:27
  • @jOSe instead of (i,j) use "increased" – vks Sep 08 '15 at 07:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89034/discussion-between-jose-and-vks). – jOSe Sep 08 '15 at 07:30
  • @jOSe `print ["increment" for i,j in enumerate(y) if j[0]>j[1]]` – vks Sep 08 '15 at 08:43
0

you could try something like this: iterate simultaneously over

  • x[:-1]: the list without the last element
  • x[1:]: the list without the first element

and compare the values. note that for equality you can compare the list elements as strings - but for < and > you probably want to convert them to float before comparing.

x=['0.00', '0.00', '0.00', '7.79', '-1.63', 
   '-0.37', '-1.42', '-0.20', '0.16', '0.25']

for xj, xi in zip(x[:-1], x[1:]):

    if xj == xi:
        print '{} == {}'.format(xj, xi)
    elif float(xj) < float(xi):
        print '{} < {}'.format(xj, xi)
    else:
        print '{} > {}'.format(xj, xi)

output:

0.00 == 0.00
0.00 == 0.00
0.00 < 7.79
7.79 > -1.63
-1.63 < -0.37
-0.37 > -1.42
-1.42 < -0.20
-0.20 < 0.16
0.16 < 0.25

UPDATE:

after your comment it semst that you just want a list containing string telling you if the elements increase or not. this should work:

def comp_str(xj, xi):
    if xj == xi:
        return 'equal'
    elif float(xj) < float(xi):
        return 'yes'
    else:
        return 'no'

compare = [ comp_str(xj, xi) for xj, xi in zip(x[:-1], x[1:]) ]
print compare

# -> ['equal', 'equal', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes']
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • I've done something like this `temp[:] = []#Emptying the list for i in range(len(list)): if i < len(list)-1: if list[i] > list[i+1]: #print "Yes" temp.append("Yes") if list[i] == list[i+1]: #print "Equal" temp.append("Equal") else: #print "no" temp.append("No") if "No" in temp: print "Decrement Value available" else: print "Increment Values only"` – jOSe Sep 08 '15 at 07:42