2011 Problem set #2 , problem 1. hey there i have started this problem. at the moment i am thinking of how i can extract the information out. So it can then be processed. There are two pieces of information , the index of the number in the tuple which gives the power and the actual value , which gives the co efficent. So if i have mytuple = (1,2,3) this will give my_power = (0,1,2) my coefficent = ( 1,2,3) i am strugggle to extract the index information. does anyone have any ideas , thanks jack
you mean you want to be able to find which index has the 3 or what? I'm not sure I understand
do you just want to be able to create ` my_power = (0,1,2) ` from ` my_coefficient = (1,2,3) ` ?
sorry i am probabaly not explaining what i mean very well , what i want is a way of extracting the index sequence of a list into actual numbers inside a list . it is this index information that i want . So if my list was my_list = ( jack , john , joe ) i like my new list to be my_new_list = (0,1,2)
you could do something like ``` my_new_list = () for i in range(len(my_list)): my_new_list += (i,) ```
Hi thank you for this . Intially what i wrote was for i in(len(poly)): print i but got a error message saying ' 'int' object is not iterable' i guess i have to use the range function . Do you have any idea on the theory behind not being able use the code i wrote. Plus how do i get the code i am writing to come up in a box as yours has above. Thanks for your help
Python can only iterate over certain kinds of objects like lists and tuples. I don't think any language can iterate *over* an integer, but in other languages you have `for` loops that look like `while` loops, in that they loop until a certain condition is met. The `for` loops in python work more like `foreach` loops in other languages, in that they specifically iterate over objects in an array. `range(5)` produces a list `[0,1,2,3,4]` which can then be iterated over by the Python interpreter. When you did ``` for i in len(poly): print i ``` you tried to iterate over a single number: the length of the array, which is not allowed.
I will explain grey boxes in private message so the syntax gets ignored
I should point out that `range` only produces a list in Python 2.7. I think it produces a "range object" in Python 3.x, which you then have to convert to a list to iterate over (which is annoying).
Join our real-time social learning platform and learn together with your friends!