打印出经过 For 循环的项目? python 2.7
Print out the items which went through For loop? python 2.7
我是 python 的新人。我有一个 for loop
里面有 if ...:
条件。
我想打印出经过 for
循环的项目(列表)。
理想情况下,项目应该用空格或逗号分隔。这是一个简单的示例,旨在与 arcpy
一起使用以打印出处理后的 shapefile。
虚拟示例:
for x in range(0,5):
if x < 3:
print "We're on time " + str(x)
我在 if
和 for
循环内外都尝试过但没有成功的尝试:
print "Executed " + str(x)
预计会回来(但不是 list
格式),可能会通过类似 arcpy.GetMessages()
的方式回来?
Executed 0 1 2
将你的x
记录在一个列表中,最后打印出这个列表:
x_list = []
for x in range(0,5):
if x < 3:
x_list.append(x)
print "We're on time " + str(x)
print "Executed " + str(x_list)
如果你使用 Python3 你可以做这样的事情..
print("Executed ", end='')
for x in range(0,5):
if x < 3:
print(str(x), end=' ')
print()
phrase = "We're on time "
# create a list of character digits (look into list comprehensions and generators)
nums = [str(x) for x in range(0, 5) if x < 3]
# " ".join() creates a string with the elements of a given list of strings with space in between
# the + concatenates the two strings
print(phrase + " ".join(nums))
注意。反对票的原因可以帮助我们的新用户了解事情应该如何。
我是 python 的新人。我有一个 for loop
里面有 if ...:
条件。
我想打印出经过 for
循环的项目(列表)。
理想情况下,项目应该用空格或逗号分隔。这是一个简单的示例,旨在与 arcpy
一起使用以打印出处理后的 shapefile。
虚拟示例:
for x in range(0,5):
if x < 3:
print "We're on time " + str(x)
我在 if
和 for
循环内外都尝试过但没有成功的尝试:
print "Executed " + str(x)
预计会回来(但不是 list
格式),可能会通过类似 arcpy.GetMessages()
的方式回来?
Executed 0 1 2
将你的x
记录在一个列表中,最后打印出这个列表:
x_list = []
for x in range(0,5):
if x < 3:
x_list.append(x)
print "We're on time " + str(x)
print "Executed " + str(x_list)
如果你使用 Python3 你可以做这样的事情..
print("Executed ", end='')
for x in range(0,5):
if x < 3:
print(str(x), end=' ')
print()
phrase = "We're on time "
# create a list of character digits (look into list comprehensions and generators)
nums = [str(x) for x in range(0, 5) if x < 3]
# " ".join() creates a string with the elements of a given list of strings with space in between
# the + concatenates the two strings
print(phrase + " ".join(nums))
注意。反对票的原因可以帮助我们的新用户了解事情应该如何。