如何获得列的唯一 values/elements?

How to get unique values/elements of a column?

我正在尝试从选项卡中获取列的唯一值。这些值是重复的,文件有 1,000 多行,我只想拥有值的名称,而不是全部,以及重复的名称。我正在处理我的代码,但是当我执行“运行”时,它会生成值的单独和随机字母(请参见下面 'Output' 中的示例)。我希望有人能帮我找到我的错误。拜托,非常感谢!

代码:

# 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)

输出:

{'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'}

等等...

期望的输出:

ORF
CDS
ARS
telomere
telomeric_repeat
X_element
X_element_combinatorial_repeat

EX。文件

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  

features只是文件一行中的一个字符串,而不是该列中的所有字符串。

将每个单词添加到循环中的 unique_list 集合中,并在最后打印集合。

unique_list = set()
for line in file:
    line = line.strip()
    unique_list.add(line.split('\t')[1])

print(unique_list)

尝试以下操作:

替换下面的代码行:

unique_list = str(set(features))

具有以下内容:

unique_list = ' '.join(set(features))

如果顺序无关紧要,您可以根据文件中第 2 列中的项目创建一个 set

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)