字符串、列表、元组、字典05

遍历

通过for ... in ... 我们可以遍历字符串、列表、元组、字典等

注意python语法的缩进

字符串遍历

>>> a_str = "hello 123"
>>> for char in a_str:
...     print(char,end=' ')
...
h e l l o   1 2 3

列表遍历

>>> a_list = [1, 2, 3, 4, 5]
>>> for num in a_list:
...     print(num,end=' ')
...
1 2 3 4 5

元组遍历

>>> a_turple = (1, 2, 3, 4, 5)
>>> for num in a_turple:
...     print(num,end=" ")
1 2 3 4 5

字典遍历

<1> 遍历字典的key(键)

<2> 遍历字典的value(值)

<3> 遍历字典的项(元素)

<4> 遍历字典的key-value(键值对)

想一想,如何实现带下标索引的遍历

>>> chars = ['a', 'b', 'c', 'd']
>>> i = 0
>>> for chr in chars:
...     print("%d %s"%(i, chr))
...     i += 1
...
0 a
1 b
2 c
3 d

enumerate()

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

>>> chars = ['a', 'b', 'c', 'd']
>>> for i, chr in enumerate(chars):
...     print i, chr
...
0 a
1 b
2 c
3 d

合介绍

集合是无序的,集合中的元素是唯一的,集合一般用于元组或者列表中的元素去重。

定义一个空集合

  set1 = set()
  # 注意以下写法为一个空字典
  set2 = {}

添加元素(add,update)

add

set1 = {1, 2, 4, 5}
#添加元素
set1.add(8)

update

set1 = {1, 2, 4, 5}
#是把要传入的元素拆分,做为个体传入到集合中
set1.update("abcd")

删除元素(remove,pop,discard)

remove

set1 = {1, 2, 4, 5}

# 使用remove删除集合中的元素 如果有 直接删除 如果没有 程序报错
set1.remove(22)

pop

set1 = {1, 2, 4, 5}

# 使用pop删除是随机删除集合中的元素 如果set1没有元素讲程序报错
set1.pop()

discard

set1 = {1, 2, 4, 5}

# 使用discard删除 如果元素存在 直接删除 如果元素不存在 不做任何操作
set1.discard(2)

交集和并集( & 和 | )

交集

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
new_set = set1 & set2
print(new_set) 
# {3, 4}

并集

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
new_set = set1 | set2
print(new_set)

# {1, 2, 3, 4, 5, 6}

公共方法

运算符

运算符 Python 表达式 结果 描述 支持的数据类型
+ [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串、列表、元组
* ['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] 复制 字符串、列表、元组
in 3 in (1, 2, 3) True 元素是否存在 字符串、列表、元组、字典
not in 4 not in (1, 2, 3) True 元素是否不存在 字符串、列表、元组、字典

+

>>> "hello " + "itcast"
'hello itcast'
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')

*

>>> 'ab' * 4
'ababab'
>>> [1, 2] * 4
[1, 2, 1, 2, 1, 2, 1, 2]
>>> ('a', 'b') * 4
('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')

in

>>> 'itc' in 'hello itcast'
True
>>> 3 in [1, 2]
False
>>> 4 in (1, 2, 3, 4)
True
>>> "name" in {"name":"Delron", "age":24}
True

注意,in在对字典操作时,判断的是字典的键

python内置函数

Python包含了以下内置函数

序号 方法 描述
1 len(item) 计算容器中元素个数
2 max(item) 返回容器中元素最大值
3 min(item) 返回容器中元素最小值
4 del(item) 删除变量

len

>>> len("hello itcast")
12
>>> len([1, 2, 3, 4])
4
>>> len((3,4))
2
>>> len({"a":1, "b":2})
2

注意:len在操作字典数据时,返回的是键值对个数。

max

>>> max("hello itcast")
't'
>>> max([1,4,522,3,4])
522
>>> max({"a":1, "b":2})
'b'
>>> max({"a":10, "b":2})
'b'
>>> max({"c":10, "b":2})
'c'

del

del有两种用法,一种是del加空格,另一种是del()

>>> a = 1
>>> a
1
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = ['a', 'b']
>>> del a[0]
>>> a
['b']
>>> del(a)
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

多维列表/元祖访问的示例

>>> tuple1 = [(2,3),(4,5)]
>>> tuple1[0]
(2, 3)
>>> tuple1[0][0]
2
>>> tuple1[0][2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range


>>> tuple1[0][1]
3
>>> tuple1[2][2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range


>>> tuple2 = tuple1+[(3)]
>>> tuple2
[(2, 3), (4, 5), 3]
>>> tuple2[2]
3
>>> tuple2[2][0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable

猜你喜欢

转载自blog.csdn.net/ZY3099492099/article/details/80887798