![]() |
|
how to divide two lists. - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: how to divide two lists. (/Thread-how-to-divide-two-lists) |
how to divide two lists. - J4W3S - 04-03-2013 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)When i run it i get this error, any idea where i'm going wrong? RE: how to divide two lists. - Ex094 - 04-03-2013 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)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. RE: how to divide two lists. - J4W3S - 04-03-2013 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)Also have you always been a mod? if you haven't then congrats
RE: how to divide two lists. - Ex094 - 04-03-2013 (04-03-2013, 09:22 AM)J4W3S Wrote: Thank you i had tried something like that before.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. RE: how to divide two lists. - J4W3S - 04-03-2013 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? RE: how to divide two lists. - Ex094 - 04-03-2013 @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]Hope this helps
RE: how to divide two lists. - J4W3S - 04-03-2013 thanks been trying to do this for ages, should of asked here sooner RE: how to divide two lists. - Ex094 - 04-03-2013 (04-03-2013, 10:49 AM)J4W3S Wrote: thanks been trying to do this for ages, should of asked here soonerNo Problem, Thread Solved! RE: how to divide two lists. - Deque - 04-03-2013 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. |