简体   繁体   English

我如何遍历以逗号分隔的字符串中的每个条目,并使用 Python 将其作为单独的输入包含在内

[英]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.

相关问题 我如何用逗号分隔格式化字符串 - How do i format string with comma delimited 如何使用python将UTF-8文件分成单独的行(以逗号分隔)? - How to separate a UTF-8 file into separate rows (comma delimited) using python? 如何将以空格分隔的字符串分隔为多个变量? - How do I separate a space-delimited string into multiple variables? 如何通过 Python 遍历多个分隔的 JSON 文件? - How do I loop through a multiple delimited JSON file over Python? 如何遍历列表中的每个字符串? - How do I loop through each string in my list? 我如何构造一个 while 循环以在打印语句中不包含空字符串或退出条目? - How do I structure a while-loop to not include an empty-string or quit entry in a print statement? 如何使用for循环和带用户输入的if语句在python字典中的键/值之间循环? - How do I cycle through keys/values in a python dictionary using a for loop and if statement with user input? 在Python中,如何在通过循环创建的python列表中获取条目的值 - In Python, how do I grab the value of an entry in a python list created through a loop 使用Python 3.4将标头添加到以逗号分隔的字符串中 - Adding headers to comma delimited string using Python 3.4 Python:将带分隔符的字符串的输入文件读入嵌套字典并循环浏览 - Python: Read the input file of delimited strings into a nested dictionary and loop through it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM