Python: затем () функция

SELECT Col.Column_Name from 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, 
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col 
WHERE 
    Col.Constraint_Name = Tab.Constraint_Name
    AND Col.Table_Name = Tab.Table_Name
    AND Constraint_Type = 'PRIMARY KEY'
    AND Col.Table_Name = '<your table name>'
42
задан 3zzy 14 November 2009 в 02:17
поделиться

2 ответа

The expression (sum(row) for row in M) creates what's called a generator. This generator will evaluate the expression (sum(row)) once for each row in M. However, the generator doesn't do anything yet, we've just set it up.

The statement next(G) actually runs the generator on M. So, if you run next(G) once, you'll get the sum of the first row. If you run it again, you'll get the sum of the second row, and so on.

>>> M = [[1,2,3],
...      [4,5,6],
...      [7,8,9]]
>>> 
>>> G = (sum(row) for row in M) # create a generator of row sums
>>> next(G) # Run the iteration protocol
6
>>> next(G)
15
>>> next(G)
24

See also:

72
ответ дан 26 November 2019 в 23:41
поделиться

If you've come that far, then you should already know how a common for-in statement works.

The following statement:

for row in M: print row

would see M as a sequence of 3 rows (sub sequences) consisting of 3 items each, and iterate through M, outputting each row on the matrix:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

You knew that, well...

You can see Generators just as some syntactic sugar around for-in loops. Forget about the sum() call, and type something like this on IDLE:

G = (row for row in M)
print G
for a in G: print a

You see, the Generator cannot be directly represented as text, not just as a sequence can be. But, you can iterate through a Generator as if it were a sequence.

You'll find some big differences then, but the basics are that you can use a generator not to return just the value of each item in the sequence, but the result of any expression. In the tutorial's example, the expression is sum(row).

Try the following and see what happens:

G = ("("+str(row[2])+";"+str(row[1])+";"+str(row[0])+")" for row in M)
G.next()
G.next()
G.next()
10
ответ дан 26 November 2019 в 23:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: