2007年07月02日 星期一 16:57
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, inmain() 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
2007年07月02日 星期一 19:46
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
2007年07月02日 星期一 20:23
[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
2007年07月02日 星期一 21:00
ÓÖÁ˽⵽һ¸ö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
2007年07月03日 星期二 09:28
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 > > 谢谢。 > >
2007年07月03日 星期二 10:30
其实出错信息和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
Zeuux © 2025
京ICP备05028076号