for i in [0, 1, 2, 3, 4, 5]: print i**2
for i in range(6): print i**2
xrange
returns only one value at a time, and no extra memory is needed to store the entire array. for i in xrange(6): print i**2
xrange
already in the kernel and is simply called range
. colors = ['red', 'green', 'blue', 'yellow'] for i in range(len(colors)): print colors[i]
colors = ['red', 'green', 'blue', 'yellow'] for color in colors: print color
colors = ['red', 'green', 'blue', 'yellow'] for i in range(len(colors)-1, -1, -1): print colors[i]
colors = ['red', 'green', 'blue', 'yellow'] for color in reversed(colors): print color
colors = ['red', 'green', 'blue', 'yellow'] for i in range(len(colors)): print i, '-->', colors[i]
colors = ['red', 'green', 'blue', 'yellow'] for i, color in enumerate(colors): print i, '-->', color
names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue', 'yellow'] n = min(len(names), len(colors)) for i in range(n): print names[i], '-->', colors[i]
zip
uses more memory than the first option. names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue', 'yellow'] for name, color in zip(names, colors): print name, '-->', color
zip
, izip
uses caching, which helps to significantly save memory. names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue', 'yellow'] for name, color in izip(names, colors): print name, '-->', color
izip
inscribed in the kernel and is simply called zip
. colors = ['red', 'green', 'blue', 'yellow'] def compare_length(c1, c2): if len(c1) < len(c2): return -1 if len(c1) > len(c2): return 1 return 0 print sorted(colors, cmp=compare_length)
colors = ['red', 'green', 'blue', 'yellow'] print sorted(colors, key=len)
cmp
method has been removed from the Python 3.x core. for k in d: print k
R
): for k in d.keys(): if k.startswith('R'): del d[k]
d.keys()
makes a copy of the dictionary keys, which allows us to work freely with the original structure. for k in d: print k, '-->', d[k]
for k, v in d.items(): print k, '-->', v
for k, v in d.iteritems(): print k, '-->', v
names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue'] d = dict(izip(names, colors)) # d : # {'matthew': 'blue', 'rachel': 'green', 'raymond': 'red'}
colors = ['red', 'green', 'red', 'blue', 'green', 'red'] d = {} for color in colors: if color not in d: d[color] = 0 d[color] += 1 #{'blue': 1, 'green': 2, 'red': 3}
get()
function: colors = ['red', 'green', 'red', 'blue', 'green', 'red'] d = {} for color in colors: d[color] = d.get(color, 0) + 1
defaultdict()
. But you should know how it works . d = defaultdict(int) for color in colors: d[color] += 1
names = ['raymond', 'rachel', 'matthew', 'roger', 'betty', 'melissa', 'judith', 'charlie'] d = {} for name in names: key = len(name) if key not in d: d[key] = [] d[key].append(name) {5: ['roger', 'betty'], 6: ['rachel', 'judith'], 7: ['raymond', 'matthew', 'melissa', 'charlie']}
d = defaultdict(list) for name in names: key = len(name) d[key].append(name)
Source: https://habr.com/ru/post/204476/
All Articles