由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - python question
相关主题
How to have another func call printf with va_arg list ?inline functions in C++
10个包子请教一个简单的编程问题一个C++的概念问题
go 怎么 disable这个intel icc hash_map 求救!
何用openpyxl 从excel 文档的某一行读起?STL感觉实在太变态了
How to use a function return by reference in C++[菜鸟问题]类模板问题
golang的method是后来加的?c++ iterator 弱问
Golang的promise lib哪个好?c++ template question:
大牛帮我看看这个test code为什么complie 不了啊请问Linux底下有没有最简易的show 2D x-y curve的工具
相关话题的讨论汇总
话题: func话题: join话题: range话题: wrap
进入Programming版参与讨论
1 (共1页)
w*s
发帖数: 7227
1
say i have a string "0001001101111111",
want to convert it into
"0001-0011-0111-1111",
how simple can you do it in python ?
a**a
发帖数: 416
2
# Python, pasted just now:
s = "0001001101111111"
print '-'.join(s[i:i+4] for i in range(0, len(s), 4))
# Output:
0001-0011-0111-1111
d****n
发帖数: 1637
3
b="-".join([a[i:i+4] for i in range(0,len(a),4) ])
i******l
发帖数: 270
4
'-'.join([a[i:j] for i, j in zip(range(0, len(a) , 4), range(4,len(a)+4, 4))
])
虽然work,但是很勉强,丑

【在 w*s 的大作中提到】
: say i have a string "0001001101111111",
: want to convert it into
: "0001-0011-0111-1111",
: how simple can you do it in python ?

p**o
发帖数: 3409
5
>>> s = "0001001101111111"
>>> zip(*[iter(s)]*4)
[('0', '0', '0', '1'), ('0', '0', '1', '1'),
('0', '1', '1', '1'), ('1', '1', '1', '1')]
>>> [''.join(x) for x in zip(*[iter(s)]*4)]
['0001', '0011', '0111', '1111']
>>> '-'.join(''.join(x) for x in zip(*[iter(s)]*4))
'0001-0011-0111-1111'

【在 w*s 的大作中提到】
: say i have a string "0001001101111111",
: want to convert it into
: "0001-0011-0111-1111",
: how simple can you do it in python ?

r*******n
发帖数: 3020
6
import re
'-'.join(re.findall('\d{4}', "0001001101111111"))

【在 w*s 的大作中提到】
: say i have a string "0001001101111111",
: want to convert it into
: "0001-0011-0111-1111",
: how simple can you do it in python ?

w*s
发帖数: 7227
7
what kind of company uses python, sounds like interesting job
r*g
发帖数: 3159
8
不相干的,用两天前看到的J:
}.,'-',._4]\ '0001001101111111'
0001-0011-0111-1111
m********5
发帖数: 17667
9
There is a very useful module for text routines: textwrap
>>> import textwrap
>>> "-".join(textwrap.wrap("0001001101111111",4))
'0001-0011-0111-1111'

【在 w*s 的大作中提到】
: say i have a string "0001001101111111",
: want to convert it into
: "0001-0011-0111-1111",
: how simple can you do it in python ?

w******p
发帖数: 166
10
several ways with s='0001001101111111'
''.join(x if i%4 or i==0 else '-'+x for i,x in enumerate(s))
'-'.join(['{0:04b}'.format(int(x,16)) for x in "%x"%(int(s,2))])
reduce(lambda x, y: x+'-'+y, [s[i::4] for i in range(len(s)/4)])
import re; re.sub(r'(....)', lambda x: '-'+x.group(0), s)[1:]
import struct; '-'.join(struct.unpack(">"+"4s"*(len(s)/4),s))
a****e
发帖数: 9589
11
最先想到re,然后是mod(%),range 想的妙(佩服),textwrap 从未接触过(学习
了),其他的从直觉上觉得效率不高,pass了。
写了个测试程序如下:
import timeit
s = "0001001101111111"
def mtimeit(func):
def wrap(*args, **kwargs):
t = timeit.Timer(func)
print func.func_name,
print min(t.repeat(number=1))
return func
return wrap
@mtimeit
def re_func():
import re
return '-'.join(re.findall('\d{4}', s))
@mtimeit
def mod_func():
return ''.join(x if i%4 or i==0 else '-'+x for i,x in enumerate(s))

@mtimeit
def wrap_func():
import textwrap
return "-".join(textwrap.wrap(s,4))

@mtimeit
def range_func():
return '-'.join(s[i:i+4] for i in range(0, len(s), 4))


if __name__ == '__main__':
re_func()
mod_func()
wrap_func()
range_func()

