如何將兩列連接成一個唯一的文本

如何將兩列連接成一個唯一的文本

所以我有以下內容

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

任何基本的嵌套集合為了任何程式設計/腳本語言中的循環都可能產生您想要的結果。例如,在Python:

#!/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())

前任。產品.txt:

1
2
3

前任。公司.txt:

ABC Ltd
EFG Ltd
XYZ Ltd

相關內容