首页 > 解决方案 > 从数组索引中获取多个字符串 - PYTHON

问题描述

我只是想不出如何在 python 中轻松地做到这一点:

myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]

现在我希望输出是

  1. print(myArray[0:3]) -> "this is a test"
  2. print(myArray[4:7]) -> "this is another test"

python中是否有一个功能允许这样做而无需在myArray中的for word中迭代整个数组...

我得到的是一个循环中的索引,它只告诉我应该“打印”哪个单词。

我更喜欢python“独家”变体,它简短而简单,最好的情况是一个衬里,它应该需要尽可能少的内存(即使是数千次尝试也很快)

标签: pythonarrayspython-3.x

解决方案


您可以尝试 join(),我希望这是您正在寻找的解决方案

myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]
print(' '.join(myArray[:4]))
print(' '.join(myArray[4:]))

推荐阅读