继续学习http://www.cnblogs.com/vamei
1. 表和tuple,len(s), min(s),max(s),sum(s)
all(s) 返回: True, 如果所有元素都为True的话
any(s) 返回: True, 如果任一元素为True的话
s.count(x) 返回: x在s中出现的次数
s.index(x) 返回: x在s中第一次出现的下标
2.表
# l为一个表, l2为另一个表
l.extend(l2) 在表l的末尾添加表l2的所有元素
l.append(x) 在l的末尾附加x元素
l.sort() 对l中的元素排序
l.reverse() 将l中的元素逆序
l.pop() 返回:表l的最后一个元素,并在表l中删除该元素
del l[i] 删除该元素
(以上这些方法都是在原来的表的上进行操作,会对原来的表产生影响,而不是返回一个新表。)
#str为一个字符串,sub为str的一个子字符串。s为一个序列,它的元素都是字符串。width为一个整数,用于说明新生成字符串的宽度。
str.count(sub) 返回:sub在str中出现的次数
str.find(sub) 返回:从左开始,查找sub在str中第一次出现的位置。如果str中不包含sub,返回 -1
str.index(sub) 返回:从左开始,查找sub在str中第一次出现的位置。如果str中不包含sub,举出错误
str.rfind(sub) 返回:从右开始,查找sub在str中第一次出现的位置。如果str中不包含sub,返回 -1
str.rindex(sub) 返回:从右开始,查找sub在str中第一次出现的位置。如果str中不包含sub,举出错误
str.isalnum() 返回:True, 如果所有的字符都是字母或数字
str.isalpha() 返回:True,如果所有的字符都是字母
str.isdigit() 返回:True,如果所有的字符都是数字
str.istitle() 返回:True,如果所有的词的首字母都是大写
str.isspace() 返回:True,如果所有的字符都是空格
str.islower() 返回:True,如果所有的字符都是小写字母
str.isupper() 返回:True,如果所有的字符都是大写字母
>>> s = '1,2,3,3'
>>> s.split(',')
['1', '2', '3', '3']
>>> s.split(',',2)
['1', '2', '3,3']
str.split([sep, [max]]) 返回:从左开始,以空格为分割符(separator),将str分割为多个子字符串,总共分割max次。将所得的子字符串放在一个表中返回。可以str.split(',')的方式使用逗号或者其它分割符
str.rsplit([sep, [max]]) 返回:从右开始,以空格为分割符(separator),将str分割为多个子字符串,总共分割max次。将所得的子字符串放在一个表中返回。可以str.rsplit(',')的方式使用逗号或者其它分割符
str.join(s) 返回:将s中的元素,以str为分割符,合并成为一个字符串。
str.strip([sub]) 返回:去掉字符串开头和结尾的空格。也可以提供参数sub,去掉位于字符串开头和结尾的sub
str.replace(sub, new_sub) 返回:用一个新的字符串new_sub替换str中的sub
str.capitalize() 返回:将str第一个字母大写
str.lower() 返回:将str全部字母改为小写
str.upper() 返回:将str全部字母改为大写
str.swapcase() 返回:将str大写字母改为小写,小写改为大写
str.title() 返回:将str的每个词(以空格分隔)的首字母大写
str.center(width) 返回:长度为width的字符串,将原字符串放入该字符串中心,其它空余位置为空格。
str.ljust(width) 返回:长度为width的字符串,将原字符串左对齐放入该字符串,其它空余位置为空格。
str.rjust(width) 返回:长度为width的字符串,将原字符串右对齐放入该字符串,其它空余位置为空格。
1.判断2008是否是闰年
def isLearYear(year):
... if year % 4 == 0:
... if year % 100 != 0 or (year % 100 == 0 and year % 400 == 0):
... return True
... return False
isLearYear(2008)
True
2.判断某年某月某日是某年第几天
mport time
now = time.strptime('20080101','%Y%m%d')
time.strftime('%j',now)
subDay = [31,28,31,30,31,30,31,31,30,31,30,31]
>>> def dayInyear(year, month, day):
... iday = sum(subDay[i] for i in range(0, month -1)) // sum(subDay[:month-1])
... iday += day
... if month > 2 and isLearYear(year):
... iday += 1
... return iday
//输入验证代码
dateinput = raw_input("Please Input date(1991.9.22) -> ")
if dateinput.find(".") > 0 : dates = dateinput.split(".")
if dateinput.find("/") > 0 : dates = dateinput.split("/")
if dateinput.find("-") > 0 : dates = dateinput.split("-")
year = int(dates[0])
month = int(dates[1])
day = int(dates[2])
if month<0 or month>12 :
print "error month"
if day<0 or day>31 :
print "error day"
print year, month, day
import operator
class UserScore:
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
file_name = 'record.txt'
user_scores = []
lines_read = []
has_invalid_name = False
# read and check lines
with open(file_name) as f:
for line in f:
if not line.startswith('#'):
if line[0].islower():
line = line[0].upper() + line[1:]
has_invalid_name = True
cur = line.strip().split(', ') // if len(cur) == 3:
user_scores.append(UserScore(cur[0], cur[1], int(cur[2])))
lines_read.append(line)
if has_invalid_name:
with open(file_name, 'w') as f:
f.write(''.join(lines_read))
print('data file has some invalid rows, i have updated it.')
print('users whose scores are below 60:')
for score in filter(lambda s: s.score < 60, user_scores):
print(score.name)
print('users whose names start with L:')
for score in filter(lambda s: s.name.startswith('L'), user_scores):
print(score.name)
all_scores = map(lambda s: s.score, user_scores)
reduce(lambda x,y: x+y, all_scores)
print('sum of scores is: %s' % reduce(operator.add, all_scores, 0))