`
文章列表
问题: 想要让输出的字符能够按照左对齐,右对齐,居中对齐的格式排列 解决方法: 在Python中,是用str的rjust,ljust,center方法来实现上述的功能.看下面的例子: >>> print '|', 'hej'.ljust(20), '|', 'hej'.rjust(20), '|', 'hej'.center(20), '|' | hej             |             hej |       hej       | >>> print 'hej'.center(20, '+') ++++++++hej+++++++++ ...
问题: 需要判断一个对象,或者方法的一个参数,看它们是否是字符串. 解决方法: 判断一个对象是否是字符串或者unicode串的最简单有效的办法是使用isinstance和basestring def isAString(anobj):     return isinstance(anobj, basestring) 一般的说,判断一个对象是否是字符串,最容易想到的方法是: def isString(str):     return type(str) is type('') 这样有一些缺点:无法判断unicode字符串,或者用户从basestring继承下来的字符串类型. 当然,上面 ...
问题: 需要把一个字符(ASCII或Unicode)转换为数字编码,或者转过来 解决方法: 对于ASCII字符,可以使用内建的ord和chr方法实现需求: >>> chr(97) 'a' >>> ord('a') 97 对于Unicode字符,需要使用ord和repr,获得unicode字符的方法,使用unichr: >>> print ord(u'\u2020') 8224 >>> print repr(unichr(8224)) u'\u2020' 相关说明: 注意chr(n) 与 str(n)的区别 ...
问题: 要一个一个的处理字符串中的字符。 解决方法: 你能使用带有string的list作为它的参数去构建一个字符的list(也就是说,每个字符串的长度为一) thelist = list(thestring) 在python中,字符串是不可变的字符的序列。所以,可以像操作普通的序列一样,按照下标来处理字符。如果要依次处理所有的字符,写一个for循环是效率比较高的方法。 如: for c in thestring:     do_something_with(c) 更快捷的方法: results = [do_something_with(c) for c in thestring] ...
URI:http://www.pythonchallenge.com/pc/hex/ambiguity.html Username: butter; password: fly 说明: -- 解题方法 # Copyright for the following five classes by John Eriksson # <http://arainyday.se/>, 2006. Originally written for the AStar # library <http://www.pygame.org/projects/9/195/> and re ...
URI:http://www.pythonchallenge.com/pc/hex/bonus.html Username: butter; password: fly 说明: -- 解题方法: import this 过关答案: ambiguity
URI: http://www.pythonchallenge.com/pc/hex/copper.html Username: butter; password: fly 说明: — 解题方法: import Image, ImageSequence img = Image.open("white.gif") # http://www.pythonchallenge.com/pc/hex/white.gif out = Image.new("P", (125, 125)) pix = out.load() pos = [25, 25 ...
URI: — Username: butter; password: fly 说明: -- 解题方法: import zlib, bz2 h = open("package.pack") # Hides within the ZIP file we got at the data = h.read() # end of level 20. h.close() output = [] while True: if data.startswith("BZh"): data = bz2 ...
URI: http://www.pythonchallenge.com/pc/hex/idiot2.html Username: butter; password: fly 说明: 得到的Zip文件就是level 21. 解题方法: import httplib, base64 base64_login = base64.encodestring('%s:%s' % ("butter", "fly"))[:-1] headers = {"Authorization": "Basic %s" % b ...
URI: http://www.pythonchallenge.com/pc/hex/bin.html Username: butter; password: fly 说明: — 解题方法: import base64, wave, array # Input ... h = open("data") a = array.array("c", base64.b64decode(h.read())) a.byteswap() h.close() # ... Processing ... h = wave.open(&qu ...
URI: http://www.pythonchallenge.com/pc/return/balloons.html Username: huge; password: file 说明: — 解题方法: import gzip, difflib # Download the GNU zip file from: # http://www.pythonchallenge.com/pc/return/deltas.gz h = gzip.open("deltas.gz") d = difflib.Differ() part_1, part_2 = ...
URI: http://www.pythonchallenge.com/pc/return/romance.html Username: huge; password: file 说明: — 解题方法: import re, urllib, urllib2, bz2, xmlrpclib # First of all, get the data that is hidden within the "Set-Cookie" headers: uri = "http://www.pythonchallenge.com/pc/def/linkedli ...
URI: http://www.pythonchallenge.com/pc/return/mozart.html Username: huge; password: file 说明: — 解题方法: import Image, ImageChops # Download the image from: http://www.pythonchallenge.com/pc/return/mozart.gif image = Image.open("mozart.gif") magic = chr(195) for y in range(image. ...
URI: http://www.pythonchallenge.com/pc/return/uzi.html Username: huge; password: file 说明: 题面1**6年 Jan 26的后一天,猜猜这是谁的生日?用google搜搜吧 解题方法: import calendar print [i for i in range(1006, 2006, 10) if calendar.weekday(i, 1, 1) == 3][-3] 过关答案: mozart
URI: http://www.pythonchallenge.com/pc/return/italy.html Username: huge; password: file 说明: — 解题答案: import Image, math def get_pixel(t): t = (100 * 100 - 1) - t shell = int((math.sqrt(t) + 1) / 2) leg = 0 if (shell == 0) else int((t - (2 * shell - 1) ** 2) / 2 / shell) elt ...
Global site tag (gtag.js) - Google Analytics