# -*- coding: utf-8 -*-

p = [1, 2, 0, -1, 3, 4, 5]
smaller = 0
bigger = 0

i = 0
while i < len(p):  
  if p[i] >= p:
    bigger += 1  
    print bigger
  i += 1

#bigger than -1 is (1, 2, 0, 3, 4, 5)
#bigger than 1 is (2, 3, 4, 5)
#bigger than 3 is (4 and 5)
#............

How do to for this?

Recommended Answers

All 6 Replies

I'm not exactly sure on what you want to accomplish.

However, you can change the while loop to a for loop

for i in range(len(p)):
    pass #....

I'm not exactly sure on what you want to accomplish.

However, you can change the while loop to a for loop

for i in range(len(p)):
    pass #....

p = [1, 2, 0, -1, 3, 4, 5]

greater than -1 --> 1, 2, 0, 3, 4, 5 --> return -1

greater than 2 --> 3, 4, 5 --> return 2

I've added a print statement which should clarify what you are actually doing.

p = [1, 2, 0, -1, 3, 4, 5]
smaller = 0
bigger = 0
 
i = 0
while i < len(p):  
  print "comparing", p[1], p
  if p[i] >= p:
    bigger += 1  
    print bigger
  i += 1

I think you want two for loops. Note that the logic is different if the list is sorted first.

stop = len(p)
for x in range(0, stop):
    greater_list = []
    for y in range(0, stop):
        if p[x] < p[y]:
            greater_list.append(p[y])
    print p[x], "greater =", greater_list

If you learn simple list comprehensions you can do simpler way (python 2.6)

for limit in (-1,1,3):
      print [y in x if y>limit]

I've added a print statement which should clarify what you are actually doing.

p = [1, 2, 0, -1, 3, 4, 5]
smaller = 0
bigger = 0
 
i = 0
while i < len(p):  
  print "comparing", p[1], p
  if p[i] >= p:
    bigger += 1  
    print bigger
  i += 1

I think you want two for loops. Note that the logic is different if the list is sorted first.

stop = len(p)
for x in range(0, stop):
    greater_list = []
    for y in range(0, stop):
        if p[x] < p[y]:
            greater_list.append(p[y])
    print p[x], "greater =", greater_list

Thanks I'm tryng to this.

# -*- coding: utf-8 -*-
 
p = [1, 2, 10, -1, 3, 4, 5, 9, +66, 99, 88]
min_greater = 0
min_smaller = 0

stop = len(p)
for x in range(0, stop):
    greater_list = []
    smaller_list = []
    for y in range(0, stop):
        if p[x] < p[y]:
            greater_list.append(p[y])
	elif p[x] > p[y]:
	    smaller_list.append(p[y])
 
    if len(greater_list) > min_greater:
      min_greater = len(greater_list)
      a = x
      b = p[x]
      c = greater_list

    if len(smaller_list) > min_smaller:
      min_smaller = len(smaller_list)
      Amin = x
      Bmin = p[x]
      Cmin = smaller_list

    if len(smaller_list) == len(greater_list):
      print "Average element is:", p[x]

print ''
print "Minimum number:", b, "\nElement index:", a, "\n", "Len greater elements:", min_greater
print "Greater than", b, "is", c 
print ''
print "Maximum number:", Bmin, "\nElement index:", Amin, "\n", "Len greater elements:", min_smaller
print "Smaller than", Bmin, "is", Cmin 
print ''
#alist = [ '1', '1', '2', '1', '3', '4', '1', '3']
#print [(a, alist.count(a)) for a in set(alist)]

Hey tonyjv, how to use your code? Is not working!

I'm using Python 2.6.4 and Linux.

And this code works so:

File "P.py", line 3
print [y in x if y>limit]
^
SyntaxError: invalid syntax

Sorry typed it without testing from mobile 'y for' missing:

x = [1, 2, 10, -1, 3, 4, 5, 9, +66, 99, 88]
for limit in (-1,1,3):
    print 'Limit',limit
    data = [y for y in x if y>limit]
    print "Data: %s, Len: %i, Sum: %.2f" % (data, len(data), sum(data))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.