Python论坛  - 讨论区

标题:[python-chinese] 如何最简的判断4个数之中有没有相等的数?

2005年06月07日 星期二 15:53

cr999 cr999 at ir.hit.edu.cn
Tue Jun 7 15:53:40 HKT 2005

Print后加“,”


-----邮件原件-----
发件人: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] 代表 Neo Chan
(netkiller)
发送时间: 2005年6月7日 14:37
收件人: python-chinese at lists.python.cn
主题: [python-chinese] 如何输出.

 
我用 
for  
 print 
输出,print自动在后右加\r\n 我如何处理.我想在一行输出.

Neo Chan
Callsign: BG7NYT
73

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

2005年06月07日 星期二 15:56

Qiangning Hong hongqn at gmail.com
Tue Jun 7 15:56:35 HKT 2005

Neo Chan (netkiller) wrote:
>  
> 我用 
> for  
>  print 
> 输出,print自动在后右加\r\n 我如何处理.我想在一行输出.

在print的最后加上逗号

print 'abc',
print 'def',

或者用sys.stdout.write()

-- 
Qiangning Hong

 ________________________________________________________
/ No excellent soul is exempt from a mixture of madness. \
|                                                        |
\ -- Aristotle                                           /
 --------------------------------------------------------
        \  ^___^
         \ (@@@)\_______
           (___)\       )\/\
                ||----w |
                ||     ||

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

2005年06月07日 星期二 16:03

cpunion cpunion at 263.net
Tue Jun 7 16:03:25 HKT 2005

可以先调用string.join连接成一个字符串再输出

Neo Chan (netkiller) wrote:

