huoJupyter Notebook
1、LIST定义、函数、输出
#LIST
name = []
name.append("str")
name.append(int)
indes = len(name)
print(type(name))
temp = [1,2,3,4,5,6,7,8,9,10]
index_1 = len(temp)
index = len(temp)-1
print(index_1) #10
print(index) #9
print(temp[index]) #10
print(temp[len(temp)-1]) #10
print(temp[-1]) #10
2.1、切片
#temp = []
print(temp[3:6]) #[3,6)#[4, 5, 6]
print(temp[3:]) #[4, 5, 6, 7, 8, 9, 10]
print(len(temp[3:])) #7
a = range(10)
print(a) #[0,10)#range(0, 10)
print(len(a)) #10
list(range(10)) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(2,5)) #[2, 3, 4]
list(range(6,10,2)) #[6, 8]
list(range(1,-11,-1)) #[1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
a = [["a",1,2,3,4],["b",5,6,7,8]]
for i in a:
print(i)
for k in i:
print(k)
"""
['a', 1, 2, 3, 4]
a
1
2
3
4
['b', 5, 6, 7, 8]
b
5
6
7
8
"""
2.2、切片删除元素
###########
3、Dictionary
name = ["a","b","c","d"]
score = [11,22,33,44]
index = range(len(name))
find_name = "b"
for i in index:
#if name[i] == find_name:
if find_name == name[i]:
find_score = score[i]
print(find_score)
"""22"""
or?
score = {} #key value
print(type(score)) #<class 'dict'>
score["a"] = 11
score["b"] = 22
score["c"] = 33
score["d"] = 44
"""
score = {
"a" : 11,
"b" : 22,
"c" : 33,
"d" : 44
}
print(score)
print(score["a"])
"""
#输出
print(score) #{'a': 11, 'b': 22, 'c': 33, 'd': 44}
print(score["a"]) #11
print(score.keys()) #dict_keys(['a', 'b', 'c', 'd'])
print(score.values()) #dict_values([11, 22, 33, 44])
#改值
score["a"] = 99
print(score["a"]) #99
score["a"] = score["a"] + 1
print(score["a"]) #100
#判断元素是否在字典中
print("a" in score) #true
print("22" in score) #false
print(22 in score) #false
字典可用于统计
a = ["c","d","w","w","c","k"]
a_dic = {}
for i in a:
if i in a_dic:
a_dic[i] = a_dic[i] + 1
else:
a_dic[i] = 1
print(a_dic)
#{'c': 2, 'd': 1, 'w': 2, 'k': 1}
|