Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:57:48+00:00 2026-06-14T13:57:48+00:00

I am having a problem with my nested loops. I am getting an output,

  • 0

I am having a problem with my nested loops. I am getting an output, but it is duplicating each output for the number of fields in my data set.

for dat in data_body:
    x = float(dat[5])
    y = float(dat[6])
    point = ogr.Geometry(ogr.wkbPoint)
    point.AddPoint(x,y)
    feature.SetGeometry(point)
    for i, d in enumerate(dat):
        for j, field in enumerate(new_fields):
            if i == j:
                feature.SetField(field, d)
                layer.CreateFeature(feature)

For my datasets, the list of lists data_body has 13 rows of data and 55 columns (i.e. 55 positions). For my list new_fields I have a 55 values, which correspond to the columns of data_body list of lists. Thus, in my code block for the object feature.SetField(field, d). I should have each value of the data_body correspond to each unique field, as long as rows match each other, i.e. i and j. However, rather getting an object back with 13 rows of data corresponding with column values corresponding to the values in new_fields. I get 13 * 55 values, i.e. 715 rows, where values are missing for ~ 50% of the data. My output data table kind of looks like a triangle of data.

I am not sure if my question makes sense, but if it does, any help with my nested looping strategies would be helpful. i think my issue is my conceptualization of what my loop is actually doing, as well as what my if statement might be doing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:57:50+00:00Added an answer on June 14, 2026 at 1:57 pm

    As avasal said, you want to iterate over dat and new_fields at the same time, and the easiest way to do this is to use zip:

    for d, field in zip(dat, new_fields):
        feature.SetField(field, d)
        layer.CreateFeature(feature)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problem with nested loop. I have multiple number of posts, and each
I'm having a problem with some for nested loops that I have to convert
I am new to programming and having some problem figuring out nested loops. I
I'm having trouble getting my head around a problem on recursive nested lists. The
I'm having a problem with nested bindings with Knockout.JS For example if I have
I'm having a problem with nested model forms that contain radio buttons, when I
I'm having a problem with my nested form. Models: class UserAnswer < ActiveRecord::Base belongs_to
I'm having a problem with Rails 3.2.1, a nested resource keeps complaining about an
I'm having a problem using nested partials with dynamic form builder code (from the
I'm having a problem with forward declarations in a nested namespace. I put a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.