为保证结果相对准确,运行了5次,结果都是range >> re >> mod >> wrap,数据如下:
1,
re_func 8.10623168945e-06
mod_func 9.05990600586e-06
wrap_func 3.91006469727e-05
range_func 5.96046447754e-06
2,
re_func 7.86781311035e-06
mod_func 1.00135803223e-05
wrap_func 3.88622283936e-05
range_func 6.91413879395e-06
3,
re_func 7.86781311035e-06
mod_func 9.05990600586e-06
wrap_func 4.10079956055e-05
range_func 7.15255737305e-06
4,
re_func 8.10623168945e-06
mod_func 1.00135803223e-05
wrap_func 3.50475311279e-05
range_func 5.96046447754e-06
5,
re_func 8.82148742676e-06
mod_func 1.00135803223e-05
wrap_func 4.00543212891e-05
range_func 6.91413879395e-06
结论:
lz 给我和arya (死火山下) 发包子把
m********5
发帖数: 17667
12
因为import, dot操作等消耗的时间远超过其他步骤
所以我觉得有必要修改一下,直接把reptfda用在测试func中
import re
rept=re.compile('\d{4}')
reptfda=rept.findall
为了 eliminate global var lookup overhead 的影响,应该多尝试几次 number=1应
该是不行的。
修改了一下:
def testAthome2(s,niter):
import re
rept=re.compile('\d{4}')
reptfda=rept.findall
def mtimeit(func):
def wrap(*args, **kwargs):
t = timeit.Timer(func)
print func.func_name,
print min(t.repeat(number=niter))
return func
return wrap

。。。
@mtimeit
def re_func():
#import re
return '-'.join(re.findall('\d{4}', s))
@mtimeit
def re2_func():
return '-'.join(reptfda(s))

。。。
re2_func()
。。。
s="0001001101111111"
testAthome2(s,1)
re_func 5.96046447754e-06
re2_func 2.86102294922e-06
mod_func 1.09672546387e-05
wrap_func 4.38690185547e-05
range_func 5.96046447754e-06
testAthome2(s,10)
re_func 3.19480895996e-05
re2_func 2.00271606445e-05
mod_func 5.48362731934e-05
wrap_func 0.000328063964844
range_func 2.59876251221e-05
testAthome2(s,1000)
re_func 0.00301098823547
re2_func 0.0019268989563 #最快?!
mod_func 0.00567197799683
wrap_func 0.0313379764557
range_func 0.0023398399353
再来看看长string下的表现
testAthome2(s*50,10)
re_func 0.000476121902466
re2_func 0.000459909439087#慢下来了但是差距很小; 想到它其实可以比较方便扩展,
还是不错的
mod_func 0.00182509422302
wrap_func 0.113087892532
range_func 0.00037407875061#最快
但是目前re的实现在仅仅需要做compartmentation的时候缺点很明显:
>>> s="10"*8
>>> s2=s+'22'
>>> '-'.join(reptfda(s))
'1010-1010-1010-1010'
>>> '-'.join(s2[i:i+4] for i in range(0, len(s2), 4))
'1010-1010-1010-1010-22'
可以用rept=re.compile('\d{1,4}')

【在 a****e 的大作中提到】
: 最先想到re,然后是mod(%),range 想的妙(佩服),textwrap 从未接触过(学习
: 了),其他的从直觉上觉得效率不高,pass了。
: 写了个测试程序如下:
: import timeit
: s = "0001001101111111"
: def mtimeit(func):
: def wrap(*args, **kwargs):
: t = timeit.Timer(func)
: print func.func_name,
: print min(t.repeat(number=1))

a****e
发帖数: 9589
13
其实结果是一样的
如果需要import,当然import 的时间也算在运行时间之中,否则的话我们不如直接修
改module,来产生结果,那样岂不最快?是不是有点掩耳盗铃
对于长字串,s = "0001001101111111" * 1000,结果和惊人内:
re_func 0.00101804733276
mod_func 0.00382304191589
wrap_func 7.6852619648
range_func 0.000818967819214

【在 m********5 的大作中提到】
: 因为import, dot操作等消耗的时间远超过其他步骤
: 所以我觉得有必要修改一下,直接把reptfda用在测试func中
: import re
: rept=re.compile('\d{4}')
: reptfda=rept.findall
: 为了 eliminate global var lookup overhead 的影响,应该多尝试几次 number=1应
: 该是不行的。
: 修改了一下:
: def testAthome2(s,niter):
: import re

A******D
发帖数: 1075
14
哎,大家一上来就比赛时间;我觉得可读性、不易出错性最重要。所以一般re的东西我
能避免就避免,太容易出错了。range那个比较好懂。
1 (共1页)
进入Programming版参与讨论
相关主题
请问Linux底下有没有最简易的show 2D x-y curve的工具How to use a function return by reference in C++
用那个design pattern好?golang的method是后来加的?
关于inserterGolang的promise lib哪个好?
binary_search只要求forward_iterator?大牛帮我看看这个test code为什么complie 不了啊
How to have another func call printf with va_arg list ?inline functions in C++
10个包子请教一个简单的编程问题一个C++的概念问题
go 怎么 disable这个intel icc hash_map 求救!
何用openpyxl 从excel 文档的某一行读起?STL感觉实在太变态了
相关话题的讨论汇总
话题: func话题: join话题: range话题: wrap