python - 从数组索引中获取多个字符串 - PYTHON
问题描述
我只是想不出如何在 python 中轻松地做到这一点:
myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]
现在我希望输出是
print(myArray[0:3]) -> "this is a test"
print(myArray[4:7]) -> "this is another test"
python中是否有一个功能允许这样做而无需在myArray中的for word中迭代整个数组...
我得到的是一个循环中的索引,它只告诉我应该“打印”哪个单词。
我更喜欢python“独家”变体,它简短而简单,最好的情况是一个衬里,它应该需要尽可能少的内存(即使是数千次尝试也很快)
解决方案
您可以尝试 join(),我希望这是您正在寻找的解决方案
myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]
print(' '.join(myArray[:4]))
print(' '.join(myArray[4:]))
推荐阅读
- flutter - Flutter:取消后端中间件中的流订阅
- scala - 即使在 intellij 中转储 maven 依赖项后,import scalatest 仍显示未解决
- javascript - 将数据传递给 Javascript,未捕获的 TypeError
- vb.net - VB.NET 通过 TCPClient 接收进程
- r - 更改 R Shiny DT 内的数据标签
- python - 如何根据天间隔对时间戳和计数项目进行分组?
- cassandra - Cassandra-2.2.3:多次修复后,反复面临“写入大分区错误”
- provisioning - Thingsboard 设备配置:如何通过 API 在指定设备组中创建新设备?
- apache-kafka - 使用哪个消息系统?
- apache-kafka - 学习使用哪个 Kafka API 以将传统集成系统转换为 Apache Kafka