Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Why does list import using quotation marks, how can I avoid this/get rid of them?
    primarykey
    data
    text
    <p>Really simple question here but bugging me for long enough to ask. Code looks like this:</p> <pre><code>f4 = open("genomekey2.txt", 'rb') keyline = f4.readline() keygenomes = [] for keyline in f4: keygenomes.append(keyline[:-1]) </code></pre> <p>the genomekey2.txt file format looks like this </p> <pre><code>['Prochlorococcus marinus str. MIT 9202'] ['Prochlorococcus marinus str. NATL1A'] ['Synechococcus sp. RS9917'] ['Nostoc sp. PCC 7120'] ['Synechococcus sp. JA-2-3B'a(2-13)'] </code></pre> <p>The problem being when I print the genomekey list it has all of the entries I want but with quotation marks around each of the [ ] found within the list. I want to get rid of the quotation marks so I can compare it with another list but so far haven't found a way. I tried...</p> <pre><code>for a in keygenomes: a.replace('"', '') </code></pre> <p>But that didn't seem to work. I would rather a solution where it just doesn't add the quotation marks on at all. What are they for anyway and which part of the code (.append, .readline()) is responsible for adding them? Massively beginner question here but you guys seem pretty nice.</p> <p>Edit: I eventually want to compare it with a list which is formatted as such</p> <p>[['Arthrospira maxima CS-328'], ['Prochlorococcus marinus str. MIT 9301'], ['Synechococcus sp. CC9605'], ['Synechococcus sp. WH 5701'], ['Synechococcus sp. CB0205'], ['Prochlorococcus marinus str. MIT 9313'], ['Synechococcus sp. JA-3-3Ab'], ['Trichodesmium erythraeum IMS101'], ['Synechococcus sp. PCC 7335'], ['Trichodesmium erythraeum IMS101'], ...</p> <p>Edit: So I think I got something to work with a combination of answers, thank you all for your help! The quotations were interfering with the list comparison so I just added them on to the first list as well, even though I think it's only mimicking the list being entered as a string (of which I now think I understand the distinction) it seems to work</p> <pre><code>f4 = open("genomekey2.txt", 'rb') keyline = f4.readline() keygenomes = [] for keyline in f4: keygenomes.append(keyline[:-1]) specieslist = " ".join(["%s" % el for el in specieslist]) nonconservedlist = [i for i in keygenomes if i not in specieslist] </code></pre> <p>Edit: Yeah the above worked but the more elegant solution I found here (http://forums.devshed.com/python-programming-11/convert-string-to-list-71857.html) after understanding the problem better thanks to your guys help is like this:</p> <pre><code>for keyline in f4: keyline = eval(keyline) keygenomes.append(keyline) </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload