Python论坛  - 讨论区

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

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

fff 670 fff670 at gmail.com
Tue Jun 7 12:53:07 HKT 2005

判断4个数或更多个数之中有没有相等的,
有为真,没有为假
可不可以用一条语句就表达,
或有没有现成的函数可用?

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

2005年06月06日 星期一 14:17

Petr Šimon info at klubko.net
Mon Jun 6 14:17:27 HKT 2005

> 一段文本.将每个字母做为list一个元素???
> a='netkiller'
> 我要得到是这样的
> ('n','e','t','k'......'e','r')
tuple(a)

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

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

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 12:57:47 HKT 2005

 
IF 'A' IN ('A','B'........)
看看这样用行不行???

Neo Chan
Callsign: BG7NYT
73
-----Original Message-----
From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of fff 670
Sent: Tuesday, June 07, 2005 12:53 PM
To: python-chinese at lists.python.cn
Subject: [python-chinese] 如何最简的判断4个数之中有没有相等的数?

判断4个数或更多个数之中有没有相等的,
有为真,没有为假
可不可以用一条语句就表达,
或有没有现成的函数可用?
-------------- 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/eab160a2/NeoChan.vcf

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

2005年06月07日 星期二 13:00

cpunion cpunion at 263.net
Tue Jun 7 13:00:20 HKT 2005

假如存放在一个容器中:
a = [1, 2, 3, 3, 4]
b = len(a) != len(set(a))
print b and "有相等的数" or "没有相等的数"

因为set会消除同等的数,所以通过长度判断就知道了。

fff 670 wrote:

>判断4个数或更多个数之中有没有相等的,
>有为真,没有为假
>可不可以用一条语句就表达,
>或有没有现成的函数可用?
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>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日 星期二 13:07

fff 670 fff670 at gmail.com
Tue Jun 7 13:07:02 HKT 2005

这样可以,可要用到FOR遍历[A,B.....]
不知道有没有更简的方法?

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

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

fff 670 fff670 at gmail.com
Tue Jun 7 13:15:36 HKT 2005

如果要更进一步找出相等的数呢?

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

2005年06月07日 星期二 13:32

Bo Yang boyang at mac.com
Tue Jun 7 13:32:41 HKT 2005

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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 572 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20050607/4daf34f6/attachment.bin

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

2005年06月07日 星期二 13:37

Bo Yang boyang at mac.com
Tue Jun 7 13:37:31 HKT 2005

:) 想到一起去了。

- ahbei
(http://blog.douban.com)

On Jun 7, 2005, at 1:00 PM, cpunion wrote:

> 假如存放在一个容器中:
> a = [1, 2, 3, 3, 4]
> b = len(a) != len(set(a))
> print b and "有相等的数" or "没有相等的数"
>
> 因为set会消除同等的数,所以通过长度判断就知道了。
>
> fff 670 wrote:
>
>> 判断4个数或更多个数之中有没有相等的,
>> 有为真,没有为假
>> 可不可以用一条语句就表达,
>> 或有没有现成的函数可用?
>>
>> ---------------------------------------------------------------------- 
>> --
>>
>> _______________________________________________
>> 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
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1435 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20050607/7346b49d/attachment.bin

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

2005年06月07日 星期二 13:46

cpunion cpunion at 263.net
Tue Jun 7 13:46:18 HKT 2005

 >>> a = [1, 2, 2, 3, 3, 4, 5, 6, 6, 7]
 >>> b = set(a)
 >>> a
[1, 2, 2, 3, 3, 4, 5, 6, 6, 7]
 >>> b
set([1, 2, 3, 4, 5, 6, 7])
 >>> c = [i for i in a if i not in b or b.remove(i) and False]
 >>> c
[2, 3, 6]

哈哈,用set效率应该还不算低吧,remove应该也比较快的,如果你自己用循环查 
找而没有排序,我相信没有set快。

fff 670 wrote:

>如果要更进一步找出相等的数呢?
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>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日 星期二 13:47

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 13:47:45 HKT 2005

Skipped content of type multipart/alternative-------------- 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/d5a238d8/NeoChan-0001.vcf

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

2005年06月07日 星期二 13:49

cpunion cpunion at 263.net
Tue Jun 7 13:49:10 HKT 2005

哦,set的remove方法没有返回值,所以后面的and False可以去掉:

c = [i for i in a if i not in b or b.remove(i)]

cpunion wrote:

> >>> a = [1, 2, 2, 3, 3, 4, 5, 6, 6, 7]
> >>> b = set(a)
> >>> a
> [1, 2, 2, 3, 3, 4, 5, 6, 6, 7]
> >>> b
> set([1, 2, 3, 4, 5, 6, 7])
> >>> c = [i for i in a if i not in b or b.remove(i) and False]
> >>> c
> [2, 3, 6]
>
> 哈哈,用set效率应该还不算低吧,remove应该也比较快的,如果你自己用循环 
> 查 找而没有排序,我相信没有set快。
>
> fff 670 wrote:
>
>> 如果要更进一步找出相等的数呢?
>>  
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> 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日 星期二 14:01

Qiangning Hong hongqn at gmail.com
Tue Jun 7 14:01:51 HKT 2005

fff 670 wrote:
> 如果要更进一步找出相等的数呢?

[x for x in set(a) if a.count(x) > 1]

-- 
Qiangning Hong

 _____________________________________________________________
/ It is impossible to travel faster than light, and certainly \
| not desirable, as one's hat keeps blowing off.              |
|                                                             |
\ -- Woody Allen                                              /
 -------------------------------------------------------------
  \   ^__^
   \  ($$)\_______        ________
      (__)\       )\/\    |Super |
          ||----W |       |Milker|
          ||    UDDDDDDDDD|______|

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

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

