零基础学Phyton

Python3基础入门 tuple&set(元组集合)方

2019-06-27  本文已影响11人  410ca74fb10e

写在前面

1. 元组

for method1 in dir(tuple):
    if "_" not in method1:
        print(method1)
count
index

1.1 count 计数


help(tuple.count)
Help on method_descriptor:

count(self, value, /)
    Return number of occurrences of value.
  1. 返回出现的值的次数
t1=(1,2,3,4,1,2,3,1,2,1)
print(t1.count(1))
print(t1.count(5))
4
0

1.2 index

help(t1.index)
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.tuple instance
    Return first index of value.
    
    Raises ValueError if the value is not present.
  1. 返回第一个匹配值的索引值
  2. 如果值不存在就抛出异常ValueError
  3. 默认的起始索引是0,结束索引是9223372036854775807(2的63次方-1)
t1=('a','b','c','d','a')
print(t1.index('a'))  #打印第一个a的索引
print(t1.index('b'))
print(t1.index('e')) #e不存在抛出异常
0
1



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-5-bf7ccf31889f> in <module>
      2 print(t1.index('a'))
      3 print(t1.index('b'))
----> 4 print(t1.index('e'))


ValueError: tuple.index(x): x not in tuple

2. 集合

for method1 in dir(set):
    if "_" not in method1:
        print(method1)
add
clear
copy
difference
discard
intersection
isdisjoint
issubset
issuperset
pop
remove
union
update

2.1. add

help(set.add)
Help on method_descriptor:

add(...)
    Add an element to a set.
    
    This has no effect if the element is already present.
  1. 增加一个元素到一个集合中
  2. 如果元素已经存在没有效果(集合不可重复)
set1={1,2,3}
set1.add('a')
set1.add(4)
set1.add(1)  #添加重复的元素无效
print(set1)
try:
    set1.add([5,6]) #不可哈希的类型:list,不可以作为集合的元素
except TypeError:
    print("unhashable type: 'list'")

set1.add((5,6))
print(set1)
try:
    set1.add({7,8})
except TypeError:
    print("unhashable type: 'set'")
    
try:
    set1.add({'age':18})
except TypeError:
    print("unhashable type: 'dict'")
{1, 2, 3, 4, 'a'}
unhashable type: 'list'
{1, 2, 3, 4, 'a', (5, 6)}
unhashable type: 'set'
unhashable type: 'dict'

总结:list、set、dict这些不可哈希(可变)的元素不可以作为set中的元素

2.2. clear

清空

help(set.clear)
Help on method_descriptor:

clear(...)
    Remove all elements from this set.
set1={1,2,3}
set1.clear()
print(set1)
set()

2.3. copy

复制

help(set.copy)
set1={1,2,3}
set2=set1.copy()
print(set2)
{1, 2, 3}

2.4. difference 差集

等价于-(减法)

help(set.difference)
Help on method_descriptor:

difference(...)
    Return the difference of two or more sets as a new set.
    
    (i.e. all elements that are in this set but not the others.)
  1. 返回集合的不同
  2. 返回是一个集合
set1={1,2,3}
set2={3,4,5}
set3={2,6,7}
print(set1.difference(set2))  #在set1中,但不在set2中的
print(set1-set2)               #减法,就是差集
print('*********')
print(set1.difference(set2,set3))  #在set1中,但不在set2和set3中
print(set1-set2-set3)  
print(type(set1-set2))   #返回值是set
{1, 2}
{1, 2}
*********
{1}
{1}
<class 'set'>
set1={1,2,3}
set2={3,4,5}
print(set1-set2)

{1, 2}

2.5. discard 删除元素

help(set.discard)
Help on method_descriptor:

discard(...)
    Remove an element from a set if it is a member.
    
    If the element is not a member, do nothing.
  1. 移除一个成员
  2. 如果不是成员,啥也不做
set1={1,2,3}
set1.discard(1)
print(set1)
set1.discard(4)
print(set1)
{2, 3}
{2, 3}

2.6. intersection 交集

等价于&

help(set.intersection)
Help on method_descriptor:

