0

i know that 6/5 returns 1
and 6/5.0 returns 1.2
but suppose i am defining n=6 and d=5, i want n/d to return 1.2 instead of 1, how do i do it?

7
Contributors
14
Replies
15
Views
7 Years
Discussion Span
Last Post by shadwickman
0

also, i have a question about loops in python:
consider the following code:

for i in lst: # where lst is a predefined list with say 5 objects
  if some_condition==True:
    del i

will this work without problems? i get unexpected results..

0

To answer your first question, set d = 5.0, OR, use d/float(d) #float(n)/d works too To answer your second question, it depends on what the code fragment is trying to do, and what your expected results are. I can launch into a long exposition on why I think it doesn't work, based on guesses and assumptions, but my response would be more sane if you would just tell me what you are trying to do.

0

Replying to your second question:

l=['apple','orange','mango','pear','strawberry']
for a in l:
    if(l=='pear'):
        l.remove(i)

That ought to work! :P

0

That ought to work!

Not work maybe typeError.
'a' iterarte throught the list,then you have to compare 'a ' == 'pear' Now you compare l with 'pear'

>>> l=['apple','orange','mango','pear','strawberry']
>>> l == 'pear'
False
>>>
l = ['apple','orange','mango','pear','strawberry']
for a in l:
    if(a == 'pear'):
        l.remove(a)       

print l
0

Not work maybe typeError.
'a' iterarte throught the list,then you have to compare 'a ' == 'pear' Now you compare l with 'pear'

>>> l=['apple','orange','mango','pear','strawberry']
>>> l == 'pear'
False
>>>
l = ['apple','orange','mango','pear','strawberry']
for a in l:
    if(a == 'pear'):
        l.remove(a)       

print l

My bad! Sorry, I was such a fool! I'll fix it later! :D

0

Replying to your second question:

l=['apple','orange','mango','pear','strawberry']
for a in l:
    if(l=='pear'):
        l.remove(i)

That ought to work! :P

Why not simpply:

mylist = ['apple','orange','mango','pear','strawberry']
mylist.remove('pear')

print(mylist)  # ['apple', 'orange', 'mango', 'strawberry']
0

i know that 6/5 returns 1
and 6/5.0 returns 1.2
but suppose i am defining n=6 and d=5, i want n/d to return 1.2 instead of 1, how do i do it?

With Python3 things have changed. Floating point division is now '/' and integer division '//'.

print(6/5)    # 1.2
print(6//5)   # 1
0

Why not simpply:

mylist = ['apple','orange','mango','pear','strawberry']
mylist.remove('pear')

print(mylist)  # ['apple', 'orange', 'mango', 'strawberry']

He wanted to know how to do it with loops, which is why I posted that code, although I made a type error! :D

0

BTW, I am not able to edit my first post in this thread, anyone know why? o.O

You can only edit within 30 minutes after posting.

0

He wanted to know how to do it with loops, which is why I posted that code, although I made a type error! :D

This would make more sense for using a loop ...

mylist = ['apple','orange','mango','pear','strawberry']

target = 'pear'
newlist = []
for item in mylist:
    if item != target:
        newlist.append(item)

print newlist

Or mildly more elegant ...

mylist = ['apple','orange','mango','pear','strawberry']
target = 'pear'
newlist = [item for item in mylist if item != target]
print newlist
0

wow.. thanks a lot everybody.. the reason why i was askng was this function... which returns the lowest non-zero number in a list.

def lowest(n):#n is a list of whole numbers
	#for item in range(len(n)):
	#	if n[item]==0:
	#		del n[item]
	#for r in n:
	#	if r==0: del r
	lowest=n[0]
	for q in n:
		#print q
		if q<lowest:
			lowest=q
	print "\n",lowest,"\n"
	return lowest

all the above give unexpected results because i am editing the list while the loop is in progress...

i had earlier tried to write a program to rename all files in a directory according to a certain format, but there too i had the same problem:

lst=os.listdir("path")
for i in lst:
  blah blah(rename files)
0

Or mildly more elegant ...

In that case, you could just use filter with a lambda expression :P

mylist = ['apple','orange','mango','pear','strawberry']
target = 'pear'
newlist = filter(lambda x: x == target, mylist)

# or ... multiple targets
mylist = ['apple','orange','mango','pear','strawberry']
targets = ['pear', 'apple']
newlist = filter(lambda x: x in targets, mylist)

As for the problem regarding modifying the list while using a for loop, you could just use a while one instead. Just increment the loop index as needed.

mylist = ['apple','orange','mango','pear','strawberry']

i = 0
while i < len(mylist):
    item = mylist[i]
    if item == 'pear':
        # if you delete one index, the ones after will now be located
        # at one index lower, so you don't need to increment i.
        del mylist[i]
    else:
        # go to the next index...
        i += 1
This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.