Login Register






how to divide two lists. filter_list
Author
Message
how to divide two lists. #1
this is probably a very simple explanation but i can't seem to get it to work.
i need to make a new list that = first_list/second_list
I've got this so far.
Code:
firstlist = [10,20,30,40,50,60] secondlist = [3,5,6,8,3,5] newlist = [] newlist.append(firstlist/secondlist)
TypeError: unsupported operand type(s) for /: 'list' and 'list'
When i run it i get this error, any idea where i'm going wrong?
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23


RE: how to divide two lists. #2
You can't just divide two lists, you'll have to apply a loop to get the individual values.
You can use a For Loop like:
Code:
one = [1, 2, 3, 4] two = [2, 4, 6, 8] newlist = [] for num in one: for div in two: newlist.append(div/num) print(newlist)
This example is just to give you an idea
The loop individually divides the value of the first list with the value of the 2nd list and then appends in the array as you suggested.
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG


RE: how to divide two lists. #3
Thank you i had tried something like that before.
Code:
one = [1, 2, 3, 4] two = [2, 4, 6, 8] newlist = [] for i in one: newlist(i) =one(i)/two(i)
But i got the same error.
Also have you always been a mod? if you haven't then congrats Smile
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23


RE: how to divide two lists. #4
(04-03-2013, 09:22 AM)J4W3S Wrote: Thank you i had tried something like that before.
Code:
one = [1, 2, 3, 4] two = [2, 4, 6, 8] newlist = [] for i in one: newlist(i) =one(i)/two(i)
But i got the same error.
Also have you always been a mod? if you haven't then congrats Smile
You are doing it wrong, you are applying for loop to only one of the list and not the other. Examine my example and see how it works, To get the items separately from the list you need to apply a loop on both of them. Run my example in a debugger.
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG


RE: how to divide two lists. #5
Just ran your code. it's fine except it is dividing every value in one and two instead of the corresponding places.
one = [1, 2, 3, 4]
two = [2, 4, 6, 8]
when you divide list two by one you should get newlist = [2, 2, 2, 2]
It's output is actually newlist = [2, 4, 6, 8, 1, 2, 3, 4...]
anyway to get it to only divide values that correspond to each other?
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23


RE: how to divide two lists. #6
@J4W3S I searched for the problem and came with a solution from Stackoverflow, It was pretty easy instead we had to use a function called Zip which takes the corresponding values instead of multiplying one value with the whole list and then the other.

Code:
one = [1, 2, 3, 4] two = [2, 4, 6, 8] newlist = [] for num, div in zip(one, two): newlist.append(div/num) print(newlist)

Output:
Code:
[2.0, 2.0, 2.0, 2.0]
For more help, type help(zip) in python IDE

Hope this helps Smile
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG


RE: how to divide two lists. #7
thanks been trying to do this for ages, should of asked here sooner
there are 10 types of people in this world,
those who understand binary and those who don't.

skype name: J4W3S.23


RE: how to divide two lists. #8
(04-03-2013, 10:49 AM)J4W3S Wrote: thanks been trying to do this for ages, should of asked here sooner
No Problem, Thread Solved!
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG


RE: how to divide two lists. #9
list comprehensions are a bit more concise:

Code:
one = [1, 2, 3, 4] two = [2, 4, 6, 8] newlist = [y/x for x, y in zip(one, two)]

This does the same as @Ex094 code.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]