Enum within an enum
I believe that in Java, you can simply nest enums, as long as your non-enum constants come first. enum Action { FOO, BAR; enum MOVE { UP, DOWN, LEFT, RIGHT } } This compiles for me and gives me the behavior you were looking for.
I believe that in Java, you can simply nest enums, as long as your non-enum constants come first. enum Action { FOO, BAR; enum MOVE { UP, DOWN, LEFT, RIGHT } } This compiles for me and gives me the behavior you were looking for.
This groups each 3 elements in the order they appear: new_list = [data_list[i:i+3] for i in range(0, len(data_list), 3)] Give us a better example if it is not what you want.
Intro: Nested classes relate to other classes a little differently than outer classes. Taking Java as an example: Non-static nested classes have access to other members of the enclosing class, even if they are declared private. Also, non-static nested classes require an instance of the parent class to be instantiated. OuterClass outerObj = new OuterClass(arguments); … Read more
>>> d[0] = template >>> d[1] = template These two statements made both d[0] and d[1] refer to the same object, template. Now you can access the dictionary with three names, template, d[0] and d[1]. So doing: d[0][‘mean’] = 1 modifies a dictionary object, which can be referred with the other names mentioned above. To … Read more
Here is how you would do this with a nested list comprehension: [[float(y) for y in x] for x in l] This would give you a list of lists, similar to what you started with except with floats instead of strings. If you want one flat list, then you would use [float(y) for x in … Read more
You need to alias the subquery. SELECT name FROM (SELECT name FROM agentinformation) a or to be more explicit SELECT a.name FROM (SELECT name FROM agentinformation) a
You could use an infinite, nested defaultdict: >>> from collections import defaultdict >>> infinitedict = lambda: defaultdict(infinitedict) >>> d = infinitedict() >>> d[‘key1’][‘key2’][‘key3’][‘key4’][‘key5’] = ‘test’ >>> d[‘key1’][‘key2’][‘key3’][‘key4’][‘key5’] ‘test’ Given your dotted string, here’s what you can do: >>> import operator >>> keys = “a.b.c”.split(“.”) >>> lastplace = reduce(operator.getitem, keys[:-1], d) >>> lastplace.has_key(keys[-1]) False You can … Read more
A very common pattern to do this is to use an empty dict as your default: d.get(‘foo’, {}).get(‘bar’) If you have more than a couple of keys, you could use reduce (note that in Python 3 reduce must be imported: from functools import reduce) to apply the operation multiple times reduce(lambda c, k: c.get(k, {}), … Read more
This handles both of your cases, and I think will solve the general case, without any for loops: def flatten(S): if S == []: return S if isinstance(S[0], list): return flatten(S[0]) + flatten(S[1:]) return S[:1] + flatten(S[1:])
Here is how you would do this with a nested list comprehension: [[float(y) for y in x] for x in l] This would give you a list of lists, similar to what you started with except with floats instead of strings. If you want one flat list then you would use [float(y) for x in … Read more