Skip to content
Advertisement

How to get unique values/elements of a column?

I am trying to get the unique values of a column from a tab. The values are repeated and the file has 1,000+ lines, I just want to have the names of the values, not all, and the ones that are repeated. I’m working on my code, but when I do “RUN” it generates the separate and random letters of the values (see example in ‘Output’ below). I hope someone can help me find my mistake. Please and thank you very much!

Code:

# Open file
file = open('SGD_features.tab')

# Demonstrate the use of a data structure to represent all unique feature types (column 2).

# Iterate for just one iteration
for line in file:

      # Get rid of new lines at the end.
      line = line.strip()

      # File is tab-delimited.
      elems = line.split("t")
      features = elems[1]
      unique_list = str(set(features))

      print(unique_list)

Output:

{'O', 'F', 'R'}
{'S', 'C', 'D'}
{'O', 'F', 'R'}
{'S', 'C', 'D'}
{'S', 'A', 'R'}
{'e', 'l', 'o', 'm', 'r', 't'}
{'e', 'l', 'i', 'p', 'a', 'o', 'm', 'r', '_', 't', 'c'}
{'X', 'e', 'l', 'm', '_', 't', 'n'}
{'X', 'e', 'b', 'l', 'i', 'p', 'a', 'o', 'm', 'r', '_', 't', 'c', 'n'}
{'O', 'F', 'R'}
{'S', 'C', 'D'}
{'O', 'F', 'R'}
{'S', 'C', 'D'}

And so on…

DESIRED OUTPUT:

ORF
CDS
ARS
telomere
telomeric_repeat
X_element
X_element_combinatorial_repeat

EX. FILE

S000036595  noncoding_exon                  snR18       1   142367  142468  W       2011-02-03  2000-05-19|2007-05-08   
S000000002  ORF Verified    YAL002W VPS8    CORVET complex membrane-binding subunit VPS8|VPL8|VPT8|FUN15    chromosome 1    L000003013  1   143707  147531  W       2011-02-03  2004-01-14|1996-07-31   Membrane-binding component of the CORVET complex; involved in endosomal vesicle tethering and fusion in the endosome to vacuole protein targeting pathway; interacts with Vps21p; contains RING finger motif
S000031737  CDS                 YAL002W     1   143707  147531  W       2011-02-03  2004-01-14|1996-07-31   
S000121255  ARS     ARS108      ARSI-147    chromosome 1        1   147398  147717          2014-11-18  2014-11-18|2007-03-07   Autonomously Replicating Sequence
S000000001  ORF Verified    YAL001C TFC3    transcription factor TFIIIC subunit TFC3|tau 138|TSV115|FUN24   chromosome 1    L000000641|L000002287   1   151166  147594  C   -1  2011-02-03  1996-07-31  Subunit of RNA polymerase III transcription initiation factor complex; part of the TauB domain of TFIIIC that binds DNA at the BoxB promoter sites of tRNA and similar genes; cooperates with Tfc6p in DNA binding; largest of six subunits of the RNA polymerase III transcription initiation factor complex (TFIIIC)
S000030735  CDS                 YAL001C     1   151006  147594  C       2011-02-03  1996-07-31  
S000030734  CDS                 YAL001C     1   151166  151097  C       2011-02-03  1996-07-31  
S000030736  intron                  YAL001C     1   151096  151007  C       2011-02-03  1996-07-31  

Advertisement

Answer

If order doesn’t matter, you could do it by creating a set from the items in column 2 of the lines in the file:

with open('SGD_features.tab') as file:
    unique_features = set(line.split('t')[1] for line in file)

for feature in unique_features:
    print(feature)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement