Python论坛  - 讨论区

标题:[python-chinese] 关于sum的行为。

2007年07月02日 星期一 16:57

Leo Jay python.leojay在gmail.com
星期一 七月 2 16:57:56 HKT 2007

sum难道不是对list的每个元素做加法吗?
那为什么:
m = [1,2,3]
n = [2,3,4]
print m + n
的结果是[1, 2, 3, 2, 3, 4],而
print sum([m, n])却是出错?
Traceback (most recent call last):
  File "t.py", line 9, in 
    main()
  File "t.py", line 6, in main
    print sum([m, n])
TypeError: unsupported operand type(s) for +: 'int' and 'list'
shell returned 1

谢谢。

-- 
Best Regards,
Leo Jay

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月02日 星期一 19:46

Li Qingfeng liqfemail在gmail.com
星期一 七月 2 19:46:20 HKT 2007

Help on built-in function sum in module __builtin__:

sum(...)
    sum(sequence, start=0) -> value

    Returns the sum of a sequence of numbers (NOT strings) plus the value
    of parameter 'start'.  When the sequence is empty, returns start.
»¹ÊÇÓÃÒÔÏ·½·¨°É£º
m = [1,2,3]
n = [2,3,4]
result_list = []
for i in range(len(m)):
    result_list.append(m[i]+n[i])
print result_list


2007/7/2, Leo Jay <python.leojay在gmail.com>:
>
> sumÄѵÀ²»ÊǶÔlistµÄÿ¸öÔªËØ×ö¼Ó·¨Âð£¿
> ÄÇΪʲô£º
> m = [1,2,3]
> n = [2,3,4]
> print m + n
> µÄ½á¹ûÊÇ[1, 2, 3, 2, 3, 4]£¬¶ø
> print sum([m, n])È´Êdzö´í?
> Traceback (most recent call last):
> File "t.py", line 9, in 
>    main()
> File "t.py", line 6, in main
>    print sum([m, n])
> TypeError: unsupported operand type(s) for +: 'int' and 'list'
> shell returned 1
>
> лл¡£
>
> --
> Best Regards,
> Leo Jay
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070702/5d17da28/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月02日 星期一 20:23

limon lin limon7在gmail.com
星期一 七月 2 20:23:54 HKT 2007

[i+j for i, j in zip(m,n)]


在07-7-2,Li Qingfeng <liqfemail at gmail.com> 写道:
>
> Help on built-in function sum in module __builtin__:
>
> sum(...)
>     sum(sequence, start=0) -> value
>
>     Returns the sum of a sequence of numbers (NOT strings) plus the value
>     of parameter 'start'.  When the sequence is empty, returns start.
> 还是用以下方法吧:
> m = [1,2,3]
> n = [2,3,4]
> result_list = []
> for i in range(len(m)):
>     result_list.append(m[i]+n[i])
> print result_list
>
>
> 2007/7/2, Leo Jay <python.leojay at gmail.com>:
> >
> > sum难道不是对list的每个元素做加法吗?
> > 那为什么:
> > m = [1,2,3]
> > n = [2,3,4]
> > print m + n
> > 的结果是[1, 2, 3, 2, 3, 4],而
> > print sum([m, n])却是出错?
> > Traceback (most recent call last):
> > File "t.py", line 9, in 
> >    main()
> > File "t.py", line 6, in main
> >    print sum([m, n])
> > TypeError: unsupported operand type(s) for +: 'int' and 'list'
> > shell returned 1
> >
> > 谢谢。
> >
> > --
> > Best Regards,
> > Leo Jay
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070702/56cb68b7/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月02日 星期一 21:00

Li Qingfeng liqfemail在gmail.com
星期一 七月 2 21:00:30 HKT 2007

ÓÖÁ˽⵽һ¸özip()º¯Êý£¬²»´í¡£

ÔÚ07-7-2£¬limon lin <limon7在gmail.com> дµÀ£º
>
> [i+j for i, j in zip(m,n)]
>
>
> ÔÚ07-7-2£¬Li Qingfeng <liqfemail在gmail.com> дµÀ£º
> >
> >  Help on built-in function sum in module __builtin__:
> >
> > sum(...)
> >     sum(sequence, start=0) -> value
> >
> >     Returns the sum of a sequence of numbers (NOT strings) plus the
> > value
> >     of parameter 'start'.  When the sequence is empty, returns start.
> > »¹ÊÇÓÃÒÔÏ·½·¨°É£º
> > m = [1,2,3]
> > n = [2,3,4]
> > result_list = []
> > for i in range(len(m)):
> >     result_list.append(m[i]+n[i])
> > print result_list
> >
> >
> > 2007/7/2, Leo Jay <python.leojay在gmail.com>:
> > >
> > > sumÄѵÀ²»ÊǶÔlistµÄÿ¸öÔªËØ×ö¼Ó·¨Âð£¿
> > > ÄÇΪʲô£º
> > > m = [1,2,3]
> > > n = [2,3,4]
> > > print m + n
> > > µÄ½á¹ûÊÇ[1, 2, 3, 2, 3, 4]£¬¶ø
> > > print sum([m, n])È´Êdzö´í?
> > > Traceback (most recent call last):
> > > File "t.py", line 9, in 
> > >    main()
> > > File "t.py", line 6, in main
> > >    print sum([m, n])
> > > TypeError: unsupported operand type(s) for +: 'int' and 'list'
> > > shell returned 1
> > >
> > > лл¡£
> > >
> > > --
> > > Best Regards,
> > > Leo Jay
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070702/7ce161ae/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月03日 星期二 09:28

jinq0123在163.com jinq0123在163.com
星期二 七月 3 09:28:47 HKT 2007

sum(m + n)

Leo Jay wrote:
> sum难道不是对list的每个元素做加法吗?
> 那为什么:
> m = [1,2,3]
> n = [2,3,4]
> print m + n
> 的结果是[1, 2, 3, 2, 3, 4],而
> print sum([m, n])却是出错?
> Traceback (most recent call last):
>   File "t.py", line 9, in 
>     main()
>   File "t.py", line 6, in main
>     print sum([m, n])
> TypeError: unsupported operand type(s) for +: 'int' and 'list'
> shell returned 1
>
> 谢谢。
>
>   




[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月03日 星期二 10:30

helium helium.sun在gmail.com
星期二 七月 3 10:30:40 HKT 2007

其实出错信息和sum的定义已经告诉了你
Help on built-in function sum in module __builtin__:

sum(...)
    sum(sequence, start=0) -> value

    Returns the sum of a sequence of numbers (NOT strings) plus the value
    of parameter 'start'.  When the sequence is empty, returns start.

sum([a,b])不是a+b,而是(start+a)+b,而start缺省是0,所以会报int和list相加的类型错误
正确的应该是 sum([a,b], [])


On 7/2/07, Leo Jay <python.leojay at gmail.com> wrote:
> sum难道不是对list的每个元素做加法吗?
> 那为什么:
> m = [1,2,3]
> n = [2,3,4]
> print m + n
> 的结果是[1, 2, 3, 2, 3, 4],而
> print sum([m, n])却是出错?
> Traceback (most recent call last):
>   File "t.py", line 9, in 
>     main()
>   File "t.py", line 6, in main
>     print sum([m, n])
> TypeError: unsupported operand type(s) for +: 'int' and 'list'
> shell returned 1
>
> 谢谢。
>
> --
> Best Regards,
> Leo Jay
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号