可以用for…in…这类语句迭代读取一条数据供我们使用的对象称为可迭代对象
isinstance(obj, Iterable)
# 可迭代对象案例
from collections import Iterable
a = int()
b = float()
c = bool()
d = str()
e = list()
f = tuple()
g = set()
h = dict()
print(isinstance(a, Iterable))
print(isinstance(b, Iterable))
print(isinstance(c, Iterable))
print(isinstance(d, Iterable))
print(isinstance(e, Iterable))
print(isinstance(f, Iterable))
print(isinstance(g, Iterable))
print(isinstance(h, Iterable))
"""
运行结果:
False
False
False
True
True
True
True
True
"""
一组有顺序的数据的组合
[]
”包含# list案例1
l1 = [1, 2, 3, 'Hello', 'World']
l2 = list()
l3 = []
print(type(l1))
print(l1)
print(type(l2))
print(l2)
print(type(l3))
print(l3)
"""
运行结果:
[1, 2, 3, 'Hello', 'World']
[]
[]
"""
# list案例2
s = 'Mad man'
l1 = list(s)
l2 = [s]
print(type(l1))
print(l1)
print(type(l2))
print(l2)
"""
运行结果:
['M', 'a', 'd', ' ', 'm', 'a', 'n']
['Mad man']
"""
list[index]
# 列表访问案例
l1 = [1, 2, 3, 4, 5]
print(l1[2])
print(l1[100])
"""
运行结果:
3
IndexError: list index out of range
"""
对列表进行任意一段的截取
list[index1: index2: step]
# 列表切片案例1
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l2 = l1[0: 9]
l3 = l1
print(id(l1))
print(l1)
print(id(l2))
print(l1)
print(id(l3))
print(l3)
"""
运行结果:
2290380530184
[1, 2, 3, 4, 5, 6, 7, 8, 9]
2290380530248
[1, 2, 3, 4, 5, 6, 7, 8, 9]
2290380530184
[1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
# 列表切片案例2
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[:4])
print(l[4:])
print(l[:])
"""
运行结果:
[1, 2, 3, 4]
[5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
# 列表切片案例3
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[::])
print(l[::2])
"""
运行结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
"""
# 列表切片案例4
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[:100])
"""
运行结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
# 列表切片案例5
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[-2:-5])
print(l[-2:-5:-1])
print(l[-5:-2])
"""
运行结果:
[]
[8, 7, 6]
[5, 6, 7]
"""
+
”*
”# 列表的加法乘法操作
l1 = [1, 2, 3]
l2 = ['one', 'two', 'three']
l3 = l1 + l2
print(l3)
print(l3 * 2)
"""
运行结果:
[1, 2, 3, 'one', 'two', 'three']
[1, 2, 3, 'one', 'two', 'three', 1, 2, 3, 'one', 'two', 'three']
"""
in
”和“not in
”# 列表的成员检测案例
l = ['I', 'am', 'madman', 1, 2, 3]
if 'madman' in l:
print('在!')
if 4 not in l:
print('不在!')
"""
运行结果:
在!
不在!
"""
# 列表嵌套案例
l = [[1, 2, 3], [4, 5, 6]]
# 多层循环访问
for i in l:
print(i)
for j in i:
print(j)
# 单层循环访问
for i, j, k in l:
print(i, j, k)
"""
运行结果:
[1, 2, 3]
1
2
3
[4, 5, 6]
4
5
6
1 2 3
4 5 6
"""
[i for i in list if xxx]
# 列表生成式案例
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l2 = [2, 3]
l3 = [i for i in l1]
l4 = [i for i in l1 if i % 2 == 0]
l5 = [i**2 for i in l1]
l6 = [m*n for m in l1 for n in l2]
l7 = [i for i in range(0, 11)]
print(l3)
print(l4)
print(l5)
print(l6)
print(l7)
"""
运行结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[2, 3, 4, 6, 6, 9, 8, 12, 10, 15, 12, 18, 14, 21, 16, 24, 18, 27]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
可以删除指定的内容
del list/list[index]
# 列表删除案例
l = [1, 2, 3, 4]
del l[1]
print(l)
del l[0: 2]
print(l)
del l
print(l)
"""
运行结果:
[1, 3, 4]
[4]
NameError: name 'l' is not defined
"""
len(list)
max(list)
min(list)
list(object)
list.append(value)
insert(index, value)
,如果没有那个索引会报错list.pop(index)
list.remove(index)
list.clear()
list.reverse()
listA.extend(listB)
list.count(value)
list.copy()
list.index(value)
list.sort()
可以理解成一个不允许更改的列表
()
”包含# tuple案例
t1 = ()
t2 = tuple()
t3 = (100)
t4 = (100,)
t5 = 100,
t6 = 100, 200
print(type(t1))
print(t1)
print(type(t2))
print(t2)
print(type(t3))
print(t3)
print(type(t4))
print(t4)
print(type(t5))
print(t5)
print(type(t6))
print(t6)
"""
运行结果:
()
()
100
(100,)
(100,)
(100, 200)
"""
元组的常见操作和列表类似
# 元组的访问切片案例
t = ('I', 'am', 'Madman')
# 访问操作
print(t)
print(t[2])
print(t[100])
# 切片操作
print(t[:])
print(t[-1::-1])
print(t[::-1])
"""
运行结果:
('I', 'am', 'Madman')
Madman
IndexError: tuple index out of range
('I', 'am', 'Madman')
('Madman', 'am', 'I')
('Madman', 'am', 'I')
"""
# 元组的加法乘法案例
t1 = 1, 2, 3
t2 = 'I', 'am', 'madman'
t3 = t1 + t2
print(type(t3))
print(t3)
t4 = t3 * 2
print(type(t4))
print(t4)
"""
运行结果:
(1, 2, 3, 'I', 'am', 'madman')
(1, 2, 3, 'I', 'am', 'madman', 1, 2, 3, 'I', 'am', 'madman')
"""
# 元组的in和not in案例
t = (1, 2, 3)
if 1 in t:
print('在!')
if 4 not in t:
print('不在!')
"""
运行结果:
在!
不在!
"""
# 元组的循环访问案例
t = ([1, 2, 3], [4, 5], (7, 8, 9))
# 多层循环访问
for i in t:
print(i)
for j in i:
print(j)
print('*' * 20)
# 单层循环访问,i, j, k要和元组的元素个数对应,否则报错
for i, j, k in t:
print(i, j, k)
"""
运行结果:
[1, 2, 3]
1
2
3
[4, 5]
4
5
(7, 8, 9)
7
8
9
********************
1 2 3
ValueError: not enough values to unpack (expected 3, got 2)
"""
# 元组生成式案例
t1 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
t2 = (2, 3)
t3 = (i for i in t1)
t4 = (i for i in t1 if i % 2 == 0)
t5 = (i**2 for i in t1)
t6 = (m*n for m in t1 for n in t2)
t7 = (i for i in range(0, 11))
print(t3)
print(t4)
print(t5)
print(t6)
print(t7)
"""
运行结果:
at 0x0000029E8CC8C390>
at 0x0000029E8CC8C5E8>
at 0x0000029E8CC8C660>
at 0x0000029E8CC8C6D8>
at 0x000002A6B320C7C8>
"""
# 元组的删除案例
t = (1, 2, 3, 4, 5)
# 因为元组的内容不可以改变,所以删除内容会报错
del t[1]
print(t)
del t[0: 2]
print(t)
del t
print(t)
"""
TypeError: 'tuple' object does not support item deletion
TypeError: 'tuple' object does not support item deletion
NameError: name 't' is not defined
"""
没有顺序且没有重复元素的一组数据
{}
”包含# set案例
s1 = {}
s2 = set()
s3 = {1, 2, 3, 'I', 'am', 'madman', (1, 2, 3)}
s4 = {1, 2, 3, [1, 2, 3]}
s5 = {1, 2, 3, ([1, 2, 3], [4, 5, 6])}
s6 = {1, 2, 3, {1, 2, 3}}
s7 = {1, 2, 3, ({1, 2, 3}, {4, 5, 6})}
print(type(s1))
print(s1)
print(type(s2))
print(s2)
print(type(s3))
print(s3)
print(type(s4))
print(s4)
print(type(s5))
print(s5)
print(type(s6))
print(s6)
print(type(s7))
print(s7)
"""
运行结果:
{}
set()
{1, 2, 3, 'I', (1, 2, 3), 'madman', 'am'}
TypeError: unhashable type: 'list'
TypeError: unhashable type: 'list'
TypeError: unhashable type: 'set'
TypeError: unhashable type: 'set'
"""
集合的常见操作类似列表
# 集合的访问切片案例
s = {1, 2, 3, 4, 5, 6}
print(s[1])
print(s[0: 4])
"""
运行结果:
TypeError: 'set' object does not support indexing
TypeError: 'set' object is not subscriptable
"""
# 集合的加法乘法案例
s1 = {1, 2, 3}
s2 = {'I', 'am', 'madman'}
s3 = s1 + s2
print(type(s3))
print(s3)
s4 = s1 * 2
print(type(s4))
print(s4)
"""
运行结果:
TypeError: unsupported operand type(s) for +: 'set' and 'set'
TypeError: unsupported operand type(s) for *: 'set' and 'int'
"""
# 集合的in和not in操作
s = {1, 2, 3, 4, 'Madman', (1, 2, 3)}
if (1, 2, 3) in s:
print('在!')
if 'madman' not in s:
print('不在!')
"""
运行结果:
在!
不在!
"""
# 集合的循环访问案例
s1 = {1, 2, 3, 4, 5, 6, (1, 2, 3), (4, 5, 6)}
s2 = {(1, 2, 3), (4, 5, 6), (1, 2, 3)}
s3 = {(1, 2, 3), (4, 5, 6), (1, 2)}
for i in s1:
print(i)
for i, j, k in s2:
print(i, j, k)
for i, j, k in s3:
print(i, j, k)
"""
运行结果:
1
2
3
4
5
6
(1, 2, 3)
(4, 5, 6)
4 5 6
1 2 3
ValueError: not enough values to unpack (expected 3, got 2)
"""
# 集合的生成式案例
s1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
s2 = {2, 3}
s3 = {i for i in s1}
s4 = {i for i in s1 if i % 2 == 0}
s5 = {i**2 for i in s1}
s6 = {m*n for m in s1 for n in s2}
s7 = {i for i in range(0, 11)}
print(s3)
print(s4)
print(s5)
print(s6)
print(s7)
"""
运行结果:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{8, 2, 4, 6}
{64, 1, 4, 36, 9, 16, 49, 81, 25}
{2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 21, 24, 27}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
"""
# 集合的删除案例
s = {1, 2, 3, 4}
del s[2]
print(s)
del s
print(s)
"""
运行结果:
TypeError: 'set' object doesn't support item deletion
NameError: name 's' is not defined
"""
-
”号表示# 集合的数学操作案例1
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1.intersection(s2))
print(s1.difference(s2))
print(s1 - s2)
print(s1.union(s2))
print(s1 + s2)
"""
运行结果:
{3, 4}
{1, 2}
{1, 2}
{1, 2, 3, 4, 5, 6}
TypeError: unsupported operand type(s) for +: 'set' and 'set'
"""
set1.issubset(set2)
set1.issuperset(set2)
# 集合的数学操作案例2
s1 = {1, 2, 3}
s2 = {1, 2, 3, 4}
print(s1.issubset(s2))
print(s2.issuperset(s1))
"""
运行结果:
True
True
"""
不可以修改的集合
# list、tuple、set转换问题案例
a = 100
b = [100]
c = (5, 23, 4, 6, 9, 0, 23)
t1 = tuple(a)
print(type(t1))
print(t1)
t2 = tuple(b)
print(type(t2))
print(t2)
s1 = set(a)
print(type(s1))
print(s1)
s2 = set(b)
print(type(s2))
print(s2)
s3 = set(c)
print(type(s3))
print(s3)
"""
TypeError: 'int' object is not iterable
(100,)
TypeError: 'int' object is not iterable
{100}
{0, 4, 5, 6, 9, 23}
"""
一种键值对形式的组合数据
# dict案例
d1 = {}
d2 = {1: '1', '2': 2, (1, 2, 3): [1, 2, 3], 1.1: {1: {1, 2, 3}}}
print(type(d1))
print(d1)
print(type(d2))
print(d2)
"""
运行结果:
{}
{1: '1', '2': 2, (1, 2, 3): [1, 2, 3], 1.1: {1: {1, 2, 3}}}
"""
# 访问修改删除数据操作案例
d = {'one': 1, 'two': 2, 3: 'three'}
print(d['one'])
# 修改数据
d['one'] = 'Hello'
print(d)
# 删除数据
del d['one']
print(d)
"""
运行结果:
1
{'one': 'Hello', 'two': 2, 3: 'three'}
{'two': 2, 3: 'three'}
"""
# 成员检测案例
# 成员检测时,检测的是key的内容
d = {'one': 1, 'two': 2, 'three': 3}
if 2 in d:
print('value')
if 'two' in d:
print('key')
if ('two', 2) in d:
print('key and value')
"""
运行结果:
key
"""
# dict的循环访问案例
d = {'one': 1, 'two': 2, 'three': 3}
for k in d:
print(k, '--', d[k])
print('*' * 20)
for k in d.keys():
print(k)
for v in d.values():
print(v)
for k,v in d.items():
print(k, '--', v)
"""
运行结果:
one -- 1
two -- 2
three -- 3
********************
one
two
three
1
2
3
one -- 1
two -- 2
three -- 3
"""
# 字典的生成式案例
d1 = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
d2 = {k: v for k, v in d1.items() if v % 2 == 0}
d3 = {k: 'None' for k in range(0, 5)}
print(d2)
print(d3)
"""
运行结果:
{'two': 2, 'four': 4}
{0: 'None', 1: 'None', 2: 'None', 3: 'None', 4: 'None'}
"""
# 字典变成其他类型案例
d = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
st = str(d)
l = list(d)
t = tuple(d)
se = set(d)
print(type(st))
print(st)
print(type(l))
print(l)
print(type(t))
print(t)
print(type(se))
print(se)
"""
运行结果:
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
['one', 'two', 'three', 'four']
('one', 'two', 'three', 'four')
{'two', 'four', 'one', 'three'}
"""
获取字典的键值对
dict.items()
# items()案例
d = {'one': 1, 'two': 2, 'three': 3}
i = d.items()
print(type(d))
print(d)
print(type(i))
print(i)
"""
运行结果:
{'one': 1, 'two': 2, 'three': 3}
dict_items([('one', 1), ('two', 2), ('three', 3)])
"""
获取字典的键
dict.keys()
# keys()案例
d = {'one': 1, 'two': 2, 'three': 3}
k = d.keys()
print(type(k))
print(k)
"""
运行结果:
dict_keys(['one', 'two', 'three'])
"""
获取字典的值
dict.values()
# values()案例
d = {'one': 1, 'two': 2, 'three': 3}
v = d.values()
print(type(v))
print(v)
"""
运行结果:
dict_values([1, 2, 3])
"""
根据指定的键返回相应的值,和普通方法相比,get()可以设置默认值
dict.get(key, default_value)
# get()案例
d = {'one': 1, 'two': 2, 'three': 3}
print(d.get("four"))
# get的默认值是None,可以设置
print(d.get("one", 100))
print(d.get("four", 100))
print(d["four"])
"""
运行结果:
None
1
100
KeyError: 'four'
"""
使用指定的序列作为键,使用一个值作为所有键的值
dict.fromkeys(sequence, value)
# fromkeys()案例
l = ['Guitar', 'Write', 'Code']
d = dict.fromkeys(l, 'Good')
print(type(d))
print(d)
"""
运行结果:
{'Guitar': 'Good', 'Write': 'Good', 'Code': 'Good'}
"""
获取长度
len(str/list/tuple/set/dict...)
# len()案例
st = 'I am madman'
l = [1, 2, 3, 4, 5, (1, 2, 3, 4, 5)]
t = (1, 2, 3, 4, 5, {1, 2, 3, 4, 5})
se = {1, 2, 3, 4, 5, (1, 2, 3, 4, 5)}
d = {'name': 'Madman', 'age': 23, 'local': 'Shenzhen'}
print(type(len(st)))
print(len(st))
print(type(len(l)))
print(len(l))
print(type(len(t)))
print(len(t))
print(type(len(se)))
print(len(se))
print(type(len(d)))
print(len(d))
"""
运行结果:
11
6
6
6
3
"""
求最小值/最大值
min/max(str/list/tuple/set/dict...)
# min()/max()案例
l1 = [77, 44, 99, 1, 34]
l2 = ['a', 'ab', 'A', 'AB']
l3 = [1, 2, 'Madman']
l4 = [1, 2, (1, 2, 3)]
st = 'I am madman'
t = ('I', 'am', 'madman', 'me')
se = {1, 2, 3, 4, 5, 77, 88}
d = {'name': 'Madman', 'age': 23, 'local': 'Shenzhen'}
print(type(max(l1)))
print(max(l1))
print(type(max(l2)))
print(max(l2))
print(type(max(l3)))
print(max(l3))
print(type(max(l4)))
print(max(l4))
print(type(max(st)))
print(max(st))
print(type(max(t)))
print(max(t))
print(type(max(se)))
print(max(se))
print(type(max(d)))
print(max(d))
"""
运行结果:
99
ab
TypeError: '>' not supported between instances of 'str' and 'int'
TypeError: '>' not supported between instances of 'tuple' and 'int'
n
me
88
name
"""
对某一元素进行计数
str/list/tuple.count(value)
# count()案例
st = 'I am madman'
l = [1, 2, 3, 4, 5, (1, 2, 3, 4, 5), 1, 2, 2]
t = (1, 2, 3, 4, 5, {1, 2, 3, 4, 5}, 1, 2, 2)
se = {1, 2, 3, 4, 5, (1, 2, 3, 4, 5), 1, 2, 2}
d = {'name': 'Madman', 'age': 23, 'local': 'Shenzhen'}
print(type(st.count('m')))
print(st.count('m'))
print(type(l.count(2)))
print(l.count(2))
print(type(t.count(2)))
print(t.count(2))
print(type(se.count(2)))
print(se.count(2))
print(type(d.count('name')))
print(d.count('name'))
"""
返回结果:
3
3
3
AttributeError: 'set' object has no attribute 'count'
AttributeError: 'dict' object has no attribute 'count'
"""
某一元素从左往右第一次出现的位置
obj.index(value)
# index()案例
st = 'I am madman'
l = [1, 2, 3, 4, 5, (1, 2, 3, 4, 5), 1, 2, 2]
t = (1, 2, 3, 4, 5, {1, 2, 3, 4, 5}, 1, 2, 2)
se = {1, 2, 3, 4, 5, (1, 2, 3, 4, 5), 1, 2, 2}
d = {'name': 'Madman', 'age': 23, 'local': 'Shenzhen'}
print(type(st.index('ma')))
print(st.index('ma'))
print(type(l.index(2)))
print(l.index(2))
print(type(t.index({1, 2, 3, 4, 5})))
print(t.index({1, 2, 3, 4, 5}))
print(type(se.index((1, 2, 3, 4, 5))))
print(se.index((1, 2, 3, 4, 5)))
print(type(d.index('age')))
print(d.index('age'))
"""
运行结果:
5
1
5
AttributeError: 'set' object has no attribute 'index'
AttributeError: 'dict' object has no attribute 'index'
"""
添加指定元素
set.add(element)
# add()案例
st = 'I am madman'
l = [1, 2, 3, 4, 5]
t = (1, 2, 3, 4, 5)
se = {1, 2, 3, 4, 5}
print(st.add('!'))
print(st)
print(l.add(6))
print(l)
print(t.add(6))
print(t)
print(se.add(6))
print(se)
print(se.add((7, 8, 9)))
print(se)
"""
运行结果:
AttributeError: 'str' object has no attribute 'add'
AttributeError: 'list' object has no attribute 'add'
AttributeError: 'tuple' object has no attribute 'add'
None
{1, 2, 3, 4, 5, 6}
None
{1, 2, 3, 4, 5, 6, (7, 8, 9)}
"""
删除指定元素(会报错)
list/set.remove(element)
# remove()案例
st = 'I am madman'
l = [1, 2, 3, 1]
t = (1, 2, 3, 1)
se = {1, 2, 3, 4, (1, 2, 3)}
print(st.remove('I'))
print(st)
print('*' * 20)
print(l.remove(1))
print(l)
print(l.remove(1))
print(l)
print(l.remove(1))
print(l)
print('*' * 20)
print(t.remove(1))
print(t)
print(t.remove(1))
print(t)
print(t.remove(1))
print(t)
print('*' * 20)
print(se.remove(4)
print(se)
print(se.remove((1, 2, 3)))
print(se)
print(se.remove(4))
print(se)
"""
运行结果:
AttributeError: 'str' object has no attribute 'remove'
********************
None
[2, 3, 1]
None
[2, 3]
ValueError: list.remove(x): x not in list
********************
AttributeError: 'tuple' object has no attribute 'remove'
********************
None
{1, 2, 3, (1, 2, 3)}
None
{1, 2, 3}
"""
删除指定元素(不报错)
set.discard(elemnet)
# discard()案例
st = 'I am madman'
l = [1, 2, 3, 1]
t = (1, 2, 3, 1)
se = {1, 2, 3, 4, (1, 2, 3)}
print(st.remove('I'))
print(st)
print('*' * 20)
print(l.discard(1))
print(l)
print('*' * 20)
print(t.discard(1))
print(t)
print('*' * 20)
print(se.discard(4))
print(se)
print(se.discard((1, 2, 3)))
print(se)
print(se.discard(4))
print(se)
"""
运行结果:
AttributeError: 'str' object has no attribute 'remove'
********************
AttributeError: 'list' object has no attribute 'discard'
********************
AttributeError: 'tuple' object has no attribute 'discard'
********************
None
{1, 2, 3, (1, 2, 3)}
None
{1, 2, 3}
None
{1, 2, 3}
"""
弹出一个元素
list.pop(index=-1)
set.pop()
dict.pop(key)
# pop()案例
st = 'I am madman'
l = [1, 2, 3, 1, {1, 2, 3}]
t = (1, 2, 3, 1)
se = {1, 2, 3, 4, (1, 2, 3)}
d = {'name': 'madman', 'age': 23, 'local': 'Shenzhen'}
print(st.pop())
print(st)
print('*' * 20)
print(l.pop(2))
print(l)
print(l.pop())
print(l)
print('*' * 20)
print(t.pop())
print(t)
print('*' * 20)
print(se.pop())
print(se)
print(se.pop())
print(se)
print('*' * 20)
print(d.pop('name'))
print(d)
print(d.pop())
print(d)
"""
运行结果:
AttributeError: 'str' object has no attribute 'pop'
********************
3
[1, 2, 1, {1, 2, 3}]
{1, 2, 3}
[1, 2, 1]
********************
AttributeError: 'tuple' object has no attribute 'pop'
********************
1
{2, 3, 4, (1, 2, 3)}
2
{3, 4, (1, 2, 3)}
********************
madman
{'age': 23, 'local': 'Shenzhen'}
TypeError: pop expected at least 1 arguments, got 0
"""