下载此文档

python学习笔记.doc


文档分类:IT计算机 | 页数:约13页 举报非法文档有奖
1/13
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/13 下载此文档
文档列表 文档介绍
word
word
1 / 13
word
Python lstrip() 方法用于截掉字符串左边的空格或指定字符。
Python strip() 方法用于移除字符串头尾指定的字符〔默认为空格〕。
Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否如此返回False。可选参数"start"与"end"为检索字符串的开始与完毕位置。
(suffix[, start[,end]])
不需要用split啊,split是分割用replace(src, des)#des为需要替换成的目标字串,src为需要替换的源字串如:>>> str = "My name is xxx, and yours? ...">>> print strMy name is xxx, and yours? ...>>> str = (',','')>>> print strMy name is xxx and yours? ...
(obj)
arr = [[1,2],[3,4],[5,6]]
1
取第一列的值
v = [x[0] for x in arr]
>>> a=[[1,2,3],[4,5,6]]
>>> a[0] #取一行
[1, 2, 3]
>>> a[:,0] #尝试用数组的方法读取一列失败
TypeError: list indices must be integers or slices, not tuple
1
2
3
4
word
word
2 / 13
word
5
​ 我们需要用列表解析的方法读取一列:
>>> b=[x[0] for x in a]
>>> print(b)
[1, 4]
 而对于数组,可以直接读取:
>>> import numpy as np
>>> a=([[1,2,3],[4,5,6]])
>>> a[:,0]
array([1, 4])
pop() 函数用于移除列表中的一个元素〔默认最后一个元素〕,并且返回该元素的值。
语法
pop()方法语法:
(obj=list[-1])
参数
obj -- 可选参数,要移除列表元素的对象。
返回值
该方法返回从列表中移除的元素对象。
word
word
3 / 13
word
实例
以下实例展示了 pop()函数的使用方法:
#!/usr/bin/python
aList =[123,'xyz','zara','abc'];
print"A List : ", ();
print"B List : ", (2);
以上实例输出结果如下:
A List: abc
B List: zara
python 判断字符串是否为空用什么方法?
s=''
if ()=='':
print's is null'
或者
ifnot ():
print's is null'
这是从网上搜索的一篇也的还可以的Python进制转换,经过验证可以使用。下面贴出它的实现代码:
word
word
4 / 13
word
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2/10/16 base trans. wrote by srcdog on 20th, April, 2009
# ld elements in base 2, 10, 16.
  
importos,sys
  
# global definition
# base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]
base =[str(x) forx inrange(10)] +[ chr(x) forx inrange(ord('A'),ord('A')+6)]
  
# bin2dec
# 二进制 to 十进制: int(str,n=10) 
defbin2dec(string_num):
  returnstr(int(string_num, 2))
  
# hex

python学习笔记 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数13
  • 收藏数0 收藏
  • 顶次数0
  • 上传人beny00001
  • 文件大小67 KB
  • 时间2021-12-03