美文网首页
(六)集合

(六)集合

作者: 费云帆 | 来源:发表于2018-12-27 09:16 被阅读0次

1."霸气"引言

读者可能有疑问了,出了这么多的类型,我也记不住呀,特别是里面还有不少方法。
不要担心记不住,你只要记住爱因斯坦说的就好了。
爱因斯坦在美国演讲,有人问:“你可记得声音的速度是多少?你如何记下许多东西?”
爱因斯坦轻松答道:“声音的速度是多少,我必须查辞典才能回答。因为我从来不记在辞
典上已经印着的东西,我的记忆力是用来记忆书本上没有的东西。”
多么霸气的回答。
这回答不仅仅霸气,更告诉我们一种方法:只要能够通过某种方法查找到的,就不需要记
忆。
所以,再多的数据类型及其各种方法,都不需要记忆。因为它们都可以通过下述方法但不限
于这些方法查到(这句话的逻辑还是比较严密的,包括但不限于...)
交互模式下用dir()或者help()
google(不推荐Xdu,原因自己体会啦)
还有,如果你经常练习,会发现很多东西自然而然就记住了。

2.集合的基础:

# 使用set()创建,重复的字符"i"
# 集合的元素是不可重复的
>>> set1=set('qiweisir')
# 字符串被拆分了
>>> set1
{'w', 'i', 'r', 'e', 'q', 's'}
>>> set2=set(['King','King','Jim Green','facebook'])
>>> set2
{'facebook', 'King', 'Jim Green'}
# 没有index属性,说明集合没有索引
>>> dir(set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
# 不能执行索引操作
>>> set2[1]
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    set2[1]
TypeError: 'set' object does not support indexing
# 利用大括号直接创建集合,现在不推荐这种方式了
>>> set3={'King','facebook'}
>>> set
<class 'set'>
  • 用大括号创建的集合,不能传入列表和字典(使用set可以):
>>> set1={'123',[1,2,3],'King'}
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    set1={'123',[1,2,3],'King'}
TypeError: unhashable type: 'list'
>>> set2={'123',{'name':'Jim'},'King'}
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    set2={'123',{'name':'Jim'},'King'}
TypeError: unhashable type: 'dict'
# set()
>>> set1=set(['a','b'])
>>> set1
{'a', 'b'}
>>> set2=set([1,2,3])
>>> set2
{1, 2, 3}
  • "哈希":就是不可变的,不能原地修改,比如字符串和元组
    "不可哈希":就是可变的,可以原地修改,比如列表和字典
    3.set.add()---添加元素,add()传入的参数必须是不可变的,比如字符串,数字;list和dict会哈希报错
# 这样是创建空字典...
>>> set1={}
>>> set1.add('King')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    set1.add('King')
AttributeError: 'dict' object has no attribute 'add'
# 使用set()才能创建空集合
>>> set1=set()
# add()添加的字符串,不再被拆分
>>> set1.add('King')
>>> set1
{'King'}
# 传入list,依然是错的
>>> set2.add([1,2,3])
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    set2.add([1,2,3])
TypeError: unhashable type: 'list'
# 小聪明,本质是字符串
>>> set2.add('[1,2,3]')
>>> set2
{'p', 't', 'h', 'o', 'y', '[1,2,3]', 'n'}
  • set.update()---更新集合的元素
>>> set1=set(['a','b'])
>>> set1
{'a', 'b'}
>>> set2=set([1,2,3])
>>> set2
{1, 2, 3}
>>> set2.update(set1)
>>> set2
{'a', 1, 2, 3, 'b'}
>>> set2.update([4,5,6])
>>> set2
{1, 2, 3, 4, 5, 6, 'b', 'a'}
>>> set2.update('Google')
# 多余的"o"被删除
>>> set2
{1, 2, 3, 4, 5, 6, 'l', 'b', 'a', 'G', 'o', 'g', 'e'}
  • set.pop()---删除随机的元素并返回该值
>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> help(set.pop)
Help on method_descriptor:

pop(...)
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.

>>> set1.pop()
'[1,2,3]'
>>> set1
{'n', 'u', 't', 'o', 'qiwsir', 'h', 'p'}
>>> set1.pop()
'n'
>>> set1
{'u', 't', 'o', 'qiwsir', 'h', 'p'}
>>> 
  • 删除指定的元素---set.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.

>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> set1.remove('qiwsir')
>>> set1
{'o', 'h', '[1,2,3]', 'u', 'p', 'n', 't'}
>>> set1.remove('King')
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    set1.remove('King')
KeyError: 'King'
  • 类似的用法set.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.

>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> set1.discard('qiwsir')
>>> set1
{'u', '[1,2,3]', 'h', 't', 'p', 'n', 'o'}
# 不会报错
>>> set1.discard('King')
>>> set1
{'u', '[1,2,3]', 'h', 't', 'p', 'n', 'o'}

清空所有---set.clear()

>>> help(set.clear)
Help on method_descriptor:

clear(...)
    Remove all elements from this set.

>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> set1.clear()
>>> set1
set()
>>> print(set1)
set()

4.使用set()创建的集合,都是可以原地修改的,或者说"非哈希"
想创建不可修改的集合---frozenset()

>>> set1=frozenset([1,2,3])
>>> set1
frozenset({1, 2, 3})
>>> set1.add('King')
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    set1.add('King')
AttributeError: 'frozenset' object has no attribute 'add'

相关文章

  • (六)集合

    1."霸气"引言 2.集合的基础: 用大括号创建的集合,不能传入列表和字典(使用set可以): "哈希":就是不可...

  • 第六篇学习笔记

    第六篇学习总结 一、Map集合 Map集合和Collection集合不一样,Map集合是双列集合,以键值对的形式存...

  • php redis 扩展类常用操作(5) - set

    六、redis 集合(Set)操作 Redis Set 是 String 类型的无序集合,集合成员是唯一的。 它是...

  • 【数据类型】17、容器类型:集合

    一、标签的容器二、创建一个集合三、更新一个集合四、访问集合中的元素五、集合的运算六、什么时候用集合 一、标签的容器...

  • Android 面试集合(六)

    1、锁的机制[https://juejin.cn/post/6942031707612643359] 2、Acti...

  • Java面试集合(六)

    1. abstract抽象 什么是abstract,中文为抽象,从具体事物抽出,概括它们共同的方面,本质属性与关系...

  • 六、orderset有序集合

    oreder set(有序集合) 集合本来是没有顺序的,现在要顺序的话,就得为每个值加一个权重。在这篇文章中,我们...

  • Swift第二周学习总结

    第六天 集合 1.集合的定义 2.集合的相关应用 字典 1.字典的定义 2.字典的相关应用 函数 1.定义函数 2...

  • 2018.06.29六组文章集合

    又到了每月的最后一周,谢谢六组的战友们这个月的合作与支持,这个月战友们都完成得挺好的!接下来就还有最后一项工作要进...

  • Jmeter(六)集合点

    JMeter也有像LR中的集合点,本篇就来介绍下JMeter的集合点如何去实现。 JMeter里面的集合点通过添加...

网友评论

      本文标题:(六)集合

      本文链接:https://www.haomeiwen.com/subject/bwwllqtx.html