Skip to content

Instantly share code, notes, and snippets.

@aaronlelevier
Created July 4, 2019 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronlelevier/61051608160d530f0b375e43fff66102 to your computer and use it in GitHub Desktop.
Save aaronlelevier/61051608160d530f0b375e43fff66102 to your computer and use it in GitHub Desktop.
Python Codejam
Python 2.7.16 (v2.7.16:413a49145e, Mar 2 2019, 14:32:10)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> # let's do this the same way. It has to be command line. If it were in Jupyter notebook, then that just wouldn't be fair. Right?!
...
>>> d = {'foo': 1, 'bar': 2}
>>> # what are they keys
...
>>> d.keys()
['foo', 'bar']
>>> # what are the values
...
>>> d.values()
[1, 2]
>>> # single line split
...
>>> keys, values = zip(d)
>>> keys
('foo',)
>>> values
('bar',)
>>> keys, values = zip(d.items())
>>> keys
(('foo', 1),)
>>> values
(('bar', 2),)
>>> zip(d.items())
[(('foo', 1),), (('bar', 2),)]
>>> zip(*d.items())
[('foo', 'bar'), (1, 2)]
>>> a, b = zip(*d.items())
>>> a
('foo', 'bar')
>>> b
(1, 2)
>>> d
{'foo': 1, 'bar': 2}
>>> d.items()
[('foo', 1), ('bar', 2)]
>>> c,d = d.items()
>>> c
('foo', 1)
>>> d
('bar', 2)
>>> d.keys()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'keys'
>>> d
('bar', 2)
>>> d = {'foo': 1, 'bar': 2}
>>> d
{'foo': 1, 'bar': 2}
>>> d.values()
[1, 2]
>>> zip(d.keys(), d.values())
[('foo', 1), ('bar', 2)]
>>> a, b = zip(d.keys(), d.values())
>>> a
('foo', 1)
>>> b
('bar', 2)
>>> {chr(x}:x for x in range(100, 100)}
File "<stdin>", line 1
{chr(x}:x for x in range(100, 100)}
^
SyntaxError: invalid syntax
>>> {chr(x):x for x in range(100, 100)}
{}
>>> {chr(x):x for x in range(100, 110)}
{'e': 101, 'd': 100, 'g': 103, 'f': 102, 'i': 105, 'h': 104, 'k': 107, 'j': 106, 'm': 109, 'l': 108}
>>> chars = {chr(x):x for x in range(100, 110)}
>>> next(iter(chars))
'e'
>>> chars.keys()
['e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l']
>>> chars.values()
[101, 100, 103, 102, 105, 104, 107, 106, 109, 108]
>>> chags.items()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'chags' is not defined
>>> chars.items()
[('e', 101), ('d', 100), ('g', 103), ('f', 102), ('i', 105), ('h', 104), ('k', 107), ('j', 106), ('m', 109), ('l', 108)]
>>> sup(chars.values())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sup' is not defined
>>> sum(chars.values())
1045
>>> values = chars.values()
>>> min(values)
100
>>> values[:2]
[101, 100]
>>> values
[101, 100, 103, 102, 105, 104, 107, 106, 109, 108]
>>> sorted(values) == values
False
>>> values[-3:]
[106, 109, 108]
>>> values[:-3]
[101, 100, 103, 102, 105, 104, 107]
>>> # that was the last three, or up to the last three
...
>>> # that was the last three, or up to the last three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment