Skip to content

Commit 7776583

Browse files
committed
Remove unnecessary list coercions.
1 parent 8f58668 commit 7776583

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

csp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def solve_zebra(algorithm=min_conflicts, **args):
658658
ans = algorithm(z, **args)
659659
for h in range(1, 6):
660660
print('House', h, end=' ')
661-
for (var, val) in list(ans.items()):
661+
for (var, val) in ans.items():
662662
if val == h:
663663
print(var, end=' ')
664664
print()

learning.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ def __getitem__(self, item):
209209

210210
def top(self, n):
211211
"Return (count, obs) tuples for the n most frequent observations."
212-
return heapq.nlargest(
213-
n, [(v, k) for (k, v) in list(self.dictionary.items())])
212+
return heapq.nlargest(n, [(v, k) for (k, v) in self.dictionary.items()])
214213

215214
def sample(self):
216215
"Return a random sample from the distribution."
@@ -301,7 +300,7 @@ def add(self, val, subtree):
301300
def display(self, indent=0):
302301
name = self.attrname
303302
print('Test', name)
304-
for (val, subtree) in list(self.branches.items()):
303+
for (val, subtree) in self.branches.items():
305304
print(' ' * 4 * indent, name, '=', val, '==>', end=' ')
306305
subtree.display(indent + 1)
307306

@@ -885,7 +884,7 @@ def RestaurantDataSet(examples=None):
885884
def T(attrname, branches):
886885
branches = dict((value, (child if isinstance(child, DecisionFork)
887886
else DecisionLeaf(child)))
888-
for value, child in list(branches.items()))
887+
for value, child in branches.items())
889888
return DecisionFork(restaurant.attrnum(attrname), attrname, branches)
890889

891890
""" [Figure 18.2]

mdp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def to_arrows(self, policy):
9494
chars = {
9595
(1, 0): '>', (0, 1): '^', (-1, 0): '<', (0, -1): 'v', None: '.'}
9696
return self.to_grid(
97-
dict([(s, chars[a]) for (s, a) in list(policy.items())]))
97+
dict([(s, chars[a]) for (s, a) in policy.items()]))
9898

9999
# ______________________________________________________________________________
100100
""" [Figure 17.1]

nlp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def Rules(**rules):
1414
>>> Rules(A = "B C | D E")
1515
{'A': [['B', 'C'], ['D', 'E']]}
1616
"""
17-
for (lhs, rhs) in list(rules.items()):
17+
for (lhs, rhs) in rules.items():
1818
rules[lhs] = [alt.strip().split() for alt in rhs.split('|')]
1919
return rules
2020

@@ -24,7 +24,7 @@ def Lexicon(**rules):
2424
>>> Lexicon(Art = "the | a | an")
2525
{'Art': ['the', 'a', 'an']}
2626
"""
27-
for (lhs, rhs) in list(rules.items()):
27+
for (lhs, rhs) in rules.items():
2828
rules[lhs] = [word.strip() for word in rhs.split('|')]
2929
return rules
3030

probability.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, varname='?', freqs=None):
4646
self.varname = varname
4747
self.values = []
4848
if freqs:
49-
for (v, p) in list(freqs.items()):
49+
for (v, p) in freqs.items():
5050
self[v] = p
5151
self.normalize()
5252

@@ -237,10 +237,10 @@ def __init__(self, X, parents, cpt):
237237
elif isinstance(cpt, dict):
238238
# one parent, 1-tuple
239239
if cpt and isinstance(list(cpt.keys())[0], bool):
240-
cpt = dict(((v,), p) for v, p in list(cpt.items()))
240+
cpt = dict(((v,), p) for v, p in cpt.items())
241241

242242
assert isinstance(cpt, dict)
243-
for vs, p in list(cpt.items()):
243+
for vs, p in cpt.items():
244244
assert isinstance(vs, tuple) and len(vs) == len(parents)
245245
assert all(isinstance(v, bool) for v in vs)
246246
assert 0 <= p <= 1
@@ -390,7 +390,7 @@ def normalize(self):
390390
"Return my probabilities; must be down to one variable."
391391
assert len(self.variables) == 1
392392
return ProbDist(self.variables[0],
393-
dict((k, v) for ((k,), v) in list(self.cpt.items())))
393+
dict((k, v) for ((k,), v) in self.cpt.items()))
394394

395395
def p(self, e):
396396
"Look up my value tabulated for e."
@@ -454,7 +454,7 @@ def rejection_sampling(X, e, bn, N):
454454
def consistent_with(event, evidence):
455455
"Is event consistent with the given evidence?"
456456
return all(evidence.get(k, v) == v
457-
for k, v in list(event.items()))
457+
for k, v in event.items())
458458

459459
# _________________________________________________________________________
460460

search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def __init__(self, dict=None, directed=True):
639639
def make_undirected(self):
640640
"Make a digraph into an undirected graph by adding symmetric edges."
641641
for a in list(self.dict.keys()):
642-
for (b, distance) in list(self.dict[a].items()):
642+
for (b, distance) in self.dict[a].items():
643643
self.connect1(b, a, distance)
644644

645645
def connect(self, A, B, distance=1):

0 commit comments

Comments
 (0)