如何将两列连接成一个唯一的文本

如何将两列连接成一个唯一的文本

所以我有以下内容

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())

例如 products.txt

1
2
3

例如 company.txt

ABC Ltd
EFG Ltd
XYZ Ltd

相关内容