• Python

    • 基本数据类型
    • 容器
      • 列表
      • 字典
      • 集合
      • 元组
    • 函数  
  • Numpy
    • 数组  
    • 访问数组
    • 数据类型
    • 数组计算
    • 广播
  • SciPy
    • 图像操作
    • MATLAB文件
    • 点之间的距离
  • Matplotlib
    • 绘制图形
    • 绘制多个图形
    • 图像

Python


python实现的经典的quicksort算法

 def quicksort(arr):
if len(arr)<=1:
return arr
pivot = arr[len(arr)/2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right) print quicksort([3,6,8,10,1,2,1])
#prints "[1,1,2,3,6,8,10]"

数字:整型和浮点型的使用与其他语言类似。

x = 3
print(type(x))#printd "<type 'int'>"
print(x) #prints "3"
print(x + 1) #Addition;prints "4"
print(x - 1) #Subtraction; prints "2"
print(x * 2) #Multiplication; prints "6"
print(x ** 2) #Expoentiaton; prints "9"
x += 1
print(x)# prints "4"
x *= 2
print(x) #prints "8"
y = 2.5
print(type(y))# prints "<type 'float'>"
print(y,y + 1,y * 2,y ** 2) #prints "2.5 3.5 5.0 6.25"

注意!!!! Python 中没有x++和x--的操作符。

布尔型:Python实现了所有的布尔逻辑。

t = True
f = False print(type(t)) #prints "<type'bool'>"
print(t and f ) #logical AND; prints "False"
print(t or f )#Logical OR; prints "True"
print( not t )#Logical NOT; prints "False"
print(t != f) #Logical XOR;prints "True"

字符串

hello = 'hello' #String literals  can use single quotes
world = 'world' # pr double quotes; it does not matter,
print(hello) #prints "hello"
print(len(hello)) #String length; prints "5"
hw = hello + ' '+ world #String concatention
print(hw)# prints "hello world "
hw12 = '%s %s %d' % (hello,world,12) #sprintf style string formatting
print(hw12)#prints "hello world 12"

字符串对象的方法

s = "hello"
print(s.capitalize()) #Capitalize a string prints "hello"
print(s.upper()) #Convert a stirng to upercase; prints "HELLO"
print(s.rjust(7))#Righr-justify a string,padding with space; prints " hello"
print(s.center(7))#Center a string,padding with space;prints " hello "
print(s.replace('l','(ell)')) #Replace all instance of one substring with another;
#prints "he(ell)(ell)o"
print(' world'.strip())#Strip leading and trailing whitespace; prints "world"

容器 Containers

容器类型:列表(lists) 字典(dictionaries) 集合(sets)和元组(tuples)

列表Lists

列表就是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']"

切片Slicing : 为了一次性地获取列表中的元素,python 提供了一种简洁的语法,这就是切片。

nums = list(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]"

在Numoy数组的内容中,再次看到切片语法。

循环LOOPS

animals =['cat','dog','monkey']
for animal in animals:
print(animal)
#prints "cat", "dog","monkey",each on its own line.

如果想要在循环体内访问每个元素的指针,可以使用内置的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

列表推导List comprehensions :在编程的时候,我们常常想要将一个数据类型转换为另一种。

将列表中的每个元素变成它的平方。

nums = [0,1,2,3,4]
squares = []
for x in nums:
squares.append(x ** 2)
print(squares) #Prints [0,1,4,9,16]

使用列表推导:

nums  = [0,1,2,3,4]
squares = [x ** 2 for x in nums]
print(squares) # prints [0,1,4,9,16]

列表推导还可以包含条件:

nums = [0,1,2,3,4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares) # prints "[0,4,16]"

字典Dicionaries

字典用来存储(键,值)对,

d = {'cat':'cute','dog':'furry'}#Create a new dictionary with come 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 "Ture"
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')) # "fish" is no longer a key ;prints "N/A"

循环LOOPS:在字典中,用键来迭代。

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"

访问键和对应的值,使用items方法:

d = {'person':2,'cat':4,'spider':8}
for animal,legs in d.items():
print('A %s has %d legs' % (animal,legs))
#prints " A person has 2 legs","A spider has 8 legs","A cat has 4 legs"

字典推导Dictionary comprehensions :和列表推导类似,但是允许你方便地构建字典。

nums = {0,1,2,3,4}
even_num_square = {x:x**2 for x in nums if x % 2 == 0}
print(even_num_square) # prints "{0:0,2:4,4:16}"

CS231n:Python Numpy教程的更多相关文章

  1. CS231n课程笔记翻译1:Python Numpy教程

    译者注:本文智能单元首发,翻译自斯坦福CS231n课程笔记Python Numpy Tutorial,由课程教师Andrej Karpathy授权进行翻译.本篇教程由杜客翻译完成,Flood Sung ...

  2. python numpy 教程

    http://blog.chinaunix.net/uid-21633169-id-4408596.html

  3. numpy教程

    [转]CS231n课程笔记翻译:Python Numpy教程 原文链接:https://zhuanlan.zhihu.com/p/20878530 译者注:本文智能单元首发,翻译自斯坦福CS231n课 ...

  4. 给深度学习入门者的Python快速教程 - numpy和Matplotlib篇

    始终无法有效把word排版好的粘贴过来,排版更佳版本请见知乎文章: https://zhuanlan.zhihu.com/p/24309547 实在搞不定博客园的排版,排版更佳的版本在: 给深度学习入 ...

  5. Python Numpy基础教程

    Python Numpy基础教程 本文是一个关于Python numpy的基础学习教程,其中,Python版本为Python 3.x 什么是Numpy Numpy = Numerical + Pyth ...

  6. Python 机器学习库 NumPy 教程

    0 Numpy简单介绍 Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过numpy ...

  7. 给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV

    这次博客园的排版彻底残了..高清版请移步: https://zhuanlan.zhihu.com/p/24425116 本篇是前面两篇教程: 给深度学习入门者的Python快速教程 - 基础篇 给深度 ...

  8. 在python&numpy中切片(slice)

     在python&numpy中切片(slice) 上文说到了,词频的统计在数据挖掘中使用的频率很高,而切片的操作同样是如此.在从文本文件或数据库中读取数据后,需要对数据进行预处理的操作.此时就 ...

  9. numpy教程:统计函数Statistics

    http://blog.csdn.net/pipisorry/article/details/48770785 , , ] , '\n') 输出: True 当然可以设置度参数bias : int, ...

随机推荐

  1. go 学习笔记之学习函数式编程前不要忘了函数基础

    在编程世界中向来就没有一家独大的编程风格,至少目前还是百家争鸣的春秋战国,除了众所周知的面向对象编程还有日渐流行的函数式编程,当然这也是本系列文章的重点. 越来越多的主流语言在设计的时候几乎无一例外都 ...

  2. Winform将FastReport的report与PreviewControl建立绑定关系

    场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  3. Python--使用四种随机方法(Random)来产生随机价格

    import random # 卖橘子的计算器:写一段代码,提示用户输入橘子的价格,# 然后随机生成购买的斤数(5到10斤之间),最后计算出应该支付的金额! # 第一种# orange_price = ...

  4. Mysql学习笔记整理之索引

    索引的概念: 索引是一个分散存储的数据结构(检索)对数据库表中一列或多列的值进行排序 为什么要用索引? 索引能极大的减少存储引擎需要扫描的数据量 索引可以把随机IO变成顺序IO 索引可以帮助我们进行分 ...

  5. 200行代码实现Mini ASP.NET Core

    前言 在学习ASP.NET Core源码过程中,偶然看见蒋金楠老师的ASP.NET Core框架揭秘,不到200行代码实现了ASP.NET Core Mini框架,针对框架本质进行了讲解,受益匪浅,本 ...

  6. 数据结构之二叉树篇卷一 -- 建立二叉树(With Java)

    一.定义二叉树节点类 package tree; public class Node<E> { public E data; public Node<E> lnode; pub ...

  7. 微信小程序开发简述

    微信小程序简述 什么是微信小程序? 微信小程序,简称小程序,英文名Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用.全面开放 ...

  8. 面试并发volatile关键字时,我们应该具备哪些谈资?

    提前发现更多精彩内容,请访问 个人博客 提前发现更多精彩内容,请访问 个人博客 提前发现更多精彩内容,请访问 个人博客 写在前面 在 可见性有序性,Happens-before来搞定 文章中,happ ...

  9. Creator3D长什么样?看看官方惊艳的DEMO就知道了,附在线体验!

    Shawn 这两天在学习 Creator3D 的官方案例,由于是刚接触 Creator3D 很多东西在没弄清楚之前还是以简单的编辑介绍为主,先了解一下3D场景的基本操作: 观查场景:按住鼠标右键以自己 ...

  10. Weex项目快速打包

    安装最新稳定版的Node.js 运行 cnpm install -g weex-toolkit 安装Weex 官方提供的 weex-toolkit 脚手架工具到全局环境中 运行 weex create ...