Re: [Tutor] Question regarding lists and manipulating items in lists.
[SNIP] Thank you guys so much, sorry for the delayed response. It's awesome being able to learn a thing or two from people who know so much about their craft. I've got the code working the way I envisioned it now and probably couldn't without y'alls help. I'm so glad this mailing list exists, thanks again. Scott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
On 16/01/13 11:23, Scurvy Scott wrote: After playing with your example I keep being told that list has no attribute int_to_note. I know what the problem is, I just don't know how to fix it. Oops, sorry about that, that is my fault. I did warn that my code was untested! If you know what the problem is, the solution should be obvious. Before reading ahead, try explaining to yourself what you think the problem actually is. If you do that, the answer should pop right out at you. Still need a hint? Okay, try this: "I need to convert the Fibonacci integers to musical notes, using the notes.int_to_note function imported from mingus.core. But inside Steven's make_notes function, he sets a local variable `notes = []`, which means that inside the function I cannot access the global notes.int_to_note." Or, a shorter version: "I have notes.int_to_note, which I need, but accessing it is blocked by the local variable notes which is a list." So the obvious solution is... ...rename the local variable `notes` to something else. def make_notes(num_notes): it = fib() music = [] # start with an empty list for i in range(num_notes): n = next(it) % 12 # get the next Fibonacci number, modulo 12 music.append(notes.int_to_note(n)) return music Still untested. Also, I believe I've fixed my email so it will no longer be in HTML or anything fancy, just plain text. Many thanks! More comments below. So right now my code is: import mingus.core.notes as notes #fibonacci def fib(): a, b = 0, 1 while True: yield b a, b = b, a+b I have now tested that, and it works fine: py> it = fib() py> [next(it) for i in range(15)] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] Some people prefer to start the Fibonacci sequence with 0. That's an easy change to make: change the line "yield b" to "yield a". But Wolfram Mathworld and Mathematica start the Fibonacci sequence with 1, 1, 2, ... rather than 0, 1, 1, ... so that's good enough for me. http://mathworld.wolfram.com/FibonacciNumber.html def make_notes(num_notes): it = fib() notes = [] for i in range(num_notes): n = next(it) % 12 notes.append(notes.int_to_note(n)) return notes Which is pretty different from what my original code was. Maybe so, but I'm teaching you a practice that will see you in good stead whenever you program: each function should do *one* thing. Think of programming as creating tools. You wouldn't try to create a single tool for cutting wood, driving screws into it, and painting it. Instead we have three tools: saw, screwdriver, paint brush. It's *much* easier to make three separate tools than a single tool that tries to do all three jobs. Likewise for your program. It is better to write separate functions to: * create the Fibonacci numbers; * turn them into musical notes; than to do both jobs in one function. Now in *this* case, the labour saved is relatively small. But it is a good habit to get into, and when you get to large, complex programs it becomes essential. The generator function doesn't actually do anything when called, it just tells me it's a generator function and where it's located. If you look at how my code uses the generator function, you will see the correct way to use it. Here's another example: py> it = fib() py> n = next(it) py> while n < 100: ... print n ... n = next(it) ... 1 1 2 3 5 8 13 21 34 55 89 -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
On 16.01.2013 01:23, Scurvy Scott wrote: > After playing with your example I keep being told that list has no > attribute int_to_note. I know what the problem is, I just don't know > how to fix it. [SNIP] > So right now my code is: > > import mingus.core.notes as notes ^ On this line you import your module and give it the name "notes". > def make_notes(num_notes): >it = fib() >notes = [] ^ Inside your function "notes" is a list. >for i in range(num_notes): >n = next(it) % 12 >notes.append(notes.int_to_note(n)) ^ Since "notes" is a list inside the function, Python tries to find the method "int_to_note" for a list and fails. But I think you want to use the function which is defined in your module. You have to either rename your module reference or your list. Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
>> So here I extract out of your code (untested!) a generator which produces >> an infinite series of Fibonacci numbers, one at a time: >> >> def fib(): >> >> a, b = 0, 1 >> while True: >> yield b >> >> a, b = b, a+b >> >> >> This is untested, I may have got it wrong. >> >> Next, a function to generate notes from those Fibonacci numbers: >> >> >> def make_notes(num_notes): >> it = fib() >> notes = [] # start with an empty list >> for i in range(num_notes): >> n = next(it) % 12 # get the next Fibonacci number, modulo 12 >> notes.append(notes.int_to_note(n)) >> return notes >> >> >> That returns a list of notes. >> >> >> Does that help? Start with that, and see how you go. >> >> >> >> -- >> Steven >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > Steve > After playing with your example I keep being told that list has no > attribute int_to_note. I know what the problem is, I just don't know > how to fix it. > Also, I believe I've fixed my email so it will no longer be in HTML or > anything fancy, just plain text. > > So right now my code is: > > import mingus.core.notes as notes > > > #fibonacci > def fib(): > a, b = 0, 1 > while True: > yield b > a, b = b, a+b > > > def make_notes(num_notes): > it = fib() > notes = [] > for i in range(num_notes): > n = next(it) % 12 > notes.append(notes.int_to_note(n)) > return notes > > Which is pretty different from what my original code was. The > generator function doesn't actually do anything when called, it just > tells me it's a generator function and where it's located. And like I > said the listing function doesn't want to comply with the module being > called under append method. My code was working almost as I intended > it to, it just wasn't creating a list properly of everything, which I > didn't notice until after one of you guys mentioned it to me. Now I > see it and I can sorta visualize what I'm supposed to do, but can't > see what to do to fix it, if that makes any sense. On a hunch I've gone back to my original code, and feel I've almost got the list properly working. import mingus.core.notes as notes #fibonacci def fib(num1,num2): a, b = 0, 1 for i in xrange(num1,num2): c = b % 12 a_list= [] a, b = b, a+b while True: a_list.append(notes.int_to_note(c)) print a_list The problem here, which most of you will probably see immediately, is that all this does is infinitely append the same note to the list over and over again, which is not my intention. This is the closest I've gotten the code to actually working somewhat correctly as far as this list is concerned. Any hints would be helpful. I also think Oscars solution with searching the list for items in the 'search' list and then just removing them is the most elegant so far but I'm still open to anything you guys can show me, that's why you're the tutors and I came here to ask, right? I appreciate any information y'all can share. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
On Tue, Jan 15, 2013 at 4:01 PM, Steven D'Aprano wrote: > On 16/01/13 10:40, Scurvy Scott wrote: > [...] > >> Anyways, the problem I'm having is I'm not really sure how to search a >> list >> for multiple elements and remove just those elements. Below is my code so >> far, and information y'all could provide would be appreciated. Thanks. > > > Actually, your problem so far is that you aren't even producing a list of > elements at all, you keep creating a *single* element, then throwing it > away when you generate the next Fibonacci number. > > Also, you, or more likely Gmail, lost the indentation in your code, so I'm > going to have to guess what you intended rather than what you have. That's > because you are sending HTML email, which eats spaces. > > > >> import mingus.core.notes as notes >> #fibonacci >> def fib(num1,num2): >> a, b = 0, 1 >> for i in xrange(num1,num2): >> c = b % 12 #modulo 12 on each generated fibonacci number >> a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12 >> numbers into notes and then (I think) storing each one as an element in a >> list? >> a, b = b, a+b #this is just the algorithm for the fibonacci numbers > > > > Firstly, I recommend that you follow the principle "separation of concerns". > Keep a separate function for each part of the problem: > > * generate Fibonacci numbers; > * turn them into notes; > > > So here I extract out of your code (untested!) a generator which produces > an infinite series of Fibonacci numbers, one at a time: > > def fib(): > > a, b = 0, 1 > while True: > yield b > > a, b = b, a+b > > > This is untested, I may have got it wrong. > > Next, a function to generate notes from those Fibonacci numbers: > > > def make_notes(num_notes): > it = fib() > notes = [] # start with an empty list > for i in range(num_notes): > n = next(it) % 12 # get the next Fibonacci number, modulo 12 > notes.append(notes.int_to_note(n)) > return notes > > > That returns a list of notes. > > > Does that help? Start with that, and see how you go. > > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Steve After playing with your example I keep being told that list has no attribute int_to_note. I know what the problem is, I just don't know how to fix it. Also, I believe I've fixed my email so it will no longer be in HTML or anything fancy, just plain text. So right now my code is: import mingus.core.notes as notes #fibonacci def fib(): a, b = 0, 1 while True: yield b a, b = b, a+b def make_notes(num_notes): it = fib() notes = [] for i in range(num_notes): n = next(it) % 12 notes.append(notes.int_to_note(n)) return notes Which is pretty different from what my original code was. The generator function doesn't actually do anything when called, it just tells me it's a generator function and where it's located. And like I said the listing function doesn't want to comply with the module being called under append method. My code was working almost as I intended it to, it just wasn't creating a list properly of everything, which I didn't notice until after one of you guys mentioned it to me. Now I see it and I can sorta visualize what I'm supposed to do, but can't see what to do to fix it, if that makes any sense. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
On 15 January 2013 23:53, Scurvy Scott wrote: >> > Anyways, the problem I'm having is I'm not really sure how to search a list >> > for multiple elements and remove just those elements. Below is my code so >> > far, and information y'all could provide would be appreciated. Thanks. >> [SNIP] > > What I meant to say is, I want to be able to search the generated list > for a specific set of strings so like > > list = ['a','b','c','d','e','f','g'] > search = ['b','d','g'] > list.del[search] How about: new_list = [element for element in list if element not in search] (It would be more efficient to use a set but it's not strictly necessary) Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
On 16/01/13 10:40, Scurvy Scott wrote: [...] Anyways, the problem I'm having is I'm not really sure how to search a list for multiple elements and remove just those elements. Below is my code so far, and information y'all could provide would be appreciated. Thanks. Actually, your problem so far is that you aren't even producing a list of elements at all, you keep creating a *single* element, then throwing it away when you generate the next Fibonacci number. Also, you, or more likely Gmail, lost the indentation in your code, so I'm going to have to guess what you intended rather than what you have. That's because you are sending HTML email, which eats spaces. import mingus.core.notes as notes #fibonacci def fib(num1,num2): a, b = 0, 1 for i in xrange(num1,num2): c = b % 12 #modulo 12 on each generated fibonacci number a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12 numbers into notes and then (I think) storing each one as an element in a list? a, b = b, a+b #this is just the algorithm for the fibonacci numbers Firstly, I recommend that you follow the principle "separation of concerns". Keep a separate function for each part of the problem: * generate Fibonacci numbers; * turn them into notes; So here I extract out of your code (untested!) a generator which produces an infinite series of Fibonacci numbers, one at a time: def fib(): a, b = 0, 1 while True: yield b a, b = b, a+b This is untested, I may have got it wrong. Next, a function to generate notes from those Fibonacci numbers: def make_notes(num_notes): it = fib() notes = [] # start with an empty list for i in range(num_notes): n = next(it) % 12 # get the next Fibonacci number, modulo 12 notes.append(notes.int_to_note(n)) return notes That returns a list of notes. Does that help? Start with that, and see how you go. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding lists and manipulating items in lists.
On 15 January 2013 23:40, Scurvy Scott wrote: [SNIP] > > Anyways, the problem I'm having is I'm not really sure how to search a list > for multiple elements and remove just those elements. Below is my code so > far, and information y'all could provide would be appreciated. Thanks. Perhaps you'd like to use a list comprehension to filter out the values you are interested in: >>> my_list = [1,4,12,3,5,2,1,45,6,32] >>> my_filtered_list = [x for x in my_list if x%2] # only the odd numbers >>> print my_filtered_list [1, 3, 5, 1, 45] > > import mingus.core.notes as notes > #fibonacci > def fib(num1,num2): > a, b = 0, 1 > for i in xrange(num1,num2): > c = b % 12 #modulo 12 on each generated fibonacci number > a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12 > numbers into notes and then (I think) storing each one as an element in a > list? > a, b = b, a+b #this is just the algorithm for the fibonacci numbers Please post in plain-text rather than html as it screws up the code formatting (see above). Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question regarding lists and manipulating items in lists.
Hello guys, I'm using Ubuntu 12.10 and Python 2.7 right now. I'm working on code using the Mingus module but this question isn't specific to this module, per se. What I'm trying to do is to generate the fibonacci numbers up to a given N and then do modulo 12 on each number in order to create a list of numbers for the Mingus module to convert to notes. What I would like to do is store each note created in a different element in a list so that I can later manipulate it, say by removing certain note letters from the list at will. That way the scales created by the program can become more useful, for example I could remove all notes that aren't present in say a Harmonic Minor scale, and only keep the list items that do exist in that scale in a given key. That way it becomes, in a way, like the computer is writing your guitar solos! Pretty neat I think. Anyways, the problem I'm having is I'm not really sure how to search a list for multiple elements and remove just those elements. Below is my code so far, and information y'all could provide would be appreciated. Thanks. import mingus.core.notes as notes #fibonacci def fib(num1,num2): a, b = 0, 1 for i in xrange(num1,num2): c = b % 12 #modulo 12 on each generated fibonacci number a_list= [notes.int_to_note(c)] #using Mingus to translate the Fib mod12 numbers into notes and then (I think) storing each one as an element in a list? a, b = b, a+b #this is just the algorithm for the fibonacci numbers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor