Python基础语法(四)

今天我们继续学习Python语法的一些基础知识。

1.列表

我们先来创建几个列表看看

list = [[],[],[]]
list = [('a','A'),('b','B'),('c','C')]
for x in list:
    print(x)
for x , y in list:
    print(x , y)               

下面是执行结果

('a', 'A')
('b', 'B')
('c', 'C')
a A
b B
c C

*枚举   enumerate  可以让被遍历元素  添加一个编号 (索引值)

for后面的第一个参数  即为索引值  第二个参数为被遍历的元素

list = [[],[],[]]
list = [('a','A'),('b','B'),('c','C')]
for x , y  in enumerate(list):
    print(x , y)
for x ,( y ,z ) in enumerate(list):
    print(x , y ,z )
0 ('a', 'A')
1 ('b', 'B')
2 ('c', 'C')
0 a A
1 b B
2 c C


list = [('a','A'),('b','B'),('c','C')]
list = [(1 ,[2, 3,]),(4,[5,6]),(7,[8,9])]
for index , ( x , [ y , z]) in enumerate(list):
    print(index , x , y , z)
0 1 2 3
1 4 5 6
2 7 8 9

+可以使列表之间进行拼接,像字符串一样

list1 = ['a' , 'b' ,'c']
list2 = ['d','e','f']
list3 = list1 + list2
print(list3)
['a', 'b', 'c', 'd', 'e', 'f']
*extend  扩展 ,添加    将被合并的集合的所有值给主动进行合并的集合,最终结果是两个集合的元素个数的总和。 
list1 = ['a' , 'b' ,'c']
list2 = ['d','e','f']
list1.extend(list2)
print(list1)
['a', 'b', 'c', 'd', 'e', 'f']
用这个方法我们可以得到同样的结果

.append
list1 = [['a'],['b'],['c']]
list2 = [['c'],['d'],['e']]
# 将list2作为一个整体 给list1 list1的元素的个数等于之前的个数+ 1
list1.append(list2)
print(list1)
[['a'], ['b'], ['c'], [['c'], ['d'], ['e']]]
.startswith 找出列表中的特定元素
list = ['张三','张飞','张益达','关云长','赵子龙']
list5 = [x for x in list if x.startswith('张')]
print(list5)
['张三', '张飞', '张益达']
.revers 相反的
list = ['1','2','3']
list.reverse()
print(list)
['3', '2', '1']

*得到列表中所有元素的和
list = [1, 2 ,3 ,4 ,5,6,7,8,9,10]
some = 0
for x in list:
    some += x
print(some)


result = sum(list)
print(result)
55
55
.reverse 倒序 默认值为Flase
list = [ 1, 3, 5, 7 ,9 ,2 , 4 , 6 , 8, 10]
list2 = sorted(list , reverse = True)
print(list2)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

list = ['a' , 'b' ,'c']
print(list[0])
a

list = [['a','b'],['c','d'],['e','f']]
print(list[1][0])
c

list = [
    [
        ['a'],  #[0][0][0]
        ['b']     #010
    ],
    [
        ['c'],  #100
        ['d']   # 110
    ],
    [
        ['e'],  # 200
        ['f']   # 210
    ]
]
print(list[2][0][0])
e

list1 = [ [1 ,2 ,4], [3, 4], [5 ,6]]
for x in list1:
    for y  in x :
        print(y)
1
2
4
3
4
5
6

list = [
        [
            ['a'],  # [0][0][0]
            ['b']  # 010
        ],
        [
            ['c'],  # 100
            ['d']  # 110
        ],
        [
            ['e'],  # 200
            ['f']  # 210
        ]
    ]

for x in list :
    for y in x :
        for z in y :
            print(z)
for x , [[y],[z]]in enumerate(list):
    print(y ,z)
a
b
c
d
e
f
a b
c d
e f

2.元组

创建元组的两种方法
tp1 = ()
tp2 = tuple()
下面是列表创建
list1 = []
list2 = list()
如果元组在创建的时候没有放入元素,那这个元组就没有意义

元组和列表的区别:列表可以任意进行增删查改等操作,元组只能进行查

tp3 = ('a','b','c','d')
if 'a' in tp3:
    print('存在')
else :
    print('不存在')

存在

length  长度  在此表示元素的个数

tp3 = ('a','b','c','d')
print(len(tp3))

输出结果 :4   
+在元组中也可以使用

tp3 = ('a','b','c','d')
tp4 = ('q','w','e','r')
print(tp3 + tp4)
('a', 'b', 'c', 'd', 'q', 'w', 'e', 'r')


3.字典

声明字典的两种方式
dic1 = {}

dic2 = dict ()

举个例子

list = ['a','b','c']
 key   0   1   2
#value a   b   c

字典  key   value  键值对     每个值对应上面的列表

dic3 = {
    'name' : '小明' ,
    'age' :  17 ,
    # True代表男还是女 是自己规定的
    'sex' : True ,
    'height' : 1.76 ,
    'fond':['打游戏','学习','写代码','陪妹子逛街']
}
print(dic3)
{'name': '小明', 'age': 17, 'sex': True, 'height': 1.76, 'fond': ['打游戏', '学习', '写代码', '陪妹子逛街']}

dic3 = {
    'name' : '小明' ,
    'age' :  17 ,
    # True代表男还是女 是自己规定的
    'sex' : True ,
    'height' : 1.76 ,
    'fond':['打游戏','学习','写代码','陪妹子逛街']
}
print(dic3['fond'])
['打游戏', '学习', '写代码', '陪妹子逛街']

dic3['age'] = 18
print(dic3['age'])
dic3['girlFriend'] = '凤姐'
print(dic3)
dic3 = {
    'name' : '小明' ,
    'age' :  17 ,
    # True代表男还是女 是自己规定的
    'sex' : True ,
    'height' : 1.76 ,
    'fond':['打游戏','学习','写代码','陪妹子逛街']
}
dic3['age'] = 18
print(dic3['age'])
dic3['girlFriend'] = '凤姐'
print(dic3)
18
{'name': '小明', 'age': 18, 'sex': True, 'height': 1.76, 'fond': ['打游戏', '学习', '写代码', '陪妹子逛街'], 'girlFriend': '凤姐'}
字典里有的话修改  没有这个属性的话就增加

获取key值和value值

dic4 = {
    'name' : '小兰' ,
    'age' : 12 ,
    'fond' : '美食',
    'info':{
        'description':'很好的一个人',
        'phone':'123123123',
        'friend' : [
            {
                'friend_name':'小明' ,
                'frined_age' : 17
            },{
                'friend_name':'小王',
                 'friend_age':16
            },{
                'friend_name':'小张',
                'friend_age':14
            }
        ]
    }
}
for key in dic4:
    value = dic4[key]
    print(key , value)
name 小兰
age 12
fond 美食
info {'description': '很好的一个人', 'phone': '123123123', 'friend': [{'friend_name': '小明', 'frined_age': 17}, {'friend_name': '小王', 'friend_age': 16}, {'friend_name': '小张', 'friend_age': 14}]}
第二种获取key值和value值的方法

for key ,value in dic4.items():
    print(key ,value)
输出结果与第一种方法的结果一样



猜你喜欢

转载自blog.csdn.net/qq_42543250/article/details/80866420