fff 670 fff670 at gmail.com
Tue Jun 7 14:18:41 HKT 2005

hehe,你行,上课先.

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

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

清风 paradise.qingfeng at gmail.com
Tue Jun 7 14:22:34 HKT 2005

直接a[0],a[1]就可以取出里面的元素了

2005/6/7, Neo Chan (netkiller) <neo.chen at achievo.com>:
>  
> 一段文本.将每个字母做为list一个元素??? 
> a='netkiller' 
> 我要得到是这样的 
> ('n','e','t','k'......'e','r') 
>   
> 
> Neo Chan
> Callsign: BG7NYT
> 73 
>  
>  
>  ________________________________
>  From: 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
> 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
> 
> 
> 
> 


-- 
Blog:http://www.donews.net/changzheng

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

2005年06月07日 星期二 14:23

fff 670 fff670 at gmail.com
Tue Jun 7 14:23:21 HKT 2005

参考这段程序看看,limodou写的:

对标准输入文件做以下工作
1,统计字符总数(包括空白,TAB等)
2,统计行数
3,统计单词数(每个单词由SPACE,TAB或换行分开)

import sys

body=sys.stdin.read()
print "total charters is %d" % len(body)
lines=body.splitlines()
print "total lines is %d" % len(lines)
words=body.split()
print "total words is %d" % len(words)

可能有帮助.

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

2005年06月07日 星期二 14:25

spark spark8103 at citiz.net
Tue Jun 7 14:25:41 HKT 2005

