Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对涉及列表中某些文件的过程进行循环?_Python_Loops - Fatal编程技术网

Python 对涉及列表中某些文件的过程进行循环?

Python 对涉及列表中某些文件的过程进行循环?,python,loops,Python,Loops,我有以下清单: grouped_shapefile,它为每个文件夹中的文件(pst和dbound对)提供以下目录 [['C:\\Users\\user\\Desktop\\eff\\20194\\DBOUND\\DBOUND.shp', 'C:\\Users\\user\\Desktop\\eff\\20194\\PST\\PST.shp'], ['C:\\Users\\user\\Desktop\\eff\\20042\\DBOUND\\DBOUND.shp', 'C:\\Users

我有以下清单:

grouped_shapefile
,它为每个文件夹中的文件(pst和dbound对)提供以下目录

[['C:\\Users\\user\\Desktop\\eff\\20194\\DBOUND\\DBOUND.shp',
  'C:\\Users\\user\\Desktop\\eff\\20194\\PST\\PST.shp'],
 ['C:\\Users\\user\\Desktop\\eff\\20042\\DBOUND\\DBOUND.shp',
  'C:\\Users\\user\\Desktop\\eff\\20042\\PST\\PST.shp'],
 ['C:\\Users\\user\\Desktop\\eff\\20161\\DBOUND\\DBOUND.shp',
  'C:\\Users\\user\\Desktop\\eff\\20161\\PST\\PST.shp'],
 ['C:\\Users\\user\\Desktop\\eff\\20029\\DBOUND\\DBOUND.shp',
  'C:\\Users\\user\\Desktop\\eff\\20029\\PST\\PST.shp'],
 ['C:\\Users\\user\\Desktop\\eff\\20008\\DBOUND\\DBOUND.shp',
  'C:\\Users\\user\\Desktop\\eff\\20008\\PST\\PST.shp']]
我想创建一个for循环,在列表包含的每个文件夹(
201942004220161
)中的每个
pst
dbound
对的相应文件中执行这段代码

import geopandas as gpd
import pandas
#pst = gpd.read_file(r'C:\Users\user\Desktop\New folder1\PST')#this is not needed in the final because it takes the path by it self
#dbound = gpd.read_file(r'C:\Users\user\Desktop\New folder1\DBOUND')#same here
dbound.reset_index(inplace=True)
wdp = gpd.sjoin(pst, dbound, how="inner", op='within')#each dbound and pst from every folder
wdp['DEC_ID']=wdp['index']

我只想知道如何创建for循环,它将执行应该执行的文件中的代码。我尝试了for循环和使用括号中的位置,但没有达到应有的效果。

不确定我是否正确理解了您的问题,您是否尝试成对遍历列表中的项目?如果是这样的话,那就相当直截了当了:

for i in grouped_shapefiles:
    pst = gpd.read_file(i[0])
    dbound = gpd.read_file(i[1])
    if i[0].split("\\")[-3] == i[1].split("\\")[-3]:
        dbound.reset_index(inplace=True)
        wdp = gpd.sjoin(pst, dbound, how="inner", op='within')
        wdp['DEC_ID'] = wdp['index']
    else:
        print ("Folder pairs mismatch")

根据我的理解进行编辑

我编写的最后一个脚本应该使用您刚刚创建的对应用于for循环中。如果您能让它工作,那就太好了。如上所述进行编辑,如果导出的代码是:
o=r'C:\Users\user\Desktop\yyyyyy h'
wdp.to_文件(o)
这样它就可以将其唯一的文件夹名称附加到每个输出文件标题上,以便用户知道哪个文件夹来自哪个文件夹?示例:wdp\U 20194,etcAdd
folder=i[0]。拆分(“\\”[-3]
o=r'C:\Users\user\Desktop\wdp\u'+folder
非常感谢,非常好。