[Tutor] Question regarding lists and manipulating items in lists.

Scurvy Scott etanes.rm at gmail.com
Wed Jan 16 01:49:51 CET 2013


<SNIP>
>> 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 at 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.


More information about the Tutor mailing list