From 6108a6017d0f25d2749e97b0fe87a2e6de1b5ad4 Mon Sep 17 00:00:00 2001 From: reachtarunhere Date: Sun, 17 Apr 2016 13:55:01 +0530 Subject: [PATCH] Removed unnecessary list coercions around map --- agents.py | 2 +- csp.py | 4 ++-- learning.py | 4 ++-- logic.py | 6 ++---- search.py | 2 +- utils.py | 6 +++--- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/agents.py b/agents.py index 6de58b830..274630d91 100644 --- a/agents.py +++ b/agents.py @@ -848,7 +848,7 @@ def score(env): env.add_thing(agent) env.run(steps) return agent.performance - return mean(list(map(score, envs))) + return mean(map(score, envs)) # _________________________________________________________________________ diff --git a/csp.py b/csp.py index 421115484..d696a787c 100644 --- a/csp.py +++ b/csp.py @@ -510,7 +510,7 @@ def flatten(seqs): return sum(seqs, []) _CELL = itertools.count().__next__ _BGRID = [[[[_CELL() for x in _R3] for y in _R3] for bx in _R3] for by in _R3] _BOXES = flatten([list(map(flatten, brow)) for brow in _BGRID]) -_ROWS = flatten([list(map(flatten, list(zip(*brow)))) for brow in _BGRID]) +_ROWS = flatten([list(map(flatten, zip(*brow))) for brow in _BGRID]) _COLS = list(zip(*_ROWS)) _NEIGHBORS = {v: set() for v in flatten(_ROWS)} @@ -583,7 +583,7 @@ def abut(lines1, lines2): return list( map(' | '.join, list(zip(lines1, lines2)))) print('\n------+-------+------\n'.join( '\n'.join(reduce( - abut, list(map(show_box, brow)))) for brow in self.bgrid)) + abut, map(show_box, brow))) for brow in self.bgrid)) # ______________________________________________________________________________ # The Zebra Puzzle diff --git a/learning.py b/learning.py index 8ff115a1f..ca953ae0a 100644 --- a/learning.py +++ b/learning.py @@ -101,14 +101,14 @@ def setproblem(self, target, inputs=None, exclude=()): to not use in inputs. Attributes can be -n .. n, or an attrname. Also computes the list of possible values, if that wasn't done yet.""" self.target = self.attrnum(target) - exclude = list(map(self.attrnum, exclude)) + exclude = map(self.attrnum, exclude) if inputs: self.inputs = removeall(self.target, inputs) else: self.inputs = [a for a in self.attrs if a != self.target and a not in exclude] if not self.values: - self.values = list(map(unique, list(zip(*self.examples)))) + self.values = list(map(unique, zip(*self.examples))) self.check_me() def check_me(self): diff --git a/logic.py b/logic.py index c64b64431..a4346a5ed 100644 --- a/logic.py +++ b/logic.py @@ -903,7 +903,7 @@ def fetch_rules_for_goal(self, goal): test_kb = FolKB( - list(map(expr, ['Farmer(Mac)', + map(expr, ['Farmer(Mac)', 'Rabbit(Pete)', 'Mother(MrsMac, Mac)', 'Mother(MrsRabbit, Pete)', @@ -916,10 +916,9 @@ def fetch_rules_for_goal(self, goal): # '(Human(h) & Mother(m, h)) ==> Human(m)' '(Mother(m, h) & Human(h)) ==> Human(m)' ])) -) crime_kb = FolKB( - list(map(expr, + map(expr, ['(American(x) & Weapon(y) & Sells(x, y, z) & Hostile(z)) ==> Criminal(x)', # noqa 'Owns(Nono, M1)', 'Missile(M1)', @@ -929,7 +928,6 @@ def fetch_rules_for_goal(self, goal): 'American(West)', 'Enemy(Nono, America)' ])) -) def fol_bc_ask(KB, query): diff --git a/search.py b/search.py index 5253fca9a..ddb62ef3d 100644 --- a/search.py +++ b/search.py @@ -584,7 +584,7 @@ def genetic_algorithm(population, fitness_fn, ngen=1000, pmut=0.1): for i in range(ngen): new_population = [] for i in len(population): - fitnesses = list(map(fitness_fn, population)) + fitnesses = map(fitness_fn, population) p1, p2 = weighted_sample_with_replacement(population, fitnesses, 2) child = p1.mate(p2) if random.uniform(0, 1) < pmut: diff --git a/utils.py b/utils.py index 09da13c61..81b01748a 100644 --- a/utils.py +++ b/utils.py @@ -86,7 +86,7 @@ def histogram(values, mode=0, bin_function=None): Sorted by increasing value, or if mode=1, by decreasing count. If bin_function is given, map it over values first.""" if bin_function: - values = list(map(bin_function, values)) + values = map(bin_function, values) bins = {} for val in values: @@ -299,8 +299,8 @@ def print_table(table, header=None, sep=' ', numfmt='%g'): for row in table] sizes = list( - map(lambda seq: max(list(map(len, seq))), - list(zip(*[list(map(str, row)) for row in table])))) + map(lambda seq: max(map(len, seq)), + list(zip(*[map(str, row) for row in table])))) for row in table: print(sep.join(getattr(