概述
- 组合数据类型属于引用型数据,可储存一组即多个数据。这一组数据可以类型不一致(不涉及分类等涉及类型的操作)。区别于基本数据类型,只能存储一个数据。
- 组合数据类型包含列表list、元组tuple、集合set、字典dirc。
- 特点:
list:可以存储多个数据、可重复、有顺序;基于它的这些特点,可以直接查询、添加、修改、删除(有序),
tuple:不能修改的list
set:无序,不能重复,存储多个数据;
dirc:无序、key值不重复,value值可重复;
- 声明赋值:
1.准备:(name)可能被认为四则运算
组合数据类型声明过程中无论是list()、tuple()、set()、dirc(),它们括号中都只接受一个参数
2.声明空的类型:
列表:l=list()或l=[]
元组:t=tuple()或t=()
集合:s=set()注意:此时,不能用s={},因为{}表示一个空字典
字典:d=dirc()或d={}
3.赋值一个元素
t=tuple(1,)注意:要添加逗号,区分四则运算
l=[1] s={1}
4.t=(1,2)/t=tuple([1,2]) l=[1,2]/l=list([1,2]) s={1,2} /s=set([1,2])
d={'key':"value1","key2":{},"key3":[]}/d=dirc(["key1","value"])
- 操作
1.添加
列表:list.append(para)末尾追加一个元素
list.insert(index,value)指定位置添加元素
list.extends(para) 追加一组数据
元组:因为它的不可修改性,不能添加,因此一旦声明了一个空元祖,它自始至终就只能是空元组
集合:set.add() 因为它的无序性,insert()不能使用
字典:dirc[key]=[value]
dirc.setdefalt(key,value)在key已存在下,不执行此行代码;key值不存在,输入value,添加的键值对中值为value,若,不写value,默认添加后为空
2.查询
所有的查询都可以有遍历方法,但字典的遍历稍有不同,定义的变量代指key值;
有序如list,tuple可以直接用下标查询 list[index1]|[index2]
无序如set用成员运算符查询 para in tuple(value1...)
如dirc用key查询
3.修改,元组不可修改();列表、字典、可以直接复制修改;集合需要先查询再添加,删除
4.删除:list.remove() 删除指定元素 del list[index]删除指定位置元素 list.pop()随机删除 list.clear()清空
元组不可修改,不能删除
set.remove() set.pop() set.clear()
dirc.pop(key) 删除指定键值对 dirc.popitems() 随机 dirc.clear()
# # 赋值 操作
# # l=list([1,3,8,2])
# # s={1,1,2}
# # t=(1,2,1)
# # d={'1':1,'1':1}
# # d1={'1':1,'1':2}
# # d2={'1':1,'2':1}
# # print("列表是重复的吗?",l)#列表是重复的吗? [1, 3, 2, 2]
# # print("集合是重复的吗?",s)#列表是重复的吗? {1, 2}
# # print("元组是重复的吗?",t)#元组是重复的吗? (1, 2, 1)
# # print("字典是重复的吗?",d)#字典是重复的吗? {'1': 1}
# # print("字典的键是重复的吗?",d1)#字典是重复的吗? {'1': 2}
# # print("字典的值是重复的吗?",d2)#字典是重复的吗? {'1': 1, '2': 1}
# # # 有序:
# # # print(s.index(1))#'set' object has no attribute 'index'
# # print(l.index(1))#0
# # print(t.index(1))#0
# # print(d.index(1))#'dict' object has no attribute 'index'
#
# # list
# # class list(object)
# # | list(iterable=(), /)
# # |
# # | Built-in mutable sequence.
# # |
# # | If no argument is given, the constructor creates a new empty list.如果没有给出参数,构造函数将创建一个新的空列表。
# # | The argument must be an iterable if specified.如果指定,参数必须是可迭代的。
#
# # | append(self, object, /)
# # | Append object to the end of the list.追加一个元素
# # |
# # | clear(self, /)
# # | Remove all items from list.
# # |
# # | copy(self, /)
# # | Return a shallow copy of the list.
# # |
# # | count(self, value, /)
# # | Return number of occurrences of value.
# # |
# # | extend(self, iterable, /)
# # | Extend list by appending elements from the iterable.从可迭代对象追加多个元素(把可迭代对象分开)到列表末尾
# # |
# # | index(self, value, start=0, stop=9223372036854775807, /)
# # | Return first index of value.
# # |
# # | Raises ValueError if the value is not present.
# # |
# # | insert(self, index, object, /)
# # | Insert object before index.添加元素到指定的位置
# l=[1,2,3]
# l.insert(1,4)
# print(l)
# # |
# # | pop(self, index=-1, /)
# # | Remove and return item at index (default last).
# # |
# # | Raises IndexError if list is empty or index is out of range.
# # |
# # | remove(self, value, /)
# # | Remove first occurrence of value.
# # |
# # | Raises ValueError if the value is not present.
# # |
# # | reverse(self, /)
# # | Reverse *IN PLACE*.
# # |
# # | sort(self, /, *, key=None, reverse=False)
# # | Stable sort *IN PLACE*.
# # |
# # | ----------------------------------------------------------------------
# # | Static methods defined here:
# # |
# # | __new__(*args, **kwargs) from builtins.type
# # | Create and return a new object. See help(type) for accurate signature.
# # |
# # | ----------------------------------------------------------------------
# # | Data and other attributes defined here:
# # |
# # | __hash__ = None
# #
# # set
# # class set(object)
# # | set() -> new empty set object
# # | set(iterable) -> new set object
# # |
# # | Build an unordered collection of unique elements.构建惟一元素的无序集合。
# # | add(...)
# # | Add an element to a set.
# # |
# # | This has no effect if the element is already present.
# # |
# # | clear(...)
# # | Remove all elements from this set.
# # |
# # | copy(...)
# # | Return a shallow copy of a set.
# # |
# # | 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.)
# # |
# s={1,2}
# s1={2}
# print(s.difference(s1))#{1}
# print(s)#{1, 2}
# print(s1)#{2}
# # | difference_update(...)
# # | Remove all elements of another set from this set.从这个集合中删除另一个集合的所有元素。
# # |
s={1,2}
s1={2}
# print(s.difference_update(s1))
print(s)
print(s1)
# # | discard(...)
# # | Remove an element from a set if it is a member.
# # |
# # | If the element is not a member, do nothing.
# # |
# s.discard(1)
print(s)#{2}
# # | intersection(...)
# # | Return the intersection of two sets as a new set.
# # |
# # | (i.e. all elements that are in both sets.)
# # |
# # | intersection_update(...)
# # | Update a set with the intersection of itself and another.
# # |
s.intersection_update(s1)
print(s)#{2}
# # | isdisjoint(...)
# # | Return True if two sets have a null intersection.如果两个集合的交集为空,则返回True
# # |
# # | issubset(...)
# # | Report whether another set contains this set.报告另一个集合是否包含此集合。
# # |
# # | issuperset(...)
# # | Report whether this set contains another set.
# # |
# # | pop(...)
# # | Remove and return an arbitrary set element.移除并返回任意的集合元素。
# # | Raises KeyError if the set is empty.
# # |
# # | remove(...)
# # | Remove an element from a set; it must be a member.
# # |
# # | If the element is not a member, raise a KeyError.
# # |
# #
# # | union(...)
# # | Return the union of sets as a new set.
# # |
# # | (i.e. all elements that are in either set.)
# # |
# # | update(...)
# # | Update a set with the union of itself and others.
# # |
# # | ----------------------------------------------------------------------
# # class tuple(object)
# # | tuple(iterable=(), /)
# # |
# # | Built-in immutable sequence.
# # |
# # | If no argument is given, the constructor returns an empty tuple.如果没有给出参数,构造函数返回一个空的元组。
# # | If iterable is specified the tuple is initialized from iterable's items.如果指定iterable,则从iterable的项初始化元组。
# # |
# # | If the argument is a tuple, the return value is the same object.如果参数是一个元组,返回值是相同的对象。
# # |
# # | Methods defined here:
# # # |
# # | index(self, value, start=0, stop=9223372036854775807, /)
# # | Return first index of value.
# # |
# # | Raises ValueError if the value is not present.
# # |
# # | ----------------------------------------------------------------------
#
# >>> help(dict)
# Help on class dict in module builtins:
#
# class dict(object)
# | dict() -> new empty dictionary
# | dict(mapping) -> new dictionary initialized from a mapping object's
# | (key, value) pairs
# | dict(iterable) -> new dictionary initialized as if via:
# | d = {}
# | for k, v in iterable:
# | d[k] = v
# | dict(**kwargs) -> new dictionary initialized with the name=value pairs
# | in the keyword argument list. For example: dict(one=1, two=2)
# |
# | clear(...)
# | D.clear() -> None. Remove all items from D.
# |
# | copy(...)
# | D.copy() -> a shallow copy of D
# |
# | get(self, key, default=None, /)
# | Return the value for key if key is in the dictionary, else default.
# |
# | items(...)
# | D.items() -> a set-like object providing a view on D's items
# |
# | keys(...)
# | D.keys() -> a set-like object providing a view on D's keys
# |
# | pop(...)
# | D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
# | If key is not found, d is returned if given, otherwise KeyError is raised
# |
# | popitem(...)
# | D.popitem() -> (k, v), remove and return some (key, value) pair as a
# | 2-tuple; but raise KeyError if D is empty.
# |
# | setdefault(self, key, default=None, /)
# | Insert key with a value of default if key is not in the dictionary.
# |
# | Return the value for key if key is in the dictionary, else default.
# |
# | update(...)
# | D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
# | If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
# | If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
# | In either case, this is followed by: for k in F: D[k] = F[k]
# |
# | values(...)
# | D.values() -> an object providing a view on D's values
# |
# | ----------------------------------------------------------------------
# 操作:
# 添加:add append insert extends = setdefault
# L 0 1 1 1 0 0
# S 1 0 0 0 0 0
# T 0 0 0 0 0 0
# D 0 0 0 0 1 1
# 删除:del remove pop popitem discord
# L 1 1 1 0 0
# S 1 1 1 0 1
# T 0 0 0 0 0
# D 1 0 1 1 0
# 查询:下标 key .get 成员运算符
# L 1 0 0 0
# S 0 0 0 1
# T 1 0 0 0
# D 0 1 1 0
# 修改:下标 key
# L 1 0
# S 0 0
# T 0 0
# D 0 1
最后
以上就是忐忑鞋子为你收集整理的组合数据类型的全部内容,希望文章能够帮你解决组合数据类型所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复