> 已经解决了..
> 目前没解决 print 在一行输出问题..print他自动加了一个\r\n
>  
>
> Neo Chan
> Callsign: BG7NYT
> 73
>
>  
>
> ------------------------------------------------------------------------
> *From:* python-chinese-bounces at lists.python.cn 
> [mailto:python-chinese-bounces at lists.python.cn] *On Behalf Of *spark
> *Sent:* Tuesday, June 07, 2005 2:26 PM
> *To:* python-chinese at lists.python.cn
> *Subject:* Re: [python-chinese] 急问一个问题.
>
> >>> a='netkiller'
> >>> list(a)
> ['n', 'e', 't', 'k', 'i', 'l', 'l', 'e', 'r']
> 不明白??直接不就有了阿
>
>     ----- Original Message -----
>     *From:* Neo Chan (netkiller) neo.chen at achievo.com>
>     *To:* python-chinese at lists.python.cn
>     python-chinese at lists.python.cn>
>     *Sent:* Tuesday, June 07, 2005 1:47 PM
>     *Subject:* [python-chinese] 急问一个问题.
>
>     一段文本.将每个字母做为list一个元素???
>     a='netkiller'
>     我要得到是这样的
>     ('n','e','t','k'......'e','r')
>      
>
>     Neo Chan
>     Callsign: BG7NYT
>     73
>
>      
>
>     ------------------------------------------------------------------------
>     *From:* python-chinese-bounces at lists.python.cn
>     python-chinese-bounces at lists.python.cn>
>     [mailto:python-chinese-bounces at lists.python.cn] *On Behalf Of *Bo Yang
>     *Sent:* Tuesday, June 07, 2005 1:33 PM
>     *To:* python-chinese at lists.python.cn
>     python-chinese at lists.python.cn>
>     *Subject:* Re: [python-chinese] 如何最简的判断4个数之中有没有相等的数?
>
>     def unique (numbers):
>     return len(set(numbers))==len(numbers)
>
>
>     - ahbei
>     ( http://blog.douban.com)
>
>     On Jun 7, 2005, at 1:07 PM, fff 670 wrote:
>
>         这样可以,可要用到FOR遍历[A,B.....]
>         不知道有没有更简的方法?
>         _______________________________________________
>         python-chinese list
>         python-chinese at lists.python.cn
>         http://python.cn/mailman/listinfo/python-chinese
>
>     ------------------------------------------------------------------------
>     _______________________________________________
>     python-chinese list
>     python-chinese at lists.python.cn
>     http://python.cn/mailman/listinfo/python-chinese
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>  
>

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

2005年06月07日 星期二 16:38

Qiangning Hong hongqn at gmail.com
Tue Jun 7 16:38:17 HKT 2005

Neo Chan (netkiller) wrote:
>  
> #the Morse code
> #author: neo.chan(at)achievo.com

[snip program code]

用dict替代大量的if语句可以使执行效率提高。
我帮你改了一个版本,供你参考。(btw, 为什么0和5的morse code是一样的啊?)


import string
import time
import winsound

MORSE_CODE = {
    '1': '.____',
    '2': '..___',
    '3': '...__',
    '4': '...._',
    '5': '.....',
    '6': '_....',
    '7': '__...',
    '8': '___..',
    '9': '____.',
    '0': '.....',

    'A': '._',
    'B': '_...',
    'C': '_._.',
    'D': '_..',
    'E': '.',
    'F': '.._.',
    'G': '__.',
    'H': '....',
    'I': '..',
    'J': '.___',
    'K': '_._',
    'L': '._..',
    'M': '__',
    'N': '_.',
    'O': '___',
    'P': '.__.',
    'Q': '__._',
    'R': '._.',
    'S': '...',
    'T': '_',
    'U': '.._',
    'V': '..._',
    'W': '.__',
    'X': '_.._',
    'Y': '_.__',
    'Z': '__..',

    '?': '..__..',
    '/': '_.._.',
    '(': '_.__._',
    ')': '_....',
    '-': '_...._',
    '.': '._._._',
    '@': '.__._.',
}


class Morse(object):
    def beep(self, code):
        for x in code:
            if x == '.':
                winsound.Beep(400, 100)
            elif x == '_':
                winsound.Beep(400, 300)
            else:
                raise Exception, 'Unknown code'
            winsound.Beep(37, 150)

    def reader(self, text):
        for char in text:
            uchar = char.upper()
            code = MORSE_CODE.get(uchar, '')
            if code:
                self.beep(code)
            print char, ':', code
            time.sleep(0.5)

#Main Program

def main():
    doc = """
Python is an easy to learn, powerful programming language.
It has efficient high-level data structures and a simple
but effective approach to object-oriented programming.
Python's elegant syntax and dynamic typing,
together with its interpreted nature,
make it an ideal language for scripting and
rapid application development in many areas on most platforms.
"""

    cw = Morse()
    cw.reader(doc)

if __name__ == '__main__':
    main()


-- 
Qiangning Hong

 ___________________________
( 2 Variable not found, 0:1 )
 ---------------------------
  o                                  ,+*^^*+___+++_
   o                           ,*^^^^              )
    o                       _+*                     ^**+_
     o                    +^       _ _++*+_+++_,         )
              _+^^*+_    (     ,+*^ ^          \+_        )
             {       )  (    ,(    ,_+--+--,      ^)      ^\
            { (@)    } f   ,(  ,+-^ __*_*_  ^^\_   ^\       )
           {:;-/    (_+*-+^^^^^+*+*<_ _++_)_    )    )      /
          ( /  (    (        ,___    ^*+_+* )   <    <      \
           U _/     )    *--<  ) ^\-----++__)   )    )       )
            (      )  _(^)^^))  )  )\^^^^^))^*+/    /       /
          (      /  (_))_^)) )  )  ))^^^^^))^^^)__/     +^^
         (     ,/    (^))^))  )  ) ))^^^^^^^))^^)       _)
          *+__+*       (_))^)  ) ) ))^^^^^^))^^^^^)____*^
          \             \_)^)_)) ))^^^^^^^^^^))^^^^)
           (_             ^\__^^^^^^^^^^^^))^^^^^^^)
             ^\___            ^\__^^^^^^))^^^^^^^^)\\
                  ^^^^^\uuu/^^\uuu/^^^^\^\^\^\^\^\^\^\
                     ___) >____) >___   ^\_\_\_\_\_\_\)
                    ^^^//\\_^^//\\_^       ^(\_\_\_\)
                      ^^^ ^^ ^^^ ^

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

