文章目录
- python deep learning基础
-
- 一,python 容器类型
-
- 1,列表
-
- 1.1,列表,就是python中的数组,长度可变,且能包含不同类型元素,
- 1.2,切片操作,可用切片方式获取列表中的元素
- 1.3 循环Loops:enumerate 函数访问每个循环元素指针
- 1.4 列表推导List comprehensions,转换数据类型时可以简化代码
- 1.5 List列表常用函数:index(x),extend(L),insert(i, x),count(x),remove(x),pop([i]),sort(cmp=None, key=None, reverse=False),sort(cmp=None, key=None, reverse=False)等
- 1.6 更多函数如popleft,del a【0】,map,用作堆栈,函数工具栏等更多信息详见以下网址
- 2,字典
- 3,集合
-
- 3.1,合集是不同个体的无序集合
- 3.2,循环Loops:在集合中循环的语法和在列表中一样,但是集合是无序的,所以你在访问集合的元素的时候,不能做关于顺序的假设。
- 3.3,集合推导Set comprehensions:和字典推导一样,可以很方便地构建集合:
- 3.4常用函数如 set<=other,set>=other,isdisjoint(other),union(*others),intersection(*others),c,symmetric_difference(other),copy,update(*others),difference_update(*others),pop,discarld,remove,add
- 4,元组Tuples
- 其他常用函数如cmp等及range,class,function,File,memoryview等目标详见网址:
- 补充1:string
- 补充2:int,float,complex常用操作
- 第一部分附录
- 二,Numpy
- 三,Scipy模块
- 四,Matplotlib
python deep learning基础
一,python 容器类型
1,列表
1.1,列表,就是python中的数组,长度可变,且能包含不同类型元素,
xs = [3, 1, 2] # Create a list
print xs, xs[2] # Prints "[3, 1, 2] 2"
print xs[-1] #负数是从后往前计数, Negative indices count from the end of the list; prints "2"
xs[2] = 'foo' # Lists can contain elements of different types
print xs # Prints "[3, 1, 'foo']"
xs.append('bar') #添加元素 Add a new element to the end of the list
print xs # Prints
x = xs.pop() #除去元素 Remove and return the last element of the list
print x, xs # Prints "bar [3, 1, 'foo']"
1.2,切片操作,可用切片方式获取列表中的元素
nums = range(5) # range is a built-in function that creates a list of integers
print nums # Prints "[0, 1, 2, 3, 4]"
print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print nums # Prints "[0, 1, 8, 9, 4]"
1.3 循环Loops:enumerate 函数访问每个循环元素指针
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
#同时打印出指针和元素, Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
1.4 列表推导List comprehensions,转换数据类型时可以简化代码
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]# x ** 2 是操作,if x % 2 == 0是条件
print even_squares # Prints "[0, 4, 16]"
1.5 List列表常用函数:index(x),extend(L),insert(i, x),count(x),remove(x),pop([i]),sort(cmp=None, key=None, reverse=False),sort(cmp=None, key=None, reverse=False)等
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count('x')
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>> a.pop()
1234.5
>>> a
[-1, 1, 66.25, 333, 333]
1.6 更多函数如popleft,del a【0】,map,用作堆栈,函数工具栏等更多信息详见以下网址
https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-queues
2,字典
2.1,字典存储键对值,d[‘fish’] = ‘wet’ ,fish为键,wet为值
d = {
'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
print d['cat'] # Get an entry from a dictionary; prints "cute"
print 'cat' in d # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet' # Set an entry in a dictionary
print d['fish'] # Prints "wet"
# print d['monkey'] # KeyError: 'monkey' not a key of d
print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"
print d.get('fish', 'N/A') # Get an element with a default; prints "wet"
del d['fish'] #删除键对值 Remove an element from a dictionary
print d.get('fish', 'N/A') # 不存在默认N/A,"fish" is no longer a key; prints "N/A"
2.2,循环loops,在字典中,用键来迭代更加容易。如果你想要访问键和对应的值,那就使用iteritems方法:
d = {
'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print 'A %s has %d legs' % (animal, legs)
# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
如果你想要访问键和对应的值,那就使用iteritems方法:
d = {
'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.iteritems():
print 'A %s has %d legs' % (animal, legs)
# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
可操作后再循环</