周末放松下,实现小时候铅笔盒上的九九乘法表

14 views
Skip to first unread message

zanpen2000

unread,
Sep 19, 2009, 10:17:42 AM9/19/09
to python-cn
这里有一句话的代码

print "\t".join([('%s*%s=%s' % (x,y,x*y)) for x in range(1,10) for y in
range(1,10) if x <= y])

现在的输出形式如下:

1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*2=4 2*3=6 2*4=8
2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21
3*8=24 3*9=27 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*5=25 5*6=30
5*7=35 5*8=40 5*9=45 6*6=36 6*7=42 6*8=48 6*9=54 7*7=49 7*8=56 7*9=63
8*8=64 8*9=72 9*9=81


能不能给改成小时候铅笔盒上的形式?


www.python8.org 资料下载,分类文档

limodou

unread,
Sep 19, 2009, 10:20:31 AM9/19/09
to pyth...@googlegroups.com
2009/9/19 zanpen2000 <zanpe...@gmail.com>:

非要写一行嘛?多写几行不就行了。

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: http://uliwebproject.appspot.com
My Blog: http://hi.baidu.com/limodou

Leo Jay

unread,
Sep 19, 2009, 10:30:16 AM9/19/09
to pyth...@googlegroups.com
2009/9/19 zanpen2000 <zanpe...@gmail.com>:

print "".join([('%s*%s=%s%s' % (y,x,x*y,'\n' if x==y else '\t')) for x


in range(1,10) for y in range(1,10) if x >= y])

输出:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

--
Best Regards,
Leo Jay

zanpen2000

unread,
Sep 19, 2009, 10:36:21 AM9/19/09
to pyth...@googlegroups.com
我这绕了有一会儿了,还是你行

在 2009-09-19六的 22:30 +0800,Leo Jay写道:

www.python8.org 资料下载,分类文档

反过来走走

unread,
Sep 19, 2009, 10:37:50 AM9/19/09
to pyth...@googlegroups.com
cool!

2009/9/19 Leo Jay <python...@gmail.com>

Upsuper

unread,
Sep 19, 2009, 11:50:02 PM9/19/09
to pyth...@googlegroups.com
'\n' if x==y else '\t' ? 没见过这种形式哦……
不应该用 x==y and '\n' or '\t' ?

2009/9/19 Leo Jay <python...@gmail.com>

zanpen2000

unread,
Sep 19, 2009, 11:54:08 PM9/19/09
to pyth...@googlegroups.com
我试过了,确实能工作

在 2009-09-20日的 11:50 +0800,Upsuper写道:
www.python8.org 资料下载,分类文档

Upsuper

unread,
Sep 20, 2009, 12:12:55 AM9/20/09
to pyth...@googlegroups.com
觉得比较奇怪就是了……还是应该推荐and or的形式吧……比较符合pythonic的说

2009/9/20 zanpen2000 <zanpe...@gmail.com>

Leo Jay

unread,
Sep 20, 2009, 2:09:38 AM9/20/09
to pyth...@googlegroups.com
2009/9/20 Upsuper <quanx...@gmail.com>:

> '\n' if x==y else '\t' ? 没见过这种形式哦……
> 不应该用 x==y and '\n' or '\t' ?
>

这个是python 2.5新加的条件表达式:
http://www.python.org/dev/peps/pep-0308/

刘鑫

unread,
Sep 20, 2009, 2:21:52 AM9/20/09
to pyth...@googlegroups.com


2009/9/20 Upsuper <quanx...@gmail.com>
觉得比较奇怪就是了……还是应该推荐and or的形式吧……比较符合pythonic的说
相反,在Python 2.5之后,推荐使用 if else 表达式(我个人倒是不太在意用哪种,从英语的角度好像两种都说得通。)



--
光见贼吃肉,没见贼挨打。
……

劉鑫
March.Liu

PT

unread,
Sep 20, 2009, 3:46:02 AM9/20/09
to python-cn`CPyUG`华蟒用户组(中文Py用户组)
and or作为条件判断准确来说是一种hack,有if else这种清晰表达,何必还用and or呢

