Python tuple [list] dictionary

List
List [] various data framed in square brackets, each data called "elements."
We are separated by commas between each element of
various types of data (integer / floating /
string) ------------------------ ----
extract a single element from the list
each element has its own location number (i.e., offset)

1. The offset is zero from the
element name added to the list of 2. with offset bracket can take the position corresponding to the
students = [ 'Bob', 'red', 'Xiaogang']
Print ( students [0])
Xiaoming
-----------------------------
extracting a plurality of elements from a list of
colon operation to intercept the call list elements sectioning
will list out a fragment processing

List sliced formulas
about empty, take the head; left to take, do not take the right
# left of the colon empty, it is necessary to offset the element from 0 beginning to take; the right space, we must take the last element to the list of
# numbers left of the colon bring the corresponding elements, does not move to the right of

List2 = >>> [5,6,7,8,9]
>>> Print (List2 [:])
[. 5,. 6,. 7,. 8,. 9]
>>> Print (List2 [2:])
[. 7 ,. 8,. 9]
>>> Print (List2 [: 2])
[. 5,. 6]
>>> Print (List2 [. 1:. 3])
[. 6,. 7]
>>> Print (List2 [2:. 4])
[7,
8] ------------------------
slice comparison and offset
the offset is to take the list of elements, and the slice It is a partial list of intercepted

>>> students = [ 'Bob', 'red', 'Xiaogang']
>>> Print (Students. [2])
Xiaogang
>>> Print (Students. [2:])
[ 'Xiaogang']
- ---------------------------
to the list of add / remove elements
append () function
append () to add elements list, you can only increase an element
error message

>>> list3.append(4,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)


>>> list3 = [1,2]
>>> list3.append(3)
>>> print(list3)
[1, 2, 3]

>>> list3.append([4,5]) #列表内部支持嵌套,单个列表也会视为一个元素
>>> print(list3)
[1, 2, 3, [4, 5]]
————————————————————————————
合并列表
把A和B的成绩合并成一个列表,并按照从低到高的顺序排序
list1 = [91, 95, 97, 99] #A
list2 = [92, 93, 96, 98] #B
# 把 A 组成绩赋值给一个新列表,用来存合并的成绩——这个细节要注意!
list3 =list1
list3.extend(list2) #list3 = list1 + list2也可以
print(list3)

[91, 95, 97, 99, 92, 93, 96, 98]

list3.sort()
print(list3)

[91, 92, 93, 95, 96, 97, 98, 99]

列表排序

————————————————————————————
del语句
students = ['小明','小红','小刚','小美']
del students[3] #语法是:del 列表名[元素的索引]
print(students)
['小明', '小红', '小刚']

既能删除一个元素,也可以删除多个
(原理和切片类似,左取右不取)

 

 


###############################################
数据类型:字典
1.有名称;
2.要用=赋值;
3.用逗号作为元素间的分隔符
字典的外层是大括号{ }
字典的元素是由一个个键值对构成的,用英文冒号连接
scores = {'小明':95,'小红':90,'小刚':90}
字典中的键具备唯一性,而值可重复

扩展知识:len()函数
得出一个列表或者字典的长度(元素个数)

>>> students = ['小明','小红','小刚']
>>> scores = {'小明':95,'小红':90,'小刚':90}
>>> print(len(students))
3
>>> print(len(scores))
3

字典提取元素
字典靠的是键
scores = {'小明': 95, '小红': 90, '小刚': 90}
print(scores['小明']) #print字典名[字典的键]
95

给字典增加/删除元素

删除
del 字典名[键]

增加
字典名[键] = 值

scores = {'小明':95,'小红':90,'小刚':90}
del scores['小刚']
scores['小刚'] = 92
scores['小美'] = 85
print(scores)
{'小明': 95, '小红': 90, '小刚': 92, '小美': 85}
——————————————————————————
扩展
Python字典 items()函数以列表返回可遍历的(键, 值) 元组数组。
语法:
dict.items()
列子:
scores = {'语文':89, '数学':95, '英语':80}
print(scores.items())
dict_items([('语文', 89), ('数学', 95), ('英语', 80)]) #列表包含元组数组

遍历字典项
for a in scores.items():
  print(a)
('语文', 89) #元组数组
('数学', 95)
('英语', 80)

遍历字典值
for a,b in scores.items(): #写成 for (a,b) in scores.items(): 或for a, b in scores.items():
  print(a,b)
语文 89
数学 95
英语 80

 

#####################################################
列表和字典的异同

不同点

元素是有自己明确的“位置”的,
所以即使看似相同的元素,
只要在列表所处的位置不同,
它们就是两个不同的列表
>>> students1 = ['小明','小红','小刚']
>>> students2 = ['小刚','小明','小红']
>>> print(students1 == students2)
False

字典相比起来就显得随和很多,
调动顺序也不影响。
因为列表中的数据是有序排列的,
而字典中的数据是随机排列的
>>> scores1 = {'小明':95,'小红':90,'小刚':100}
>>> scores2 = {'小刚':100,'小明':95,'小红':90}
>>> print(scores1 == scores2)
True
总结:列表有序,要用偏移量定位;字典无序,便通过唯一的键来取值
————————————————————————————————————
相同点
1.修改元素,可以用赋值
list1 = ['小明','小红','小刚','小美']
list1[1] = '小蓝'
print(list1)
['小明', '小蓝', '小刚', '小美']

dict1 = {'小明':'男'}
dict1['小明'] = '女'
print(dict1)
{'小明': '女'}

#del语句通常是用来删除确定不需要的键值对
2.支持任意嵌套

列表嵌套
students = [['小明','小红','小刚','小美'],['小强','小兰','小伟','小芳']]
print(students[1][3]) #先判断是列表第几个元素,再判断列表中要取出元素的偏移量
小芳

字典嵌套
scores = {
'第一组':{'小明':95,'小红':90,'小刚':100,'小美':85},
'第二组':{'小强':99,'小兰':89,'小伟':93,'小芳':88}
}
print(scores['第二组']['小芳']) #先找到字典的键,再找出要取出的值对应的键
88

总结:最外层是什么就是什么嵌套,提取多级嵌套的列表/字典时,要一层一层地取出来
任意嵌套的列子
students = {
'第一组':['小明','小红','小刚','小美'],
'第二组':['小强','小兰','小伟','小芳']
}
scores = [
{'小明':95,'小红':90,'小刚':100,'小美':85},
{'小强':99,'小兰':89,'小伟':93,'小芳':88}
]
print(students['第一组'][2])
小刚
print(scores[0]['小刚'])
100
##########################
元组tuple
小括号来包的
元组和列表都是序列,提取的方式也是偏移量
支持任意的嵌套
tuple1 = ('A','B')
list2 = (('A','B'),('C','D'),('E','F'))

print(tuple1[0])
A
print(list2[1][1])
D

*取出 反面角色 狼 *

townee = [
{'海底王国':['小美人鱼''海之王''小美人鱼的祖母''五位姐姐'],'上层世界':['王子','邻国公主']},
'丑小鸭','坚定的锡兵','睡美人','青蛙王子',
[{'主角':'小红帽','配角1':'外婆','配角2':'猎人'},{'反面角色':'狼'}]
]

print(townee[5][1]['反面角色'])

 

Guess you like

Origin www.cnblogs.com/CH-TNT/p/11223201.html