[英]Sort tuples in a list of lists in a sequence
I have a several large lists of lists of tuples.我有几个大的元组列表。 One of them is:
其中之一是:
list_one[:20]
>>>
[[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Immunologic Factor')],
[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Organic Chemical')],
[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding')],
[('Pharmacologic Substance', 'Immunologic Factor'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Chemical', 'Biologically Active Substance')],
[('Pharmacologic Substance', 'Organic Chemical'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Chemical', 'Biologically Active Substance')],
[('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding'),
('Chemical', 'Biologically Active Substance')],
[('Pharmacologic Substance', 'Immunologic Factor'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Gene or Genome', 'Biologically Active Substance')],
[('Pharmacologic Substance', 'Organic Chemical'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Gene or Genome', 'Biologically Active Substance')],
[('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding'),
('Gene or Genome', 'Biologically Active Substance')],
[('Disease or Syndrome', 'Sign or Symptom'),
('Manufactured Object', 'Disease or Syndrome'),
('Molecular Biology Research Technique', 'Manufactured Object')],
[('Disease or Syndrome', 'Patient or Disabled Group'),
('Manufactured Object', 'Disease or Syndrome'),
('Molecular Biology Research Technique', 'Manufactured Object')],
[('Disease or Syndrome', 'Age Group'),
('Manufactured Object', 'Disease or Syndrome'),
('Molecular Biology Research Technique', 'Manufactured Object')],
[('Disease or Syndrome', 'Finding'),
('Manufactured Object', 'Disease or Syndrome'),
('Molecular Biology Research Technique', 'Manufactured Object')],
[('Disease or Syndrome', 'Population Group'),
('Manufactured Object', 'Disease or Syndrome'),
('Molecular Biology Research Technique', 'Manufactured Object')],
[('Disease or Syndrome', 'Animal'),
('Manufactured Object', 'Disease or Syndrome'),
('Molecular Biology Research Technique', 'Manufactured Object')],
[('Body Part, Organ, or Organ Component', 'Pathologic Function'),
('Pathologic Function', 'Finding'),
('Nucleic Acid, Nucleoside, or Nucleotide',
'Body Part, Organ, or Organ Component')],
[('Pharmacologic Substance', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function')],
[('Pharmacologic Substance', 'Immunologic Factor'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Tissue', 'Biologically Active Substance')],
[('Pharmacologic Substance', 'Organic Chemical'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Tissue', 'Biologically Active Substance')],
[('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding'),
('Tissue', 'Biologically Active Substance')]]
How can I sort each sublist such that the tuples are in sequence (eg, [(1,2), (2,3), (3,4)]
)?如何对每个子列表进行排序,使元组按顺序排列(例如
[(1,2), (2,3), (3,4)]
)?
For example, the first sublist例如,第一个子列表
[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Immunologic Factor')]
has tuples that are in sequence.具有按顺序排列的元组。 The last one however:
然而最后一个:
[('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding'),
('Tissue', 'Biologically Active Substance')]
is not, and should be:不是,应该是:
[('Tissue', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding')
]
Note that the length of the sublist vary between lists (ie, I have 3 tuples in this list, but I may have 2, 5, or any other number).请注意,子列表的长度因列表而异(即,我在此列表中有 3 个元组,但我可能有 2、5 或任何其他数字)。 Also, the tuples can be changed to lists if its easier.
此外,如果更容易,可以将元组更改为列表。
Interesting question.有趣的问题。 You could try the following:
您可以尝试以下方法:
def sort(tuples):
connects = dict(tuples)
steps = []
start = (connects.keys() - connects.values()).pop()
while end := connects.get(start, False):
steps.append((start, end))
start = end
return steps
list_one_sorted = [sort(tuples) for tuples in list_one]
list_one
) into a dictionary, which gives you a natural way to walk through the tuples (start with a key, get the corresponding value, then use the value as key, ...).list_one
的子列表)读入字典,这为您提供了一种遍历元组的自然方式(从一个键开始,获取相应的值,然后将该值用作键,...)。start
: the only key that is not in the values.start
:唯一不在值中的键。start
, and collect the thereby sorted tuples in a new list.start
,并将由此排序的元组收集到一个新列表中。 Result for your list_one
is:您的
list_one
的结果是:
[[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Immunologic Factor')],
[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Organic Chemical')],
[('Bacterium', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding')],
[('Chemical', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Immunologic Factor')],
[('Chemical', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Organic Chemical')],
[('Chemical', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding')],
[('Gene or Genome', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Immunologic Factor')],
[('Gene or Genome', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Organic Chemical')],
[('Gene or Genome', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding')],
[('Molecular Biology Research Technique', 'Manufactured Object'),
('Manufactured Object', 'Disease or Syndrome'),
('Disease or Syndrome', 'Sign or Symptom')],
[('Molecular Biology Research Technique', 'Manufactured Object'),
('Manufactured Object', 'Disease or Syndrome'),
('Disease or Syndrome', 'Patient or Disabled Group')],
[('Molecular Biology Research Technique', 'Manufactured Object'),
('Manufactured Object', 'Disease or Syndrome'),
('Disease or Syndrome', 'Age Group')],
[('Molecular Biology Research Technique', 'Manufactured Object'),
('Manufactured Object', 'Disease or Syndrome'),
('Disease or Syndrome', 'Finding')],
[('Molecular Biology Research Technique', 'Manufactured Object'),
('Manufactured Object', 'Disease or Syndrome'),
('Disease or Syndrome', 'Population Group')],
[('Molecular Biology Research Technique', 'Manufactured Object'),
('Manufactured Object', 'Disease or Syndrome'),
('Disease or Syndrome', 'Animal')],
[('Nucleic Acid, Nucleoside, or Nucleotide',
'Body Part, Organ, or Organ Component'),
('Body Part, Organ, or Organ Component', 'Pathologic Function'),
('Pathologic Function', 'Finding')],
[('Pharmacologic Substance', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function')],
[('Tissue', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Immunologic Factor')],
[('Tissue', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pharmacologic Substance'),
('Pharmacologic Substance', 'Organic Chemical')],
[('Tissue', 'Biologically Active Substance'),
('Biologically Active Substance', 'Pathologic Function'),
('Pathologic Function', 'Finding')]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.