python基础命令笔记

如何使用PyCharm且能够使用import matplotlib.pyplot as plt

1、下载并安装

python-2.7.10

numpy-1.9.0-win32-superpack-python2.7

matplotlib-1.4.3.win32-py2.7

pyparsing-2.1.10.win32-py2.7

setuptools-0.6c11.win32-py2.7

2、下载

six-1.10.0,复制其中的six.py到Python安装目录的Lib下

python-dateutil-1.4.1,确认安装了Python,并添加环境变量(例C:\Python27),打开cmd,进入python-dateutil-1.4.1目录输入python setup.py install




1、如何改变当前路径

import os

os.chdir('绝对路径')

例:os.chdir('D:\DL\code')

查看os.getcwd()

2、修改.py文件之后使用新定义的函数之前,需要重新加载xxx.py

reload(xxx)

如果是重新运行了.py,则需要重新import xxx

3、运行import matplotlib

安装matplotlib、numpy

需要安装six、pyparsing、dateutil

1)windows下将six.py放在..\Python27\Lib下

2)pyparsing可以下载exe)

3)dateuitl可以下载安装包,在python-dateutil-1.4.1目录下cmd运行python setup.py install命令安装

4、将文本记录转换为Numpy解析数据(代码来自机器学习实战Peter Harington)

#第一步:打开文件

fr = open()

#第二步:读取文件

arrayOfLines = fr.readlines()

#第三步:取其中一行进行解析

numOfLines = len(arrayOfLines)#获取文件记录的行数

returnMat =zeros((numOfLines,3)) #初始化返回矩阵

index = 0#记录当前处理的行数

for line in arrayOfLines#按行处理

line = lline.strip()#去掉换行符

dataInALine= line.split('\t')

returnMat[index,:] = dataInALine[0:3]

classLabels.append(int(dataInALine[-1]))#记得写类型int否则默认字符串,append是追加元素

index+=1

return returnMat, classLabels

5、基本操作

1)data.shape#取各个维度的大小

2)tile(data, (m,n))#http://blog.sina.com.cn/s/blog_6bd0612b0101cr3u.html

6、sorted,官方文档 http://docs.python.org/2/library/functions.html?highlight=sorted#sorted

1)函数原型sorted(iterable[,cmp[, key[, reverse]]])

2)iterable为要排序的list或者iterable

3)cmp与key,cmp可以指定排序函数,key指定排序的域,举例: 

students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

      sorted(students, key=lambda student : student[2])

4)reverse为true逆序从大到小

5)使用operator.itemgetter()

sorted(students, key=operator.itemgetter(2))

7、字典访问的三种方法http://www.cnblogs.com/kaituorensheng/archive/2012/09/05/2672711.html

定义字典 dic = {'a':"hello",'b':"how",'c':"you"}
方法一:
for key in dic:
  print key,dic[key]
  print key + str(dic[key])
结果:
  a hello
  ahello
  c you
  cyou
  b how
  bhow
细节:
print key,dic[key],后面有个逗号,自动生成一个空格
print key + str(dic[key]),连接两个字符串,用的是加号,直接输出,中间不加逗号
方法二:
for (k,v) in dic.items():
  print "dic[%s]="%k,v
结果:
  dic[a]= hello
  dic[c]= you
  dic[b]= how
方法三:
for k,v in dic.iteritems():
  print "dic[%s]="%k,v
结果:
  dic[a]= hello
  dic[c]= you
  dic[b]= how
对比:
items()返回的是列表对象,而iteritems()返回的是iterator对象。例如:
print dic.items()        #[('a', 'hello'), ('c', 'you'), ('b', 'how')]
print dic.iteritems()   #<dictionary-itemiterator object at 0x020E9A50>
深究:iteritor是迭代器的意思,一次返回一个数据项,直到没有为止
 for i in dic.iteritems():
   print i
结果:('a', 'hello')
        ('c', 'you')
        ('b', 'how')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值