关于 Python 中速度/效率/最佳实践的两个问题。以下哪一项是“更好”的(更快、更少的内存密集型等):
for x in list: #do something to x
或者
for x in xrange(len(list)): #do something to list[x]
for string in list_of_strings: for string2 in other_string_list: if string == string2: #do something
或者
import re for string in list_of_strings: if re.match('%s'%(string),other_strings): #or re.search(etc) #do something
不是很紧迫,我主要是好奇。我想我可以使用 timeit() 或其他什么方法获得某种原始数据,但我会欣赏更多的深度,而不仅仅是“这个比你的计算机上的那个快”。