2005年06月07日 星期二 16:53

torres torreswang at gmail.com
Tue Jun 7 16:53:14 HKT 2005

","应该是在最后啊!

在05-6-7,cr999 <cr999 at ir.hit.edu.cn> 写道: 
> 
> Print后加","
> 
> -----邮件原件-----
> 发件人: python-chinese-bounces at lists.python.cn
> [mailto:python-chinese-bounces at lists.python.cn] 代表 Neo Chan
> (netkiller)
> 发送时间: 2005年6月7日 14:37
> 收件人: python-chinese at lists.python.cn
> 主题: [python-chinese] 如何输出.
> 
> 我用
> for
> print
> 输出,print自动在后右加\r\n 我如何处理.我想在一行输出.
> 
> Neo Chan
> Callsign: BG7NYT
> 73
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 


-- 
your friends
torres
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050607/9723e484/attachment.htm

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

2005年06月07日 星期二 17:17

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 17:17:40 HKT 2005

非常好,谢谢:)
我开始也想到了用DICT 不是很熟.就用了if:)

改的不错,还考虑了异常。raise Exception, 'Unknown code'

Neo Chan (netkiller)
Callsign: BG7NYT
73
-----Original Message-----
From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of Qiangning Hong
Sent: Tuesday, June 07, 2005 4:38 PM
To: python-chinese at lists.python.cn
Subject: Re: [python-chinese] the Morse code 小玩具:)写完了.

Neo Chan (netkiller) wrote:
>  
> #the Morse code
> #author: neo.chan(at)achievo.com

[snip program code]

用dict替代大量的if语句可以使执行效率提高。
我帮你改了一个版本,供你参考。(btw, 为什么0和5的morse code是一样的啊?)


import string
import time
import winsound

MORSE_CODE = {
    '1': '.____',
    '2': '..___',
    '3': '...__',
    '4': '...._',
    '5': '.....',
    '6': '_....',
    '7': '__...',
    '8': '___..',
    '9': '____.',
    '0': '.....',

    'A': '._',
    'B': '_...',
    'C': '_._.',
    'D': '_..',
    'E': '.',
    'F': '.._.',
    'G': '__.',
    'H': '....',
    'I': '..',
    'J': '.___',
    'K': '_._',
    'L': '._..',
    'M': '__',
    'N': '_.',
    'O': '___',
    'P': '.__.',
    'Q': '__._',
    'R': '._.',
    'S': '...',
    'T': '_',
    'U': '.._',
    'V': '..._',
    'W': '.__',
    'X': '_.._',
    'Y': '_.__',
    'Z': '__..',

    '?': '..__..',
    '/': '_.._.',
    '(': '_.__._',
    ')': '_....',
    '-': '_...._',
    '.': '._._._',
    '@': '.__._.',
}


class Morse(object):
    def beep(self, code):
        for x in code:
            if x == '.':
                winsound.Beep(400, 100)
            elif x == '_':
                winsound.Beep(400, 300)
            else:
                raise Exception, 'Unknown code'
            winsound.Beep(37, 150)

    def reader(self, text):
        for char in text:
            uchar = char.upper()
            code = MORSE_CODE.get(uchar, '')
            if code:
                self.beep(code)
            print char, ':', code
            time.sleep(0.5)

#Main Program

def main():
    doc = """
Python is an easy to learn, powerful programming language.
It has efficient high-level data structures and a simple but effective
approach to object-oriented programming.
Python's elegant syntax and dynamic typing, together with its interpreted
nature, make it an ideal language for scripting and rapid application
development in many areas on most platforms.
"""

    cw = Morse()
    cw.reader(doc)

if __name__ == '__main__':
    main()


--
Qiangning Hong

 ___________________________
