如何关联两个列表或列?

How to associate two lists or columns?

Code/Program:

    # 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', 'YAL068C', '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 行的输出,您在评论 您的问题下提供了 link(不是里面显示的样本数据):

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': []}