Hello,
I am a newbie at python, as will shortly become painfully apparent. I wrote some code that goes through a list of file names that contain names of stars. I put the star names in 'tgtnamelist'. The corresponding file names are in 'imagelist'. 'imagelist' and 'tgtnamelist' have the same length. 'tgtnamelist' does not contain len(tgtnamelist ) different star names, so I want to group all the filenames that correspond to a unique star name in one list. Basically I want to have subset lists of filenames and which star they contain.
Now, the code I've written works just fine and does exactly what I want, but it's a part of a much larger code where I haev to go through tgtnamelist and iamgelist repeatedly and a whole lot of nested 'for' and 'if' loops so at the end everything becomes very confusing!! Which is why I want to trim down the code a little, so I figure I'll start by getting rid of some unnecessary lists.
Is there a way to do this? To match two lists, one with entries that repeat themselves in subsets of unknown length, to another list with unique entires. The only thing connecting the lists is that target 0 is in file 0, target 1 is in file 1, etc. Target names are unordered, so the same star name can be spread out throughout the list.
Any help, tips and hints would be greatly appreciated!!
#first target
currtgtname=tgt namelist[0]
#located in first file
currimname=imag elist[0]
del imagelist[0]
del tgtnamelist[0]
#all files containing one star
subsets=[]
#all subsets of stars
alltargets=[]
for curr in range(len(image list)):
subsets.append( currimname)
if tgtnamelist[curr]==currtgtname:
currimname=imag elist[curr]
else:
currtgtname=tgt namelist[curr]
currimname=imag elist[curr]
alltargets.appe nd(list(subsets ))
del subsets[:]
I am a newbie at python, as will shortly become painfully apparent. I wrote some code that goes through a list of file names that contain names of stars. I put the star names in 'tgtnamelist'. The corresponding file names are in 'imagelist'. 'imagelist' and 'tgtnamelist' have the same length. 'tgtnamelist' does not contain len(tgtnamelist ) different star names, so I want to group all the filenames that correspond to a unique star name in one list. Basically I want to have subset lists of filenames and which star they contain.
Now, the code I've written works just fine and does exactly what I want, but it's a part of a much larger code where I haev to go through tgtnamelist and iamgelist repeatedly and a whole lot of nested 'for' and 'if' loops so at the end everything becomes very confusing!! Which is why I want to trim down the code a little, so I figure I'll start by getting rid of some unnecessary lists.
Is there a way to do this? To match two lists, one with entries that repeat themselves in subsets of unknown length, to another list with unique entires. The only thing connecting the lists is that target 0 is in file 0, target 1 is in file 1, etc. Target names are unordered, so the same star name can be spread out throughout the list.
Any help, tips and hints would be greatly appreciated!!
#first target
currtgtname=tgt namelist[0]
#located in first file
currimname=imag elist[0]
del imagelist[0]
del tgtnamelist[0]
#all files containing one star
subsets=[]
#all subsets of stars
alltargets=[]
for curr in range(len(image list)):
subsets.append( currimname)
if tgtnamelist[curr]==currtgtname:
currimname=imag elist[curr]
else:
currtgtname=tgt namelist[curr]
currimname=imag elist[curr]
alltargets.appe nd(list(subsets ))
del subsets[:]
Comment