[英]How to print or join specific elements of a list (Python)?
list = [8,4,3,2,5]
Is there a way to join two elements not using slice 有没有办法结合两个不使用切片的元素
print ' '.join(list[0:3]) #would print out the numbers in between
What if I want to print just those specific elements? 如果我只想打印那些特定元素怎么办?
You can use Python's string formatter : 您可以使用Python的字符串格式化程序 :
>>> arr = [8,4,3,2,5]
>>> print "{0} {3}".format(*arr)
8 2
Also, please don't name your variables after built-in functions/types such as list
. 另外,请不要使用内置函数/类型(例如list
命名变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.