Python Forum
Creating multiple lists from file without long for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating multiple lists from file without long for loop
#2
i think the variables(target_id etc) are the reason why the codes look longer. if the codes can auto generate the variables will look shorter IMO

variables = 9
nestedlist = [[]]*variables #you have 9 variables(target_id... etc) so 9 lists in nestedlist generated. 
for line in target_file:
    if line.split()[0] != '#': 
        splitted = line.split()
        for i in range(variables):
            nestedlist[i] = nestedlist[i] + [ splitted[i]]

for index,variable in enumerate(nestedlist):
 print index,variable
the results stored in nestedlist, each index in printed result will correspond to variable (target_id,target_ra_deg) in order.
Reply


Messages In This Thread
RE: Creating multiple lists from file without long for loop - by ka06059 - Jan-28-2018, 08:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decoding lat/long in file name johnmcd 4 552 Mar-22-2024, 11:51 AM
Last Post: johnmcd
  python convert multiple files to multiple lists MCL169 6 1,730 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Help creating shell scrip for python file marciokoko 10 1,563 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,818 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Creating csv files from Excel file azizrasul 40 6,367 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  Creating list of lists, with objects from lists sgrinderud 7 1,789 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,590 Jul-27-2022, 08:50 PM
Last Post: rob101
  Multiple Loop Statements in a Variable Dexty 1 1,270 May-23-2022, 08:53 AM
Last Post: bowlofred
  Multiple user defined plots with secondary axes using for loop maltp 1 1,592 Apr-30-2022, 10:19 AM
Last Post: maltp
  Creating file with images BobSmoss 1 1,455 Jan-08-2022, 08:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020