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 8041179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:07:49+00:00 2026-06-05T04:07:49+00:00

Python beginner here. I am iterating through a text file, column by column. for

  • 0

Python beginner here.
I am iterating through a text file, column by column.

for line in Input:
    line = line.rstrip() 
    fields = line.split("\t")   
    for col in fields[5:]:

How would I keep track of which column I am in as I iterate through the columns? I want to print the current relevant column in the output so when I have my results I know which column the results refer to.
It seems like something for which there is an obvious answer, but I don’t know it!

Thanks in advance for the help. I find the helpfulness of people here overwhelming.

some input/output examples:

lines from input file :

 chr20   1032    .       A     1/1 1/1 1/1 0/1 0/0
 chr20   1326    .       T     1/1 1/1 1/1 0/1 0/0
 chr20   1388    .       C     0/0 1/1 ./. 0/1 0/0

Then I go through these lines looking at column 5 eg 1/1, 1/1, 0/0 and calculating some output statistics. My output file contains the statistics for every x number of rows calculated using column 5.
I’d like my output file to also include the results from column 6, 7 etc and for the rows in the output file to tell me which column the data was calculated from. In essence to save me rerunning the script seperately for each column.

output example:

5   chr1    230344070   231345012   1000942 200720  66560   119841  7160    5.63775088385   94.3622491161
5   chr1    231345012   232345029   1000017 167920  55040   106160  3360    3.06793279766   96.9320672023
5   chr1    232345029   233345195   1000166 179280  64841   96079   9180    8.72134449311   91.2786555069

With the first column here referring the fact it was calculated based on the values in column 5

for index, col in enumerate(fields[5:]):

Best,

Rubal

  • 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-05T04:07:50+00:00Added an answer on June 5, 2026 at 4:07 am

    You could try something like this

    for i,col in enumerate(fields[5:], 5):
        ....
    

    enumerate() will generate an index value for you, by default it starts with 0 unless a starting value is specified as 2nd parameter to enumerate() as shown above with 5.

    Variable i will start with the value 5 and allow you to track the current column you are working on and col (as before) the value of the field in that column.

    Alternatively, just for convenience and easier modification, you could use a variable:

    start_col = 5
    for i,col in enumerate(fields[start_col:], start_col):
        ....
    

    — UPDATE in reply to comments below:

    I am still not quite sure I understand your comment, but if the loop you posted is inside a bigger loop you could to keep track of your current columns like this:

    cur_column = 5
    for line in Input:
        line = line.rstrip() 
        fields = line.split("\t")   
        for col in fields[cur_colum:]:
           ...
           ...
    
    cur_column += 1 # done processing current column, increment value to next column
    

    Posting some simple input/output examples would help if your code is too big to post. Hard to really know how to help without more information. I hope this is helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right I am a complete beginner here, trying to work through various python tutorials
It's a Python beginner question. I want to loop through several subfolders of parent
I'm a beginner with both Python and RegEx, and I would like to know
I came here to ask a simple, very beginner question regarding python. I just
Python beginner here. I am using the matplotlib library to make graphs from tab
Python beginner here. I've looked around and found similar questions but can't quite cobble
I'm a python beginner, and I'm curious how can I send a dictionary through
Beginner programmer here. So bear with me. I have a simple python program. print
Python beginner here, so I apologize if this question has a simple answer. (I
beginner to python here. I have 2 nested lists that I want to merge:

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.