intersection(...)
    Return the intersection of two sets as a new set.
    
    (i.e. all elements that are in both sets.)
  1. 返回交集(多个集合里面的公共部分)
set1={1,2,3}
set2={2,3,4}
print(set1.intersection(set2))
print(set1&set2)   #还可以这么写
print(type(set1.intersection(set2)))
{2, 3}
{2, 3}
<class 'set'>

2.7. isdisjoint 是否有交集

help(set.isdisjoint)
Help on method_descriptor:

isdisjoint(...)
    Return True if two sets have a null intersection.
  1. 如果2个集合没有交集就返回True
set1={1,2,3}
set2={3,4,5}
set3={4,5,6}
print(set1.isdisjoint(set2))  #有交集返回False
print(set1.isdisjoint(set3))  #无交集返回True
False
True

2.8. issubset 是否子集

等价于小于(<)

help(set.issubset)
Help on method_descriptor:

issubset(...)
    Report whether another set contains this set.
set1={1,2}
set2={1,2,3}
set3={2,3,4}
print(set1.issubset(set2))
print(set1<set2)            #等价写法
print(set1.issubset(set3))
True
True
False

2.9. issuperset 是否超集

等价于(>)

help(set.issuperset)
Help on method_descriptor:

issuperset(...)
    Report whether this set contains another set.
set1={1,2,3}
set2={1,2}
set3={3,4}
print(set1.issuperset(set2))
print(set1>set2)
print(set1.issuperset(set3))
True
True
False

2.10. pop

help(set.pop)
Help on method_descriptor:

pop(...)
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.
1. 删除并返回任意一个元素(无序的)
2. 如果set是空的就返回KeyError
set1={1,2}
set1.pop()
print(set1)
set1.pop()
set1.pop()

{2}



---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-38-dcf4d9ec963e> in <module>
      3 print(set1)
      4 set1.pop()
----> 5 set1.pop()


KeyError: 'pop from an empty set'

2.11. remove

help(set.remove)
Help on method_descriptor:

remove(...)
    Remove an element from a set; it must be a member.
    
    If the element is not a member, raise a KeyError.
1. 删除集合中的一个元素,必须是其中的元素
2. 如果不是集合的成员,抛出KeyError
set1={1,2,3}
set1.remove(1)
print(set1)
set1.remove(1)
{2, 3}



---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-39-71361082f76e> in <module>
      2 set1.remove(1)
      3 print(set1)
----> 4 set1.remove(1)


KeyError: 1

2.12. union

等价于|

help(set.union)
Help on method_descriptor:

union(...)
    Return the union of sets as a new set.
    
    (i.e. all elements that are in either set.)
1. 返回集合的联合(一个新的集合)
set1={1,2}
set2={2,3}
set3={4,5}
print(set1.union(set2))
print(set1|set3)
{1, 2, 3}
{1, 2, 4, 5}

2.13. update

help(set.update)
Help on method_descriptor:

update(...)
    Update a set with the union of itself and others.
set1={1}
set1.update({2,3})
print("通过集合更新:    ",set1)
set1.update('hoo')
print("通过字符串更新:  ",set1)
set1.update([4,5])
print("通过列表更新:    ",set1)
set1.update((6,7))
print("通过元组更新:    ",set1)
set1.update({'age':18})
print("通过字典更新:    ",set1,"值被忽略了")
try:
    set1.update(1)
except TypeError:
    print("通过数字更新:       'int' object is not iterable"+"   数字是不可迭代的对象")

    
通过集合更新:     {1, 2, 3}
通过字符串更新:   {1, 2, 3, 'o', 'h'}
通过列表更新:     {1, 2, 3, 4, 5, 'o', 'h'}
通过元组更新:     {1, 2, 3, 4, 5, 6, 7, 'o', 'h'}
通过字典更新:     {1, 2, 3, 4, 5, 6, 7, 'age', 'o', 'h'} 值被忽略了
通过数字更新:       'int' object is not iterable   数字是不可迭代的对象

集合总结

十三个方法


上一篇下一篇

猜你喜欢

热点阅读