Python 如何关联两个列表或列?
代码/程序:Python 如何关联两个列表或列?,python,list,dictionary,multiple-columns,Python,List,Dictionary,Multiple Columns,代码/程序: # 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
# 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)
注释后面的代码中有问题:
# 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.演示将要素名称与要素类型关联的数据结构的使用。”
我正在尝试获取与第2列关联的文件第4列中的数据
所需输出:
# 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列),我希望获得与要素类型关联的所有要素名称(第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'}
在该输出中,它打印的是已经设置的“唯一的功能”,但如果功能名称与它不对应,它甚至不会打印所有功能
例如,文件:
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行的输出(非其中显示的示例数据):
以防万一,文件的链接:[