- 浏览: 321002 次
- 性别:
- 来自: 广州
-
最新评论
-
flex_莫冲:
tks d
JAVA教材(推荐新手学习) -
magic_yao:
不错的简介
Python库 -
lzzzing:
import time
d = {1:1}
maxv,max ...
Project Euler 第14题 -
a173549010:
怎么资源被举报了,下不了
个人珍藏的MLDN李兴华J2EE视频教程(纳米盘下载) -
a173549010:
怎么下不了
个人珍藏的MLDN李兴华J2EE视频教程(纳米盘下载)
文章列表
问题:
想要让输出的字符能够按照左对齐,右对齐,居中对齐的格式排列
解决方法:
在Python中,是用str的rjust,ljust,center方法来实现上述的功能.看下面的例子:
>>> print '|', 'hej'.ljust(20), '|', 'hej'.rjust(20), '|', 'hej'.center(20), '|'
| hej | hej | hej |
>>> print 'hej'.center(20, '+')
++++++++hej+++++++++ ...
- 2009-06-08 10:48
- 浏览 803
- 评论(0)
问题:
需要判断一个对象,或者方法的一个参数,看它们是否是字符串.
解决方法:
判断一个对象是否是字符串或者unicode串的最简单有效的办法是使用isinstance和basestring
def isAString(anobj):
return isinstance(anobj, basestring)
一般的说,判断一个对象是否是字符串,最容易想到的方法是:
def isString(str):
return type(str) is type('')
这样有一些缺点:无法判断unicode字符串,或者用户从basestring继承下来的字符串类型.
当然,上面 ...
- 2009-06-08 10:37
- 浏览 3006
- 评论(0)
问题:
需要把一个字符(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)的区别 ...
- 2009-06-08 10:11
- 浏览 2329
- 评论(0)
问题:
要一个一个的处理字符串中的字符。
解决方法:
你能使用带有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]
...
- 2009-06-08 09:17
- 浏览 1266
- 评论(0)
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 ...
- 2009-06-04 12:42
- 浏览 1410
- 评论(0)
URI:http://www.pythonchallenge.com/pc/hex/bonus.html
Username: butter; password: fly
说明:
--
解题方法:
import this
过关答案:
ambiguity
- 2009-06-04 12:38
- 浏览 1023
- 评论(0)
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 ...
- 2009-06-04 12:35
- 浏览 914
- 评论(0)
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 ...
- 2009-06-04 12:32
- 浏览 959
- 评论(0)
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 ...
- 2009-06-04 08:39
- 浏览 1029
- 评论(0)
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 ...
- 2009-06-04 08:36
- 浏览 1300
- 评论(0)
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 = ...
- 2009-06-04 08:34
- 浏览 1162
- 评论(0)
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 ...
- 2009-06-04 08:32
- 浏览 1383
- 评论(0)
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. ...
- 2009-06-04 08:30
- 浏览 1095
- 评论(0)
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
- 2009-06-03 15:53
- 浏览 1055
- 评论(0)
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 ...
- 2009-06-03 15:34
- 浏览 1152
- 评论(0)