>>> a='netkiller'
>>> list(a)
['n', 'e', 't', 'k', 'i', 'l', 'l', 'e', 'r']
不明白??直接不就有了阿
  ----- Original Message ----- 
  From: Neo Chan (netkiller) 
  To: 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 [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
  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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050607/604219a5/attachment.htm

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

2005年06月07日 星期二 14:37

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 14:37:21 HKT 2005

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

Neo Chan
Callsign: BG7NYT
73
-------------- 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/dc5c5ab5/NeoChan-0001.vcf

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

2005年06月07日 星期二 14:59

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 14:59:31 HKT 2005

 
谢谢,就是一层窗纸.哈哈...

Neo Chan
Callsign: BG7NYT
73
-----Original Message-----
From: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of Petr ?imon
Sent: Monday, June 06, 2005 2:17 PM
To: python-chinese at lists.python.cn
Subject: Re: [python-chinese] 急问一个问题.


> 一段文本.将每个字母做为list一个元素???
> a='netkiller'
> 我要得到是这样的
> ('n','e','t','k'......'e','r')
tuple(a)
-------------- 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/fb1115e0/NeoChan.vcf

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

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

Neo Chan (netkiller) neo.chen at achievo.com
Tue Jun 7 15:08:23 HKT 2005

 
#the Morse code
#author: neo.chan(at)achievo.com

import string
import time
import winsound
class Morse:
    mcode = ''
    def __init__(self):
        pass
#    def __init__(self, frequency, duration):
    def beep(self,sound):
        
        for code in sound:
            if code == 0:
                # di
                self.mcode = self.mcode + '.'
                winsound.Beep( 400, 100)
            elif code == 1:
                # da
                self.mcode = self.mcode + '_'
                winsound.Beep( 400, 300)
            else:
                break
            self.mcode = self.mcode + ' '
            winsound.Beep( 37, 150)
        #print repr(mcode)
    def reader(self,char):
        for code in char:
            #print repr(code)
            self.mcode = code + ' : '
            code = string.upper(code)
            if code == '1': self.beep((0,1,1,1,1))
            if code == '2': self.beep((0,0,1,1,1))
            if code == '3': self.beep((0,0,0,1,1))
            if code == '4': self.beep((0,0,0,0,1))
            if code == '5': self.beep((0,0,0,0,0))
            if code == '6': self.beep((1,0,0,0,0))
            if code == '7': self.beep((1,1,0,0,0))
            if code == '8': self.beep((1,1,1,0,0))
            if code == '9': self.beep((1,1,1,1,0))
            if code == '0': self.beep((0,0,0,0,0))

            if code == 'A': self.beep((0,1))
            if code == 'B': self.beep((1,0,0,0))
            if code == 'C': self.beep((1,0,1,0))
            if code == 'D': self.beep((1,0,0))
            if code == 'E': self.beep((0,))
            if code == 'F': self.beep((0,0,1,0))
            if code == 'G': self.beep((1,1,0))
            if code == 'H': self.beep((0,0,0,0))
            if code == 'I': self.beep((0,0))
            if code == 'J': self.beep((0,1,1,1))
            if code == 'K': self.beep((1,0,1))
            if code == 'L': self.beep((0,1,0,0))
            if code == 'M': self.beep((1,1))
            if code == 'N': self.beep((1,0))
            if code == 'O': self.beep((1,1,1))
            if code == 'P': self.beep((0,1,1,0))
            if code == 'Q': self.beep((1,1,0,1))
            if code == 'R': self.beep((0,1,0))
            if code == 'S': self.beep((0,0,0))
            if code == 'T': self.beep((1,))
            if code == 'U': self.beep((0,0,1))
            if code == 'V': self.beep((0,0,0,1))
            if code == 'W': self.beep((0,1,1))
            if code == 'X': self.beep((1,0,0,1))
            if code == 'Y': self.beep((1,0,1,1))
            if code == 'Z': self.beep((1,1,0,0))
            
            if code == '?': self.beep((0,0,1,1,0,0))
            if code == '/': self.beep((1,0,0,1,0))
            if code == '(': self.beep((1,0,1,1,0,1))
            if code == ')': self.beep((1,0,0,0,0))
            if code == '-': self.beep((1,0,0,0,0,1))
            if code == '.': self.beep((0,1,0,1,0,1))
            #.--.-.
            if code == '@': self.beep((0,1,1,0,1,0))
            print self.mcode
            time.sleep(0.5)
"""        
    def doc2list(self,doc):
        tmp = [];
        for n in range(len(doc)):
            tmp.append(doc[n])
            #print doc[n]
        return tmp
"""
#Main Program

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. 
"""

def main():
    cw = Morse()
    #cw.beep((0,1,0,0,1,1,1))
    #cw.reader(('a','b','c','d','e'))
    #print cw.doc2list('netkiller')
    cw.reader(tuple(doc))

if __name__ == '__main__':
    main()





Neo Chan
Callsign: BG7NYT
73
-------------- 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/b000850b/NeoChan.vcf

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

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

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

Skipped content of type multipart/alternative-------------- 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/1529bb24/NeoChan.vcf

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

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

Qiangning Hong hongqn at gmail.com
Tue Jun 7 15:31:59 HKT 2005

Neo Chan (netkiller) wrote:
> 一段文本.将每个字母做为list一个元素???
> a='netkiller'
> 我要得到是这样的
> ('n','e','t','k'......'e','r')
>  
> 
> Neo Chan
> Callsign: BG7NYT
> 73


真奇怪,我20分钟前就收到了好几封对你这个问题的回答,怎么现在才收到原贴呢?


-- 
Qiangning Hong

 ___________________________________________________________
( The little pieces of my life I give to you, with love, to )
( make a quilt to keep away the cold.                       )
 -----------------------------------------------------------
  o
   o
       ___
     {~._.~}
      ( Y )
     ()~*~()
     (_)-(_)

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号