【问题标题】:How to use python join the arrays and specify from which array?如何使用python加入数组并指定从哪个数组?
【发布时间】:2018-12-28 00:41:55
【问题描述】:

我有 2 个数组:

Array_1=["apple","orange"]
Array_2=["kiwi"]

我希望得到如下输出:

"apple" 1
"orange" 1
"kiwi" 2

这个输出会打印在txt文件中,1表示来自Array_1,2表示来自Array_2。

谁能分享我的想法?

【问题讨论】:

  • 这些是 Python 列表,而不是数组。

标签: arrays python-3.x


【解决方案1】:
with open('foo.txt', 'w') as f:
    for i, array in enumerate([Array_1, Array_2]):
        for s in array:
            print(f'"{s}"', i, file=f)

'foo.txt' 是文本文件的路径。

【讨论】:

  • 嗨,我得到的输出是单引号,例如 'apple' 1 任何方式使它成为双引号,例如 "apple" 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 2013-04-29
  • 2016-10-23
  • 1970-01-01
相关资源
最近更新 更多