[英]How do i loop through each entry in a string delimited by a comma and include it as a separate input using Python
I have a string called listnumber我有一个名为 listnumber 的字符串
listnumbers
'1.0,2.0,3.0,4.0,5.0,6.0'
I have a function that returns each value of that string我有一个返回该字符串的每个值的 function
def myfun(lists):
return ','.join([i for i in lists.split(',')])
When i type the function当我输入 function
myfun(listnumbers)
'1.0,2.0,3.0,4.0,5.0,6.0'
I have a loop script我有一个循环脚本
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("OMAP_PCT_POP_ACS17"):
if lyr.supports("DEFINITIONQUERY"):
lyr.definitionQuery="Value=" ""+myfun(listnumbers)+""
I end up getting我最终得到
Value=1.0,2.0,3.0,4.0,5.0,6.0
What i would really like is for this to loop and give me我真正想要的是让这个循环并给我
Value=1.0
Value=2.0
Value=3.0
and so on...... As separate entries.等等......作为单独的条目。 I feel i am very close i just need to make some changes.
我觉得我很接近我只需要做一些改变。
Instead of joining the string back together, just leave it apart:与其将字符串重新连接在一起,不如将其分开:
def myfun(lists):
return [i for i in lists.split(',')]
Then, in your loop, you should loop through the values of the list returned by myfun
:然后,在您的循环中,您应该遍历
myfun
返回的列表的值:
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("OMAP_PCT_POP_ACS17"):
if lyr.supports("DEFINITIONQUERY"):
for value in myfun(listnumbers):
lyr.definitionQuery = "Value=" + value
However, str.split
already does what this improved myfun
does, since it already returns a list.但是,
str.split
已经完成了改进后的myfun
所做的事情,因为它已经返回了一个列表。 Thus, you can simplify even further and get rid of myfun
entirely:因此,您可以进一步简化并完全摆脱
myfun
:
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
for lyr in m.listLayers("OMAP_PCT_POP_ACS17"):
if lyr.supports("DEFINITIONQUERY"):
for value in listnumbers.split(','):
lyr.definitionQuery = "Value=" + value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.