【问题标题】:How to get unique values/elements of a column?如何获取列的唯一值/元素?
【发布时间】:2021-07-20 08:31:42
【问题描述】:

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

代码:

# 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

前。文件

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  

【问题讨论】:

  • 请在您的问题中添加SGD_features.tab 文件内容的简短示例以及处理it 所需的输出。
  • 您好,感谢您的帮助,我更新了我的问题。
  • 好——我已经发布了答案。

标签: python unique-values


【解决方案1】:

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

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

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

print(unique_list)

【讨论】:

  • 谢谢,这在一定程度上对我有用,因为某些数据(例如“未物理映射”)在它们之间包含空间,并且在您给我的帮助下,它只会给我“不”,而“物理映射”被忽略了。 * 我更新了我的问题。
  • 我忘记了split() 中的\t 分隔符
  • 感谢您的宝贵时间和帮助!
【解决方案2】:

尝试以下方法:

替换你的 dollowing 代码行:

unique_list = str(set(features))

以下内容:

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

【讨论】:

  • 感谢您的帮助,但它没有用,我得到了同样的结果。我更新了我的问题。
【解决方案3】:

如果顺序无关紧要,您可以通过从文件中行的第 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)

【讨论】:

  • 如何整理它?它是按字母顺序还是按出现顺序都没有关系。我尝试使用unique_features.sorted(),但它对我不起作用,它一直在随机打印不同的订单。很抱歉给您带来不便,我是这个 Python 世界的新手,事实上,谢谢,这个答案对我有用。
  • @Valy1004:问题是sets 不保留顺序(所以这个问题也存在于涉及他们的其他答案中)。您可以通过sorted(unique_features) 获得按字母顺序排序的列表。请看What should I do when someone answers my question?
  • 好的,谢谢。我尝试使用sorted(unique_features),但它一直在随机打印。不过没关系,我会继续努力的。非常感谢您的帮助和时间!
  • Valy1004:嗯,这非常很奇怪,如果set 包含相同的元素,就不应该发生这种情况。你在做print(sorted(unique_features))吗?您可以使用unique_features = sorted(unique_features) 将其永久化。有一个评价很高的第三方模块,名为 sortedcontainers,你可以试试它有一个 SortedSet——尽管坦率地说,我希望得到相同的结果。
  • 确实,list.sort() 方法 就地更改容器并且不返回任何内容,而 sorted() 函数 可能会令人困惑b> 创建并返回一个新的list 对象(但可以应用于任何可迭代的对象,例如set)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 2017-04-22
  • 1970-01-01
  • 2021-06-02
相关资源
最近更新 更多