[clue] python (function return array) [programming]

Nick Pascucci npascut1 at gmail.com
Tue Dec 27 19:59:05 MST 2011


Hey, no problem. One thing to keep in mind about your solution: you're
returning a tuple now, not a list. A tuple is immutable once formed, so if
you get errors when trying to modify the return value, that's why.
Generally you want to match the data structure you use to the concept
they're supposed to be modeling, so if your data is mutable and you only
want to return the first three elements, you want to use a slice: return
my_list[:3].

Cheers,
Nick



On Tue, Dec 27, 2011 at 7:53 PM, Mike Bean <beandaemon at gmail.com> wrote:

> OK, I think I got this:
>
> def test_function():
>     my_array=[1,2,3]
>     return(my_array[0], my_array[1], my_array[2])
>
> for x in test_function():
>     print(x)
>
> Thank you Nick!
>
> Bean
>
> On Tue, Dec 27, 2011 at 6:38 PM, Nick Pascucci <npascut1 at gmail.com> wrote:
>
>> Hi Mike,
>>
>> You can return lists just like any other variable in Python. When you
>> iterate over it, just remember that the returned value was a list, and not
>> a single value. You don't need to make it into a tuple or wrap it in
>> another list like in your example. Here's a demo for you:
>>
>> def foo():
>>     return ["baz", "bar", "quux"]
>>
>> my_list = foo()
>> for x in my_list:
>>     print x  # Prints "baz\nbar\nquuz"
>>
>> If you do like in your example, and put my_list = [foo()] you'll have a
>> list of lists containing your desired list. Probably not what you're
>> looking for.
>>
>> Hope that helps,
>> Nick
>>
>>
>>
>> On Tue, Dec 27, 2011 at 7:18 PM, Mike Bean <beandaemon at gmail.com> wrote:
>>
>>> OK, I'm pretty sure this is a fairly novice question, but I'm working a
>>> project on my own and my attempts to suss the solution have met with
>>> limited success.
>>> I'm writing mostly similar code over and over again, bad me.  But I have
>>> to be able to return a list/array, and I don't know if return statement can
>>> handle more then just a single variable.  So for example, I realize your
>>> generic scenario is
>>>
>>> def function()
>>>      variable="whatever"
>>>      return(variable)
>>>
>>> newvar=function()
>>> print("newvar")
>>>
>>> but I want to know is what's wrong with my logic in this sense:
>>>
>>> def function()
>>>      my_array=[1,2,3]
>>>      return(my_array)
>>>
>>> new_array=[(my_array)]
>>> for x in new_array:
>>>      print(x)
>>>
>>> testing has met with no success obviously.  I'm assuming it's because an
>>> array is not the same thing as a generic variable.  In any case, any
>>> suggestions/guidance from folks with experience in programming/python is
>>> appreciated.
>>>
>>> Bean
>>>
>>> _______________________________________________
>>> clue mailing list: clue at cluedenver.org
>>> For information, account preferences, or to unsubscribe see:
>>> http://cluedenver.org/mailman/listinfo/clue
>>>
>>
>>
>> _______________________________________________
>> clue mailing list: clue at cluedenver.org
>> For information, account preferences, or to unsubscribe see:
>> http://cluedenver.org/mailman/listinfo/clue
>>
>
>
> _______________________________________________
> clue mailing list: clue at cluedenver.org
> For information, account preferences, or to unsubscribe see:
> http://cluedenver.org/mailman/listinfo/clue
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cluedenver.org/pipermail/clue/attachments/20111227/1595fd34/attachment-0001.html 


More information about the clue mailing list