2つの列を一意のテキストに連結する方法

2つの列を一意のテキストに連結する方法

だから私は次のことをする

Product     Customer
1           ABC Ltd
2           EFG Ltd
3           XYZ Ltd

すべての組み合わせを含むリストを作成したいのですが、

Product     Customer
1           ABC Ltd
1           EFG Ltd
1           XYZ Ltd
2           ABC Ltd
2           EFG Ltd

.... など。この例では 9 つのエントリが残ります。また、追加または変更された場合に更新されるように動的にする必要があります。

ありがとう

答え1

ネストされた基本セットのためにどのようなプログラミング言語やスクリプト言語でも、ループを使えば望み通りの結果が得られる可能性があります。例えば、パイソン:

#!/path/to/Python3/python

# lists of strings to concatenate
products = ['1', '2', '3']
companies = ['ABC Ltd', 'EFG Ltd', 'XYZ Ltd']

# Nested 'for' loops
for product in products:
    for company in companies:
        print (product, '\t', company)

次の出力が生成されます。

1    ABC Ltd
1    EFG Ltd
1    XYZ Ltd
2    ABC Ltd
2    EFG Ltd
2    XYZ Ltd
3    ABC Ltd
3    EFG Ltd
3    XYZ Ltd

の値は製品そして企業ループを使用して、更新されたテキスト ファイルなどから動的に読み取ることもできますfor。Python では、次のように記述します。

# Replaces e.g
# products = ['1', '2', '3']
# companies = ['ABC Ltd', 'EFG Ltd', 'XYZ Ltd']

products = []
companies = []

with open ('products.txt', 'r') as products_file:
    for line in products_file:
        products.append(line.strip())

with open ('companies.txt', 'r') as companies_file:
    for line in companies_file:
        companies.append(line.strip())

例: products.txt:

1
2
3

例: companies.txt:

ABC Ltd
EFG Ltd
XYZ Ltd

関連情報