• Как сделать цикл по кортежам?

    kshnkvn
    @kshnkvn
    yay ✌️ t.me/kshnkvn
    a = (10, 20, 30)
    b = [(5, 10, 15), (11, 12, 14)]
    c = list()
    
    for i, v in enumerate(b):
        c.insert(i, [])
        for x, y in zip(a, v):
            c[i].append(x * y)
    
    print(c)

    outp: [[50, 200, 450], [110, 240, 420]]
    Оно?
    Ответ написан
    6 комментариев
  • Как сделать цикл по кортежам?

    shabelski89
    @shabelski89
    engineer
    Я думал речь про операции внутри запроса.
    Вот пример на SQLite набросал:
    root@a:~# sqlite3
    SQLite version 3.11.0 2016-02-15 17:29:24
    Enter ".help" for usage hints.
    Connected to a transient in-memory database.
    Use ".open FILENAME" to reopen on a persistent database.
    sqlite> CREATE TABLE table1(id INT PRIMARY KEY AUTOINCREMENT, fld1 INTEGER , fld2 INTEGER, fld3 INTEGER);
    Error: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
    sqlite> CREATE TABLE table1(id INTEGER PRIMARY KEY AUTOINCREMENT, fld1 INTEGER , fld2 INTEGER, fld3 INTEGER);
    sqlite> CREATE TABLE table2(id INTEGER PRIMARY KEY AUTOINCREMENT, fld1 INTEGER , fld2 INTEGER, fld3 INTEGER);
    sqlite> INSERT INTO table1(fld1, fld2, fld3) VALUES(15, 10, 5);
    sqlite> INSERT INTO table1(fld1, fld2, fld3) VALUES(3, 6, 9);
    sqlite> INSERT INTO table2(fld1, fld2, fld3) VALUES(5, 10, 15);
    sqlite> INSERT INTO table2(fld1, fld2, fld3) VALUES(4, 8, 12);
    sqlite> SELECT * FROM table1;
    1|15|10|5
    2|3|6|9
    sqlite> SELECT * FROM table2;
    1|5|10|15
    2|4|8|12
    sqlite> SELECT (table1.fld1 * table2.fld1)
       ...> FROM table1
       ...> INNER JOIN TABLE2 ON table1.id=table2.id;
    75
    12
    sqlite> SELECT (table1.fld2 * table2.fld2)
       ...> FROM table1
       ...> INNER JOIN TABLE2 ON table1.id=table2.id;
    100
    48
    sqlite> SELECT (table1.fld1 * table2.fld1), (table1.fld2 * table2.fld2), (table1.fld3 * table2.fld3)
       ...> FROM table1
       ...> INNER JOIN TABLE2 ON table1.id=table2.id;
    75|100|75
    12|48|108
    sqlite>
    Ответ написан
    1 комментарий