python之列表,元组,字典。

在博主学习列表,元组以及字典的时候,经常搞混这三者。因为他们都是用括号表示的。分别是[],(),{}.

列表(list):

[1,'abc',1.26,[1,2,3],(1,2,3),{'age:18'}]

列表中的元素可以是整型,浮点型,字符串,也可以是元组,列表,字典。

列表中的元素是有序的,而且元素可以更改,包括增删改。列表类似于Java之中的数组。

列表的常用方法:append,extend,remove,insert,pop,sort,reverse。

 1 #!/usr/bin/python3
 2 # -*- coding: utf-8 -*-
 3 # @Time     :2018/11/23 13:54
 4 # @Author   :yosef
 5 # @Email    :[email protected]
 6 # @File:    :class4.py
 7 # @Software :PyCharm Community Edition
 8 
 9 list1 = [1, 1.36, 'a', 'python', (1,), [1, 2], {'age': 18}]
10 
11 # print(list1)
12 # for i in list1:
13 #     print(i)
14 # list1.append("5") # append方法只能在列表的末尾添加一个元素
15 
16 #  增 append extend insert
17 list1.append("10")  # append方法只能在列表的末尾添加一个元素
18 print(list1)
19 
20 list1.extend([1, 2]) # extend方法可以连接两个列表list
21 print(list1)
22 
23 list1.insert(1, 0.36) # insert相比append, insert可以插入具体位置,append只能在末尾。
24 list1.insert(13,"这是11") # 当索引位置大于list原本长度,相当于在末尾增加元素
25 # print(len(list1))
26 print(list1)
27 
28 # 删 1.python的del 2.list的remove 3. list的pop
29 del list1[0]    # del方法可以通过索引直接删除list之中的某个元素
30 print(list1)
31 
32 list1.extend([0.36, 0.36])
33 list1.remove(0.36) # remove方法是通过传入的值删除list中相匹配的第一个元素
34 print(list1)
35 
36 list1.pop(0)    # pop方法也是通过索引来删除list中元素,对比del方法,一个是Python自带,一个是list自带
37 print(list1)
38 
39 # 改 直接通过list索引来修改相应位置的值
40 list1[0] = 'b'
41 print(list1)
42 
43 # 查 类似于字符串的查
44 print(list1) # 打印list所有元素
45 print(list1[0:1]) # 打印list的第一个元素
46 print(list1[2:5]) # 打印list的第3-5个元素
47 print(list1[-1])  # 打印list的最后一个元素

这是结果:

2. 元组(tuple)

元组有序,且不可修改。

先看这张图:

我们从编译器中可以看到,元组只有2个方法,一个是计数另一个是看索引,并不支持增删改查。

 1 #!/usr/bin/python3
 2 # -*- coding: utf-8 -*-
 3 # @Time     :2018/11/23 16:43
 4 # @Author   :yosef
 5 # @Email    :[email protected]
 6 # @File:    :class5.py
 7 # @Software :PyCharm Community Edition
 8 
 9 tuple1 = (1, 1.36, 'a', 'python', (1,), [1, 2], {'age': 18})
10 print(tuple1.count(1))      # 参数计数
11 print(tuple1.index(1.36))   # 元素索引位置

元组内部元素不可修改,但是内部元素的列表,字典可以修改其内部元素。注意,当元组只有一个元素时,要在元素后加上",",否则会当成原本的变量类型处理。

1 tuple2=(1,)
2 tuple3=(1)
3 print(tuple2,tuple3)

结果:

tuple2是元组,tuple3是整型3.

3. 字典(dict)

首先对于字典,我们要知道它与列表元组不同的是,字典是无序的,可以增加修改删除。字典的对应关系是key: value.

 1 #!/usr/bin/python3
 2 # -*- coding: utf-8 -*-
 3 # @Time     :2018/11/23 17:11
 4 # @Author   :yosef
 5 # @Email    :[email protected]
 6 # @File:    :class6.py
 7 # @Software :PyCharm Community Edition
 8 
 9 dict1 = {"Name": "yosef",
10          "Sex": "man",
11          "Age": 22,
12          "City": "Shanghai"}
13 
14 # 增加
15 dict1["Hobby"] = "Coding"  # 不需要调用方法,直接用dict[new_key] = value  可以新增新的key:value
16 print(dict1)
17 
18 # 删除
19 dict1.pop("Hobby")  # 调用dict的pop方法,可以删除不需要的key:value。传入的参数是key
20 print(dict1)
21 
22 # 修改
23 dict1["Age"] = 23       # 这里语句与新增一样,如果原本有key,则覆盖原本的,即修改,反之新增一个key:value
24 print(dict1)
25 
26 # 查看
27 for value in dict1.values():        # 查看字典的所有value
28     print(value)
29 
30 for key in dict1.keys():            # 查看字典的所有key
31     print(key)
32 
33 print(dict1["Name"])                # 通过key查看value

结果:

猜你喜欢

转载自www.cnblogs.com/wlyhy/p/10008838.html