zoukankan      html  css  js  c++  java
  • python 集合的相关操作

    集合的相关操作:

    1 创建集合。set():可变的 不可变集合:frozenset()
    2 添加操作: add,update
    3 删除 remove
    4 成员关系 in,not in
    6 交集,并集,差集 & | -
    7 set去重 列表list内容元素重复


    #encoding=utf-8


    ##可变集合

    info = set('abc')
    info.add('python')##添加单个对象到集合里

    print info

    info.update('python')##把对象里的每个元素添加到集合里

    print info

    info.remove('python')

    print info


    ##不可变集合

    t = frozenset('haha')##不能进行添加,修改和删除的操作。

    ##成员操作 in,not in

    print 'a' in info

    print 'h' in t

    print 'jay' not in info

    ##判断2个集合是否相等,之和元素本身有关,和顺序无关。

    print set('abc') == set('cba')

    ##并集,交集,差集

    print set('abc') | set('cbdef')##并集

    print set('abc') & set('cbdef')##交集

    print set('abc') - set('cbdef')##差集

    去重的方法:
    liststr = ['haha','gag','hehe','haha']
    #for循环

    m = []

    for i in liststr:
    if i not in m:
    m.append(i)

    print m

    m = set(liststr)

    print list(m)

    转载地址:http://www.cnpythoner.com/post/245.html

  • 相关阅读:
    Hashmap实现原理
    策略模式
    Google Drive ubuntu
    numix Docky
    Google Drive 和 Dropbox 同步同一个文件夹目录
    sublime text 2
    matlab cell
    liteide
    taglist and nerdtree
    codeblocks
  • 原文地址:https://www.cnblogs.com/wanpython/p/2963819.html
Copyright © 2011-2022 走看看