1
def cross(A, B):
    "Cross product of elements in A and elements in B."
    return [a+b for a in A for b in B]

digits   = '123456789'
rows     = 'ABCDEFGHI'
cols     = digits
squares  = cross(rows, cols)
unitlist = ([cross(rows, c) for c in cols] +
            [cross(r, cols) for r in rows] +
            [cross(rs, cs) for rs in ('ABC','DEF','GHI') for cs in ('123','456','789')])
units = dict((s, [u for u in unitlist if s in u]) 
             for s in squares)
peers = dict((s, set(sum(units[s],[]))-set([s]))
             for s in squares)

有人可以帮我理解(简单来说)最后两行是什么意思吗?

4

1 回答 1

0
units = dict((s, [u for u in unitlist if s in u]) 
             for s in squares)

是相同的

units = dict()
for s in squares:
    units[s] = [u for u in unitlist if s in u]

甚至类似于以下低效版本

units = dict()
for s in squares:
    units[s] = []
    for u in unitlist:
        if s in u:
            units[s].append(u)
于 2013-03-01T18:33:18.253 回答