来自多个列表的多个字符串

发布于 2025-02-05 21:17:49 字数 370 浏览 2 评论 0原文

我有以下内容:

import pandas as pd

Roads =['Motorways','Streets','Avenues']

Countries =  ['England','Scotland','Wales']

Visuals = ['Graph','Table']


TabNames = [pd.Series(Roads)+ pd.Series(Countries) + pd.Series(Visuals).tolist()]

print(TabNames) 

我将如何打印输出,以便像“高速公路英格兰图”,苏格兰高速公路图,“高速公路威尔士图”,“街道英格兰图”等等。

然后,如何复制输出或将其吐出到.txt文件?

I have the following:

import pandas as pd

Roads =['Motorways','Streets','Avenues']

Countries =  ['England','Scotland','Wales']

Visuals = ['Graph','Table']


TabNames = [pd.Series(Roads)+ pd.Series(Countries) + pd.Series(Visuals).tolist()]

print(TabNames) 

How would I print the output so that it would be like 'Motorways England Graph', Motorways Scotland Graph', 'Motorways Wales Graph', 'Streets England Graph' so on and so forth.

How do I then copy the output or have it spit it out to a .txt file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2025-02-12 21:17:49

您可以尝试此操作而无需导入任何库:

Roads = ['Motorways', 'Streets', 'Avenues']
Countries = ['England', 'Scotland', 'Wales']
Visuals = ['Graph', 'Table']

with open('result.txt', 'w') as write_file:
    for road in Roads:
        for country in Countries:
            for visual in Visuals:
                sentence = road + ' ' + country + ' ' + visual
                print(sentence)
                write_file.write(sentence)
                write_file.write('\n')
    
write_file.close()

输出:

Motorways England Graph
Motorways England Table
Motorways Scotland Graph
Motorways Scotland Table
Motorways Wales Graph
Motorways Wales Table
Streets England Graph
Streets England Table
Streets Scotland Graph
Streets Scotland Table
Streets Wales Graph
Streets Wales Table
Avenues England Graph
Avenues England Table
Avenues Scotland Graph
Avenues Scotland Table
Avenues Wales Graph
Avenues Wales Table

You could try this without the need to import any library:

Roads = ['Motorways', 'Streets', 'Avenues']
Countries = ['England', 'Scotland', 'Wales']
Visuals = ['Graph', 'Table']

with open('result.txt', 'w') as write_file:
    for road in Roads:
        for country in Countries:
            for visual in Visuals:
                sentence = road + ' ' + country + ' ' + visual
                print(sentence)
                write_file.write(sentence)
                write_file.write('\n')
    
write_file.close()

Output:

Motorways England Graph
Motorways England Table
Motorways Scotland Graph
Motorways Scotland Table
Motorways Wales Graph
Motorways Wales Table
Streets England Graph
Streets England Table
Streets Scotland Graph
Streets Scotland Table
Streets Wales Graph
Streets Wales Table
Avenues England Graph
Avenues England Table
Avenues Scotland Graph
Avenues Scotland Table
Avenues Wales Graph
Avenues Wales Table
也只是曾经 2025-02-12 21:17:49

尝试以下操作:

index = pd.MultiIndex.from_product([Roads, Countries, Visuals], names = ["Roads", "Countries", "Visuals"])
pd.DataFrame(index = index).reset_index()

Try this:

index = pd.MultiIndex.from_product([Roads, Countries, Visuals], names = ["Roads", "Countries", "Visuals"])
pd.DataFrame(index = index).reset_index()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文