哪个更快? (如果不是m in m)或(如果m.count(b)> 0) [英] Which is faster? (if not b in m) or (if m.count(b) > 0)
问题描述
Python中哪个更快,为什么?
jc = {}; m = []
x = [[1,2,3,4,5,6,7,8,9],[..],.......]#向上10000个条目
def mcountb():
for x中的项目:
b = item [:]; b.sort(); bc = 0
b中的bitem:bc + = int(bitem)
尝试:m = jc [bc]
除外:m = []
如果m.count(b)== 0:
m.append(b); jc [bc] = m
def binm()
x中的项目:
b = item [:]; b.sort(); bc = 0
b中的bitem:bc + = int(bitem)
尝试:m = jc [bc]
除外:m = []
如果不是b in m:
m.append(b); jc [bc] = m
< 11 *************** *******@z14g2000cwz.googlegroups .com> ;, Farel写道:
Python中哪个更快?为什么?
``如果不是b in m``查看`m`的每个元素,直到它找到`b`
然后停止。假设`b`在`m`中,否则`m`的所有元素都是
" touch。
``if m.count( b)> 0``将始终通过
`count()`方法中的`m`的所有元素。
Ciao,
Marc''BlackJack''Rintsch
Em Ter,2006-02-14?* s 20:14 -0800,Farel escreveu:Python中哪个更快,为什么?
jc = {}; m = []
x = [[1,2,3,4,5,6,7,8,9],[..],.......]#向上10000个条目
def binm()
对于x中的项目:
b = item [:]; b.sort(); bc = 0
对于b中的bitem:bc + = int(bitem)
尝试:m = jc [bc]
除了:m = []
如果不是b in m :
m.append(b); jc [bc] = m
如果只是将
元素求和,为什么要复制列表并对其进行排序?而不是
b = item [:]; b.sort(); bc = 0
bitem in b:bc + = int(bitem)
你可以做的只是
bc = sum(int(i)for i in item)
或者,如果订单*真的很重要,那么非常奇怪,你可以做到
bc = sum(int(i)for i in sorted(item))
另外,如果你想拥有字典jc的值
只有唯一的元素,你可以使用集合:
a = set()<打印
设置([])a.add(10)
打印
设置([10])a.add(10)
打印
set([10])a.add(10)
print a
set([10])
所以,最后的例子是(假设你不需要总结
订单):
def binm():
for x中的项目:
bc = sum(项目中i的int(i))
尝试:
jc [bc] .add(b)
除了KeyError:
jc [bc] = set([b])
看起来更漂亮,声明更少,而且(或应该)更快。这个
仅适用于Python 2.4,但您可以将其调整为在其他版本上运行,
。
干杯,
Felipe。
-
Quem excele em empregar a for?§amilitarsubjulga os ex ?? rcitos dos
outros povos sem travar batalha,toma cidades fortificadas dos outros
povos sem as atacar e destr?3i os estados dos outros povos sem lutas
prolongadas。 Deve lutar sob o C ?? u com o prop?3sito primordial da
''preserva?§?£o''。 Desse modo suas armas n?£se se embotar?£o,e os ganhos
poder?£o ser preservados。 Essa ?? a estrat ?? gia para planejar ofensivas。
- Sun Tzu,emA arte da guerra
Farel写道:Python中哪个更快?为什么?
为什么不亲自尝试?
提示:
来自timeit进口计时器
帮助(计时器)
-
bruno desthuilliers
python -c" print''@''。join([''。''。join([w [:: - 1] for p in p.split( ''。'')])
p in''o **** @ xiludom.gro''。split(''''')])"
Which is Faster in Python and Why?
jc = {}; m = []
x = [ [1,2,3,4,5,6,7,8,9],[..],.......] # upwards of 10000 entries
def mcountb():
for item in x:
b = item[:]; b.sort(); bc = 0
for bitem in b: bc += int(bitem)
try: m = jc[bc]
except: m = []
if m.count(b) == 0:
m.append(b); jc[bc]=m
def binm()
for item in x:
b = item[:]; b.sort(); bc = 0
for bitem in b: bc += int(bitem)
try: m = jc[bc]
except: m = []
if not b in m:
m.append(b); jc[bc]=m
In <11**********************@z14g2000cwz.googlegroups .com>, Farel wrote:
Which is Faster in Python and Why?
``if not b in m`` looks at each element of `m` until it finds `b` in it
and stops then. Assuming `b` is in `m`, otherwise all elements of `m` are
"touched".
``if m.count(b) > 0`` will always goes through all elements of `m` in the
`count()` method.
Ciao,
Marc ''BlackJack'' Rintsch
Em Ter, 2006-02-14 ?*s 20:14 -0800, Farel escreveu:Which is Faster in Python and Why?
jc = {}; m = []
x = [ [1,2,3,4,5,6,7,8,9],[..],.......] # upwards of 10000 entries
def binm()
for item in x:
b = item[:]; b.sort(); bc = 0
for bitem in b: bc += int(bitem)
try: m = jc[bc]
except: m = []
if not b in m:
m.append(b); jc[bc]=m
Why do you copy the list and sort it if you''re just summing its
elements? Instead of
b = item[:]; b.sort(); bc = 0
for bitem in b: bc += int(bitem)
you can do just
bc = sum(int(i) for i in item)
or, if the order *really* matters, what is very strange, you can do
bc = sum(int(i) for i in sorted(item))
Another thing, if you want to have in the values of the dictionary jc
only unique elements, you can use sets:
a = set()
print a set([]) a.add(10)
print a set([10]) a.add(10)
print a set([10]) a.add(10)
print a
set([10])
So, the final example would be (assuming that you don''t need to sum in
order):
def binm():
for item in x:
bc = sum(int(i) for i in item)
try:
jc[bc].add(b)
except KeyError:
jc[bc] = set([b])
Looks prettier, have less statements and is (or should be) faster. This
only works in Python 2.4, but you can adapt it to run on other versions,
too.
Cheers,
Felipe.
--
"Quem excele em empregar a for?§a militar subjulga os ex??rcitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destr?3i os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o C??u com o prop?3sito primordial da
''preserva?§?£o''. Desse modo suas armas n?£o se embotar?£o, e os ganhos
poder?£o ser preservados. Essa ?? a estrat??gia para planejar ofensivas."
-- Sun Tzu, em "A arte da guerra"
Farel wrote:Which is Faster in Python and Why?
Why don''t you try by yourself ?
hint :
from timeit import Timer
help(Timer)
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"
这篇关于哪个更快? (如果不是m in m)或(如果m.count(b)> 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!