首页 技术 正文
技术 2022年11月19日
0 收藏 398 点赞 3,436 浏览 3948 个字

一.基本数据类型

1.整形 

a=10
type(a)
Out[75]: inta.bit_length() #字节长度
Out[76]: 4整形相除会返回整形,想要返回浮点数,需要用浮点数相除
1./4
Out[79]: 0.25

  

2.浮点型

b=0.25
type(b)
Out[80]: float浮点数表示成有理分式

 b.as_integer_ratio()
  Out[81]: (1, 4)

那么怎么确定精度呢? 需要引入decimal模块

import decimal
 from decimal import Decimal

decimal.getcontext()
 Out[82]: Context(prec=8, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Inexact, Rounded], traps=[InvalidOperation, DivisionByZero, Overflow])

可以看到 精度是28,还有最大值、最小值
可以改变精度

decimal.getcontext().prec=4 # 改变精度为4

e=Decimal(1)/Decimal(11)  #计算1/11
  print(e)

输出结果:
 0.09091

确定精度为10:

decimal.getcontext().prec=10

e=Decimal(1)/Decimal(11)
 print(e)

0.09090909091

3.字符串

字符串表示文本

t='this is a string't.capitalize()
Out[88]: 'This is a string'

按tab键可以有自动补全功能,可以查看内建方法。比如:图中有字符串的一些内建方法,capitalize等。

python金融应用(二)基本数据类型和结构

t.split()  #字符串的分割
Out[23]: ['this', 'is', 'a', 'string'] #结果分割为列表查找

 t.find(‘a’)  #查找元素‘a’,返回元素位置,注意:python位置下标从0开始
 Out[25]: 8

 替换

t.replace(‘ ‘,’|’)  #把空格替换为‘|’
 Out[27]: ‘this|is|a|string’

去除操作

s=’http://wwww.python.org’
 s.strip(‘htp:/’) #去除’htp:/’
 Out[29]: ‘wwww.python.org’

正则表达式 re的简单应用

import re

series=”””
’01/18/2014 13:00:00′,100,’1st’,   #三引号表示可以换行
’01/18/2014 13:00:00′,110,’2nd’,
’01/18/2014 13:00:00′,120,’3rd’,
“””

dt=re.compile(r”‘[0-9/:\s]+'”)  #匹配规则:反斜杠表示转译
result=dt.findall(series)
print(result)

[“’01/18/2014 13:00:00′”, “’01/18/2014 13:00:00′”, “’01/18/2014 13:00:00′”] #返回值是一个列表

接着把时间序列转化为datetime对象

from datetime import datetime

pydt=datetime.strptime(result[0].replace(“‘”,””),’%m/%d/%Y %H:%M:%S’)
pydt

Out[102]: datetime.datetime(2014, 1, 18, 13, 0)

type(pydt)

Out[103]: datetime.datetime

小注:

datetime模块的时间与字符串转化:

import datetime
from datetime import datetime

datetime.datetime.strftime():由日期格式转化为字符串格式

datetime.datetime.now().strftime(‘%m-%d-%Y %H:%M:%S’)

Out[105]: ’05-02-2018 18:27:58′

datetime.datetime.strptime():由字符串格式转化为日期格式

datetime.datetime.strptime(’05-16-2017 21:01:35′, ‘%m-%d-%Y %H:%M:%S’)
Out[107]: datetime.datetime(2018, 5, 2, 18, 27, 58)

具体参考:https://www.cnblogs.com/cindy-cindy/p/6720196.html

二.基本数据结构

1.元组:tpule

t=(1,2.5,'date')t1=1,2.5,'date'
type(t1)
Out[3]: tuplet1[2]
Out[4]: 'date'

只有两个方法 : 计数与下表返回索引
t.count(para)
t.index(para)

t.count(1)
Out[6]: 1t.index(1)
Out[7]: 0

2.列表

列表的构建

l=[1,2,1.5]
l1=[x for x in range(10)]
l2=list(t)
print(l,'\n',l1,'\n',l2)
[1, 2, 1.5]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2.5, 'date']

操作方法:

print('abc\ndef')  #\n  换行
abc
deftype(l1)
Out[10]: listl.append([4,3]) #添加列表
l
Out[11]: [1, 2, 1.5, [4, 3]]l.extend([1,3,6]) # 添加元素
l
Out[12]: [1, 2, 1.5, [4, 3], 1, 3, 6]l.insert(1,'date')
l
Out[13]: [1, 'date', 2, 1.5, [4, 3], 1, 3, 6]l.remove('date')
lOut[14]: [1, 2, 1.5, [4, 3], 1, 3, 6]p=l.pop(2) # pop 方法 可用返回值接受
print(l,'\n',p)[1, 2, [4, 3], 1, 3, 6]
1.5l[:2]
Out[16]: [1, 2]

3.字典

d={
'name':'John',
'sex':'male',
'age':18,
'country':'China'}
d['name']
Out[17]: 'John'd.keys() # 所有key组成的列表
Out[18]: dict_keys(['name', 'sex', 'age', 'country'])d.values()
Out[19]: dict_values(['John', 'male', 18, 'China'])d.items() # 字典转化为 list list中元素是元组,元组由字典的key 和balue组合
Out[20]: dict_items([('name', 'John'), ('sex', 'male'), ('age', 18), ('country', 'China')])print('{} is {}'.format(d['name'],d['age']))
John is 18birth=Trueif birth is True:
d['age']+=1print(d['age'])
19

  

字典的遍历

for i in d:
print(i)name
sex
age
country
for item in d.items():
print(item)('name', 'John')
('sex', 'male')
('age', 19)
('country', 'China')
for values in d.values():
print(values)John
male
19
China

4.集合

s=set(['u','i','du','du','u'])    print(s)   t=set(['du','u'])
t
{'i', 'u', 'du'}
Out[27]: {'du', 'u'}

交并补

s.union(t)
Out[29]: {'du', 'i', 'u'}s.intersection(t)
Out[30]: {'du', 'u'}s.difference(t)
Out[31]: {'i'}

5. 函数编程

简单的函数:

def f(x):
return x**2f(2)
Out[32]: 4  

求偶数:

def even(x):
return x%2==0even(2)
Out[33]: True

  

map函数:
 python3下的map()函数返回类型为iterators,不再是list

map()的使用方法形如map(f(x),Itera).对,它有两个参数,第一个参数为某个函数,第二个为可迭代对象。

list(map(even,range(4)))
Out[34]: [True, False, True, False]list(map(lambda x:x**2,range(4)))Out[35]: [0, 1, 4, 9]

filter 函数:

过滤功能,刷选出符合条件的

list(filter(even,range(15)))
Out[36]: [0, 2, 4, 6, 8, 10, 12, 14]

reduce函数:
 reduce()的使用方法形如reduce(f(x),Itera).对,它的形式和map()函数一样。不过参数f(x)必须有两个参数。reduce()函数作用是:把结果继续和序列的下一个元素做累积计算

from functools import reduce
reduce(lambda x,y:x+y,range(5))Out[37]: 10

其实就是类似下边的函数:

def cunsum(x):
total=0
for i in range(x):
total+=i
return totalcunsum(5)
Out[38]: 10
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898