Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python

Name: Anonymous 2010-05-19 5:58

<code>def add_lists(a, b):
    """
      >>> add_lists([1, 1], [1, 1])
      [2, 2]
      >>> add_lists([1, 2], [1, 4])
      [2, 6]
      >>> add_lists([1, 2, 1], [1, 4, 3])
      [2, 6, 4]
    """
    new_list = []
    i = 0
    while i < len(a):
        new_list += a(i) + b(i)
        i += 1
    return new_list

if __name__ == '__main__':
    import doctest
    doctest.testmod()</code>

TypeError: 'list' object is not callable

How can I fix this?

Name: hibt? 2010-05-19 6:03

a[i], b[i] instead of a(i), b(i)

Name: Anonymous 2010-05-19 6:09

>>2
Okay, that removed the call error. 

now I've this runtime error:
TypeError: 'int' object is not iterable

Name: Anonymous 2010-05-19 6:27

>>3
Go ask you're homework questions somewhere else.

Name: Anonymous 2010-05-19 6:34

>>4
Am I supposed to feel bad for asking a /prog/ related question in /prog/?  No doubt I'm new to the language and programming in general.  If you'd like me to leave quickly/improve my skills, you'd answer the question.  Anything less is an act of trolling.  Why post on a /prog/ if you've no intention of discussing /prog/ related material?  Perhaps you're incapable of debugging a program so simple? 

tldr: I'm new, don't be an ass, be helpful

Name: Anonymous 2010-05-19 6:40

>>5
You based your arguments on the false assumption this question is /prog/ related. I think you will find /pr/ more suitable for you're needs.

Name: Anonymous 2010-05-19 6:46

Stick to playing flash games, OP. If you are unable to read an error message, then you have no hope.

Name: Anonymous 2010-05-19 7:02

TypeError: 'int' object is not iterable

I'm trying to traverse an integer instead of a list. 

new_list += a[i] + b[i]

I'm misunderstanding something.  From what I've conjured (incorrectly):

the sum of a[0] and b[0] will concatenate to the end of new_list.

do the "[]" ask it to search the list for the value inside the "[]"?

if so, wouldn't "(index)" be the correct way to call the element of the list?

Name: I'm so clever 2010-05-19 7:05


def add_lists(*lists):
    return list(map(sum, zip(*lists)))

Name: Anonymous 2010-05-19 7:27

do the "[]" ask it to search the list for the value inside the "[]"?
No, what are you learning python from, because they are filling your mind with bullshit.

When you use the command a[x], you are telling python to get the value stored in the 'a' list at position 'x'.

if so, wouldn't "(index)" be the correct way to call the element of the list?
no, if you are determined to do it with a list method, use a.index(x)

Seriously though, google yourself up a couple of python tutorials and learn the language.

Name: Anonymous 2010-05-19 7:38

>>10
Thank you for the information.  I doubt it's the book.  Rather, I'm not paying close enough attention.  I believe I've figured it out. 

new_list += a(i) + b(i):
is incorrect because I was asking for the element of a tuple instead of a list.

new_list += a[i] + b[i]:
is incorrect because I was attempting to concatenate an integer to a list.  ie: "[] + int"

new_list += [a[i] + b[i]]:
corrects the above because it concatenates a list instead of an integer. 

Once again thank you for the help.  I appreciate it. 

I'll continue plucking through this book and will work at reading and debugging more carefully.

Name: Anonymous 2010-05-19 9:41

You don't need outdated book, op. It's not /lit/.
What you need is not so outdated http://docs.python.org/tutorial/

Name: Anonymous 2010-05-19 11:42

>>11
is incorrect because I was asking for the element of a tuple instead of a list.
No. Tuples are accessed in the same way as lists. What you were doing was trying to call those lists as if they were functions, with the argument i. Read your fucking error messages.

Also: add_lists = lambda a, b: reduce(lambda n, (a, b): n + [a + b], zip(a, b), [])

Name: Anonymous 2010-05-19 16:11

>>>1

(define (add-lists a b)
  (map + a b))
or
(define (add-lists a b)
  (list-ec (:parallel (:list i a)
                      (:list j b))
           (+ i j)))

Name: Anonymous 2010-05-19 16:54

>>4,6
* Your
* Your
fucking idiots

OP, see >>14.

Name: Anonymous 2010-05-19 17:46

>>15 That's the point
if __name__ == '__main__':
FIOC MY ANUS

Name: Anonymous 2010-05-19 17:55

>>16
if __name__ == '__main__':
Can someone explain wtf this is about?

I have a misbehaving script with this particular test (which seems to be failing.) I'm not an FIOC programmer so it's not clear to me what this is about.

Name: Anonymous 2010-05-19 18:08

>>17
It means that the code will only be executed if you run that file directly (i.e. as "__main__"), but won't run if you use that file as a library. It's useful for tests.

Name: Anonymous 2010-05-19 18:15

>>18
Ah, thanks. Now I'm going to try something dangerous.

Name: Anonymous 2010-05-19 19:21

>>12
Thank you.  Right now I'm enjoying the open source:

http://openbookproject.net/thinkcs/python/english2e/

It seems to be doing well so far and I'll finish it before moving on to another.

>>13
Ahh, thanks for correcting me on that.  So to be clear, I'd call a tuple the same way?  ie: "tuple[index]"

Name: Anonymous 2010-05-19 19:43

>>20
You know about the REPL, right? Try it yourself.
All you need to know to learn Python is dir() and __doc__.

Name: Anonymous 2010-05-19 21:50

>>21
--- __doc__
+++ help()

Name: Anonymous 2010-05-19 22:06

>>21
no, I haven't delved into REPL yet.  You recommend I go into it before Python?

Name: Anonymous 2010-05-19 22:15

RECOMMEND MY ANUS

Name: Anonymous 2010-05-19 22:16

>>23
Since you seem to be fundamentally incapable of looking anything up, I suggest you just give up programming altogether.

Name: Anonymous 2010-05-19 22:18

>>23
You should try >>24's anus.

Name: Anonymous 2010-05-19 22:19

SUGGEST MY ANUS

Name: Anonymous 2010-05-19 22:19

>>27
Hey, I already recommended it, what more do you want from me?!

Name: Anonymous 2010-05-20 7:08

>>1
(mapcar #'+ a b)

Name: Anonymous 2010-05-20 7:50

>>30
my other mapcar is a mapcdr

Name: Anonymous 2010-05-20 12:25

OP is a huge idiot who deserves to fail every programming exam on his whole life.

That, or one of the best trolls we have had recently. Which means our trolls are quite poor quality indeed.

Name: Anonymous 2010-05-20 16:27

>>32
Clearly a troll. Nobody's that dumb.

Name: Anonymous 2010-05-20 19:16

>>32
>>33
not a troll... yes it was an obvious mistake

Name: Anonymous 2010-11-27 18:54

Don't change these.
Name: Email:
Entire Thread Thread List