Python For循环在函数外部运行良好,但在函数内部抛出错误

Python For循环在函数外部运行良好,但在函数内部抛出错误,python,function,Python,Function,我有一个for循环,它在函数外部完美地工作,我决定把它放在函数内部,所以我不是每次都输入它,但它总是抛出一个错误。函数的作用是传递一个geojson文件和一个要追加的空列表 对于循环(在功能工作之外): for feature in geojson['features']: if feature['geometry']['type'] == 'Polygon': points.extend(feature['geometry']['coordinates'][0])

我有一个for循环,它在函数外部完美地工作,我决定把它放在函数内部,所以我不是每次都输入它,但它总是抛出一个错误。函数的作用是传递一个geojson文件和一个要追加的空列表

对于循环(在功能工作之外):

for feature in geojson['features']:
    if feature['geometry']['type'] == 'Polygon':
        points.extend(feature['geometry']['coordinates'][0])    
        points.append([None, None])   
    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            points.extend(polyg[0])
            points.append([None, None])
    elif feature['geometry']['type'] == 'LineString': 
        points.extend(feature['geometry']['coordinates'])
        points.append([None, None])
    elif feature['geometry']['type'] == 'MultiLineString': 
        for line in feature['geometry']['coordinates']:
            points.extend(line)
            points.append([None, None])
    else: pass
import json

geojson = json.load(open("towns.geojson"))

points= []

def coordinate_extract(geojson_file, empty_list):
    for feature in geojson_file['features']:
        if feature['geometry']['type'] == 'Polygon':
            empty_list.extend(feature['geometry']['coordinates'][0])    
            empty_list.append([None, None]) # mark the end of a polygon   
        elif feature['geometry']['type'] == 'MultiPolygon':
            for polyg in feature['geometry']['coordinates']:
                empty_list.extend(polyg[0])
                empty_list.append([None, None]) #end of polygon
        elif feature['geometry']['type'] == 'LineString': 
            empty_list.extend(feature['geometry']['coordinates'])
            empty_list.append([None, None])
        elif feature['geometry']['type'] == 'MultiLineString': 
            for line in feature['geometry']['coordinates']:
                empty_list.extend(line)
                empty_list.append([None, None])
        else: pass   
    print(empty_list)

coordinate_extract('geojson', 'points')
功能:

for feature in geojson['features']:
    if feature['geometry']['type'] == 'Polygon':
        points.extend(feature['geometry']['coordinates'][0])    
        points.append([None, None])   
    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            points.extend(polyg[0])
            points.append([None, None])
    elif feature['geometry']['type'] == 'LineString': 
        points.extend(feature['geometry']['coordinates'])
        points.append([None, None])
    elif feature['geometry']['type'] == 'MultiLineString': 
        for line in feature['geometry']['coordinates']:
            points.extend(line)
            points.append([None, None])
    else: pass
import json

geojson = json.load(open("towns.geojson"))

points= []

def coordinate_extract(geojson_file, empty_list):
    for feature in geojson_file['features']:
        if feature['geometry']['type'] == 'Polygon':
            empty_list.extend(feature['geometry']['coordinates'][0])    
            empty_list.append([None, None]) # mark the end of a polygon   
        elif feature['geometry']['type'] == 'MultiPolygon':
            for polyg in feature['geometry']['coordinates']:
                empty_list.extend(polyg[0])
                empty_list.append([None, None]) #end of polygon
        elif feature['geometry']['type'] == 'LineString': 
            empty_list.extend(feature['geometry']['coordinates'])
            empty_list.append([None, None])
        elif feature['geometry']['type'] == 'MultiLineString': 
            for line in feature['geometry']['coordinates']:
                empty_list.extend(line)
                empty_list.append([None, None])
        else: pass   
    print(empty_list)

coordinate_extract('geojson', 'points')
TypeError:字符串索引必须是整数


我不明白为什么在函数内部而不是外部的
第8行出现此错误。有什么建议吗?

在调用函数时,不要对变量使用引号

它将输入视为一个字符串,您需要变量

coordinate_extract(geojson, points)

先生,您肯定需要从基础开始

在调用函数时不要对变量使用引号

coordinate_extract('geojson', 'points')
它将输入视为一个字符串,您需要变量

coordinate_extract(geojson, points)
先生,你一定要从基础做起

coordinate_extract('geojson', 'points')
您错误地传递了一个字符串,而不是包含json数据的实际对象geojson

因此,for循环试图访问字符串中的索引,这是可行的,但字符串只包含数字索引

只需删除引号并将geojson&点数组分别作为对象和列表传递,而不是作为字符串传递

您错误地传递了一个字符串,而不是包含json数据的实际对象geojson

因此,for循环试图访问字符串中的索引,这是可行的,但字符串只包含数字索引


只需删除引号并分别将geojson和points数组作为对象和列表传递,而不是作为字符串传递。

为什么要在此处将字符串作为参数传递:
coordinate\u extract('geojson','points')
coordinate\u extract('geojson','points')
这是使用文本字符串
“geojson”
“points”
作为参数,而不是那些名称的变量。为什么在这里传递字符串作为参数:
坐标提取('geojson','points')
坐标提取('geojson','points')
这是使用文字字符串
“geojson”
“points”)“
作为参数,而不是那些名称的变量。哈哈,是的!我几乎不在编程中使用函数,但我想开始使用更多的函数。谢谢你的简单解决方案。谢谢你。你可以结束这个问题了。祝你有美好的一天。。!哈哈,是的!我几乎不在编程中使用函数,但我想开始使用更多的函数。谢谢你的简单解决方案。谢谢你。你可以结束这个问题了。祝你有美好的一天。。!