左连接的一种实现方式

Posted on 2016-09-10 15:04  何处尘归  阅读(268)  评论(4)    收藏  举报

c++:

#include <iostream>
#include <Windows.h>

#define row1 4
#define row2 3
#define column1 3
#define column2 3

int main() {
    int x1[row1][column1 + column2 - 1] = {
        {1, 2, 3, 0, 0},
        {2, 3, 4, 0, 0},
        {3, 4, 5, 0, 0},
        {4, 5, 6, 0, 0}
    };

    int x2[row2][column2] = {
        {1, 5, 6},
        {2, 7, 8},
        {4, 9, 10}
    };

    int i = 0;
    int j = 0;

    while (i < row1)
    {
        if (x1[i][0] > x2[j][0])
        {
            j++;
        }
        else if (x1[i][0] < x2[j][0])
        {
            i++;
        }
        else
        {
            for (int k = 1; k < column2; k++)
            {
                x1[i][column1 + k - 1] = x2[j][k];
            }
            i++;
        }
    }

    for (int i = 0; i < row1; i++)
    {
        for (int j = 0; j < column1 + column2 - 1; j++)
        {
            std::cout << x1[i][j] << ' ';
        }
        std::cout << std::endl;
    }

    system("pause");

    return 1;
}

 

python

row1 = 4
row2 = 3
column1 = 3
column2 = 3

x1 = [
    [1, 2, 3],
    [2, 3, 4],
    [3, 4, 5],
    [4, 5, 6],
]

x2 = [
    [1, 5, 6],
    [2, 7, 8],
    [4, 9, 10],
]

i = 0
j = 0

while (i < row1):
    if (x1[i][0] > x2[j][0]):
        j = j + 1
    elif (x1[i][0] < x2[j][0]):
        for _ in range(1, column2):
            x1[i].append(0)
        i = i + 1
    else:
        for t in range(1, column2):
            x1[i].append(x2[j][t])
        i = i + 1

for i in x1:
    for j in i:
        print(str(j) + ' ', end='')
    print()

 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3