( 2 Variable not found, 0:1 )
 ---------------------------
  o                                  ,+*^^*+___+++_
   o                           ,*^^^^              )
    o                       _+*                     ^**+_
     o                    +^       _ _++*+_+++_,         )
              _+^^*+_    (     ,+*^ ^          \+_        )
             {       )  (    ,(    ,_+--+--,      ^)      ^\
            { (@)    } f   ,(  ,+-^ __*_*_  ^^\_   ^\       )
           {:;-/    (_+*-+^^^^^+*+*<_ _++_)_    )    )      /
          ( /  (    (        ,___    ^*+_+* )   <    <      \
           U _/     )    *--<  ) ^\-----++__)   )    )       )
            (      )  _(^)^^))  )  )\^^^^^))^*+/    /       /
          (      /  (_))_^)) )  )  ))^^^^^))^^^)__/     +^^
         (     ,/    (^))^))  )  ) ))^^^^^^^))^^)       _)
          *+__+*       (_))^)  ) ) ))^^^^^^))^^^^^)____*^
          \             \_)^)_)) ))^^^^^^^^^^))^^^^)
           (_             ^\__^^^^^^^^^^^^))^^^^^^^)
             ^\___            ^\__^^^^^^))^^^^^^^^)\\
                  ^^^^^\uuu/^^\uuu/^^^^\^\^\^\^\^\^\^\
                     ___) >____) >___   ^\_\_\_\_\_\_\)
                    ^^^//\\_^^//\\_^       ^(\_\_\_\)
                      ^^^ ^^ ^^^ ^
_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Neo Chan.vcf
Type: text/x-vcard
Size: 1081 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20050607/509f6c96/NeoChan.vcf

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

2005年06月07日 星期二 17:22

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 17:22:09 HKT 2005

Skipped content of type multipart/mixed-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3066 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20050607/9f23ed8e/smime.bin

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

2005年06月07日 星期二 18:12

Qiangning Hong hongqn at gmail.com
Tue Jun 7 18:12:52 HKT 2005

Neo Chan (netkiller) wrote:
> 请教,DICT 可以这样用吗?
> 
> MORSE_CODE = {
>     '1': (0,1,1,1,1),
> ???

完全可以。

> 如果不行我就
> MORSE_CODE = {
>     '1': `0,1,1,1,1`,
> 因为用".","_" 时同时出现两次"__" 就有点乱了.:)

[snip quotations of previous mails]

-- 
Qiangning Hong


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

2005年06月07日 星期二 18:54

Zoom Quiet zoom.quiet at gmail.com
Tue Jun 7 18:54:47 HKT 2005

哈哈哈!!好玩!!
充分体现了Python 的快速实现想法的本质!!
本地运行成功!!
非常的好!

在 05-6-7,Qiangning Hong<hongqn at gmail.com> 写道:
> Neo Chan (netkiller) wrote:
> > 请教,DICT 可以这样用吗?
> >
> > MORSE_CODE = {
> >     '1': (0,1,1,1,1),
> > ???
> 
> 完全可以。
> 
> > 如果不行我就
> > MORSE_CODE = {
> >     '1': `0,1,1,1,1`,
> > 因为用".","_" 时同时出现两次"__" 就有点乱了.:)
> 
> [snip quotations of previous mails]
> 
> --
> Qiangning Hong
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
[Time is unimportant, only life important!]

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

2005年06月08日 星期三 13:26

Eric Hu eric.hu72 at gmail.com
Wed Jun 8 13:26:57 HKT 2005

在 05-6-7,Qiangning Hong<hongqn at gmail.com> 写道:
> Neo Chan (netkiller) wrote:
> >
> > #the Morse code
> > #author: neo.chan(at)achievo.com
> 
> [snip program code]
> 
> 用dict替代大量的if语句可以使执行效率提高。
> 我帮你改了一个版本,供你参考。(btw, 为什么0和5的morse code是一样的啊?)

回答你的这个问题:因为原来的程序写错了。正确的morse code是:
0 -> '_____'
5 -> '.....'

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号