摘要: 1 ten = set(range(10)) 2 lows = set([0, 1, 2, 3, 4]) 3 odds = set([1, 3, 5, 7, 9]) 4 5 lows.add(9) # lows set([0,1,2,3,4,9]) 6 lows.difference(odds) # set([0, 2, 4]) 7 lows.intersection(odds) # set([1,3 ,9]) 8 lows.issubset(ten) # True 9 lows.issuperset(odds) # False10 lows.remove(0)11 lows.symmet.. 阅读全文
posted @ 2013-01-10 22:02 roicel 阅读(598) 评论(0) 推荐(0) 编辑
摘要: import sysdef process_file(filename): '''Open, read, and print a file.''' input_file = open(filename, "r") for line in input_file: line = line.strip() print line input_file.close()if __name__ == '__main__': process_file(sys.argv[1]) 此代码的运行方式为 python 文件名 sy.. 阅读全文
posted @ 2013-01-10 21:33 roicel 阅读(237) 评论(0) 推荐(1) 编辑
摘要: 如果要用python匹配字符串的开头或末尾是否包含一个字符串,就可以用startswith,和endswith比如:content = 'ilovepython'如果字符串content以ilove开始,返回True,否则返回Falsecontent.startswith("ilove")返回truecontent.startswith("sss")返回false如果字符串content以python结尾,返回True,否则返回Falsecontent.endswith('python')返回truecontent.end 阅读全文
posted @ 2013-01-10 21:25 roicel 阅读(236) 评论(0) 推荐(0) 编辑