深度学习python及numpy等第三方包基础

文章目录

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"

可操作后再循环</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值