机器学习笔记-基本python数据结构使用
Python for Machine Learning
阅读GitHub上的机器学习开源资料《A Complete Machine Learning Package》
所做的笔记。
数据结构(Lists, Dictionaries, Tuples, Sets)
List
使用方式:标识符 = [元素1, 元素2, 元素3, …]
相关函数:
list.sort()
排序
list.reverse()
倒置
list.append(element)
添加元素
list.remove(element)
移除元素
list.pop(index)
返回index指向的元素,参数为空时返回最后一个元素
list.count(element)
返回该元素在list里出现的次数
Dictionaries
Dictionary结构存储键-值
使用方式:
countries_code = {
"United States": 1,
"India": 91,
"Germany": 49,
"China": 86,
"Rwanda":250
}
相关函数:
dic.items()
返回dictionary中的键-值
dic.keys()
返回dictionary中的键元素
dic.values()
返回dictionary中的值元素
dic[key]
返回该键对应的值
dic[new_key] = new_val
加入一个新的键-值
Tuples
类似list,但声明后无法改变其中的元素,无法插入。
Sets
Sets里存储的元素是唯一的,不会重复。
# List Vs Set
odd_numbers = [1,1,3,7,9,3,5,7,9,9]
print("List:{}".format(odd_numbers))
print("#####")
set_odd_numbers = {
1,1,3,7,9,3,5,7,9,9}
print("Set:{}".format(set_odd_numbers))
输出结果:
List:[1, 1, 3, 7, 9, 3, 5, 7, 9, 9]
#####
Set:{1, 3, 5, 7, 9}
For Loop
遍历方式1:for item in item_set/list/dic/turple
even_nums