On 9月20日, 下午12时12分, Upsuper <quanxunz...@gmail.com> wrote:
> 觉得比较奇怪就是了......还是应该推荐and or的形式吧......比较符合pythonic的说
>
> 2009/9/20 zanpen2000 <zanpen2...@gmail.com>
>
>
>
> > 我试过了,确实能工作
>
> > 在 2009-09-20日的 11:50 +0800,Upsuper写道:
> > > '\n' if x==y else '\t' ? 没见过这种形式哦......
> > > 不应该用 x==y and '\n' or '\t' ?
>
> > > 2009/9/19 Leo Jay <python.leo...@gmail.com>
> > > 2009/9/19 zanpen2000 <zanpen2...@gmail.com>:

zanpen2000

unread,
Sep 20, 2009, 5:39:58 AM9/20/09
to pyth...@googlegroups.com
and 和 or 太绕了我觉得。 还是 if..else 清晰。

在 2009-09-20日的 14:09 +0800,Leo Jay写道:


> 2009/9/20 Upsuper <quanx...@gmail.com>:
> > '\n' if x==y else '\t' ? 没见过这种形式哦……
> > 不应该用 x==y and '\n' or '\t' ?
> >
>
> 这个是python 2.5新加的条件表达式:
> http://www.python.org/dev/peps/pep-0308/
>
>

www.python8.org 资料下载,分类文档

Iceberg

unread,
Sep 20, 2009, 10:50:36 PM9/20/09
to python-cn`CPyUG`华蟒用户组(中文Py用户组)

On 9月20日, 下午2时21分, 刘鑫 <march....@gmail.com> wrote:

> >> 在 2009-09-20日的 11:50 +0800,Upsuper写道:
> >> > '\n' if x==y else '\t' ? 没见过这种形式哦……
> >> > 不应该用 x==y and '\n' or '\t' ?

> > 觉得比较奇怪就是了……还是应该推荐and or的形式吧……比较符合pythonic的说
>
> 相反,在Python 2.5之后,推荐使用 if else 表达式(我个人倒是不太在意用哪种,从英语的角度好像两种都说得通。)
>

and or形式只是一种晦涩的hack,绝对不能称得上pythonic,只是条件所限不得已而为之。事实上它还有一个危险的隐患。例如:
x = 1
y = 2
result = condition and x or y
这里设计者的本意通常应该是如果condition为真就让result取值为x,反之取值为y,对吧?但设想一下,如果condition确实为真,
但x的值碰巧是0呢?最终result会被赋成y。

刘老师说得对,python2.5之后,应该使用官方的result = x if condition else y的形式,就完全不存在上述隐
患。

另外,也请刘老师容许我这样说,从英语的角度,只有if else的形式符合日常语法。and or的方式在日常英语里是不会这样用的。:-)

Iceberg

刘鑫

unread,
Sep 20, 2009, 10:55:37 PM9/20/09
to pyth...@googlegroups.com


2009/9/21 Iceberg <ice...@21cn.com>

以前我也这么觉得啊,后来还真见过有写老外这么写文章,当然这个句式没有 if else那么通用,就是那么一两回,在比较特定的上下文里……
你想,我这英语水平,也不可能读多深的文章吧,一般也就新概念那程度了XD

Iceberg

瑞依(lwkyy)

unread,
Sep 20, 2009, 11:03:51 PM9/20/09
to pyth...@googlegroups.com
'\n' if x==y else '\t'这是2.5以后的版本加入的三目运算方法,我前天在看python核心编辑看到了,呵呵。

2009/9/21 刘鑫 <marc...@gmail.com>

pan shizhu

unread,
Sep 23, 2009, 9:42:23 PM9/23/09
to pyth...@googlegroups.com
不是pythonic的问题,对于编程来说首先是正确,其次是好看。

and or 的形式只要中间表达式为零,结果就是错误的。所以这种形式根本不能用,也不应该用。在编程的正确性面前,我觉得别的理由都是扯淡吧。

李海东

unread,
Sep 25, 2009, 5:51:50 AM9/25/09
to pyth...@googlegroups.com
for i in range(1, 10):
    for j in range(1, i+1):
        print '%d*%d=%2d ' % (j,i,i*j),
    print ''

2009/9/24 pan shizhu <pan.s...@gmail.com>


Harris Lee@SH
ERP/Retail/Python

victor lee

unread,
Sep 25, 2009, 5:58:30 AM9/25/09
to pyth...@googlegroups.com
那种隐患貌似(condition and [x] or [y])[0]就可以了。

2009/9/25 李海东 <harris...@gmail.com>
Reply all
Reply to author
Forward
0 new messages