In my python script, there are 7 rows (0 to 6) in a list of lists (alignments). The SeqIDs of the first 2 rows, the next 2 rows and the last 3 rows are similar. Now, while appending the rows using the following codes
uniques = [] ap = open(tempparse, ‘w’) for row in range(len(alignments)): ap.write(‘row ‘+str(row)+’\n’)
for compare in range(row+1,len(alignments)):
......
......
.......
the last row (i. e. the last one of the last 3 rows in the list ) is not appended because it does not have any subsequent row to compare, and therefore, the six rows are appended except for the last one.
How can I resolve this issue?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Maybe just try adding additional if statement and if there are no subsequent row to compare just add a default value.
If you wish you could share some of the data that you are running this against so that I could test it as well as it is very hard to just imagine the result without seeing any data.
Heya,
It seems that you want to compare each row with every subsequent row in your “alignments” list and append it somewhere, and you’re having trouble because the last row doesn’t have any rows after it to compare with. This behaviour is due to the nature of your for loop which compares the current row with the next ones, and hence the last row has no subsequent row to be compared.
If you want to include the last row in your “uniques” list regardless of comparison, you can simply append it after the loop. Here is the modified code:
uniques = []
ap = open(tempparse, 'w')
for row in range(len(alignments)):
ap.write('row '+str(row)+'\n')
for compare in range(row+1,len(alignments)):
# Your comparison and appending logic here
# If some condition is met, add the row or compare index to uniques
# For example: uniques.append(alignments[row])
# After the loop ends, check if the last row is already in uniques.
# If not, append it.
if alignments[-1] not in uniques:
uniques.append(alignments[-1])
ap.close()
Please modify the comparison and appending logic as per your needs.
NOTE: It’s highly recommended to use with open(tempparse, 'w') as ap: instead of ap = open(tempparse, 'w'). This ensures that the file is properly closed after it is no longer needed. Here is how you can use it:
with open(tempparse, 'w') as ap:
for row in range(len(alignments)):
ap.write('row '+str(row)+'\n')
# Rest of the code
With this modification, you don’t need to call ap.close(), as it will be done automatically when the with block is exited.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.