程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何关联两个列表或列?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何关联两个列表或列??

开发过程中遇到如何关联两个列表或列?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何关联两个列表或列?的解决方法建议,希望对你解决如何关联两个列表或列?有所启发或帮助;

代码/程序:

    # 1. Represent all unique feature types (column 2).

    lines = f.read().split('\n')

    # Set features

    unique_features = set((i.split('\t')[1]) for i in lines)
    unique_features = sorted(unique_features)
    qty_features = [str(i.split('\t')[1]) for i in lines]

for feature in unique_features:
    print(feature,qty_features.count(feature))


# 2. For every feature type (column 2) it stores all feature names (column 4) that had that type.

# Create a List of types and names

types = List(unique_features)
names = List([str(i.split('\t')[3]) for i in lines])

data = dict()

for i in range(len(types)):
    data[types[i]] = names[i]

print(data)

我在评论后的代码中有问题:

"# 2. Demonstrate the use of a data structure that associates the feature names to feature types".

我正在尝试获取与第 2 列关联的文件第 4 列中的数据。

期望的输出:

对于每个特征类型(第 2 列),我想获取与特征类型相关联的所有特征名称(第 4 列)。

{'ORF': ['YAL069W','YAL068W-A','YAL068C','YAL067W-A' ... ] }
{'CDS': ['YAL068W-A','YAL067W-A','YAL067C' ... ] }

以及第 2 列中的所有唯一值:

(ARS
ARS_consensus_sequence
CDS
LTR_retrotransposon
ORF
W_region
X_element
X_element_combinatorial_repeat
X_region
Y_prime_element
Y_region
Z1_region
...
etc etc)

属于它们的第 4 列的每个值都存储在它们中,就像对它们进行分类一样......

我得到的代码是

{'ARS': 'YAL069W','ARS_consensus_sequence': '','CDS': 'YAL068W-A','LTR_retrotransposon': '','ORF': 'ARS102','W_region': 'TEL01L','X_element': '','X_element_combinatorial_repeat': '','X_region': '','Y_prime_element': 'YAL068C','Y_region': '','Z1_region': 'YAL067W-A','Z2_region': '','blocked_reading_frame': 'ARS103','centromere': 'YAL067C','centromere_DNA_Element_I': '','centromere_DNA_Element_II': 'YAL066W','centromere_DNA_Element_III': '','external_transcribed_spacer_region': 'YAL065C','five_prime_UTR_intron': '','gene_group': 'YAL064W-B','intein_enCoding_region': '','internal_transcribed_spacer_region': 'YAL064C-A','intron': '','long_terminal_repeat': 'YAL064W','mating_type_region': '','matrix_attachment_site': 'YALWdelta1','ncRNA_gene': 'YAL063C-A','non_transcribed_region': '','nonCoding_exon': 'YAL063C','not in systematic sequence of S288C': '','not physically mapped': 'ARS104','origin_of_replication': '','plus_1_translational_frameshift': 'YAL062W','pseudogene': '','rRNA_gene': 'YAL061W','silent_mating_type_cassette_array': '','snRNA_gene': 'YAL060W','snorNA_gene': '','tRNA_gene': 'YAL059W','telomerase_RNA_gene': '','telomere': 'YAL059C-A','telomeric_repeat': '','transposable_element_gene': 'YAL058W'}

在该输出中,它打印的是已经设置的“unique_features”,但由于功能名称与其不对应,它甚至没有将它们全部打印出来。

EG。文件:

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  

解决方法

您的代码效率低下,因为它反复拆分所有行。在下面的代码中,这仅在首次从文件中读入时执行一次。此外,在读取之后,它们会被转置为行列,因为大部分处理是针对每列中的内容完成的。

from pprint import pprint,pp

NUMCOLS = 4  # Number columns of interest.
filepath = 'SGD_features.tab'

# Retrieve only number of columns needed from each row of data.
with open(filepath) as file:
    rows = [row.split('\t',NUMCOLS)[:NUMCOLS] for row in file.read().splitlines()]

cols = list(zip(*rows))  # Transpose rows and columns for further processing.
unique_features = sorted(set(cols[1]))

print('Quantities of each unique feature')
qty_features = cols[1]
for feature in unique_features:
    print(f'  {qty_features.count(feature)} - {feature}')

feature_dict = {key: [] for key in unique_features}  # Initialize to empty lists.
for col1,col3 in zip(cols[1],cols[3]):
    if col3:
        feature_dict[col1].append(col3)

print()
print('Feature dictionary')
pp(feature_dict,indent=1)

处理该 SGD_features.tab 文件的前 15 行的输出,您在问题下的评论中提供了链接(不是中显示的示例数据)它):

Quantities of each unique feature
  2 - ARS
  4 - CDS
  5 - ORF
  1 - X_element
  1 - X_element_combinatorial_repeat
  1 - telomere
  1 - telomeric_repeat

Feature dictionary
{'ARS': ['ARS102','ARS103'],'CDS': [],'ORF': ['YAL069W','YAL068W-A','YAL068C','YAL067W-A','YAL067C'],'X_element': [],'X_element_combinatorial_repeat': [],'telomere': ['TEL01L'],'telomeric_repeat': []}

大佬总结

以上是大佬教程为你收集整理的如何关联两个列表或列?全部内容,希望文章能够帮你解决如何关联两个列表或列?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: