我有一个python脚本,它解析一个xml文件,然后给我所需的信息。我的输出看起来像这样,并且100%正确:
output = ['77:275,77:424,77:425,77:426,77:427,77:412,77:413,77:414,77:412,77:413,77:414,77:412,77:413,77:414,77:412,77:413,77:414,77:431,77:432,77:433,77:435,77:467,77:470,77:471,77:484,77:485,77:475,77:476,77:437,77:438,77:439,77:440,77:442,77:443,77:444,77:445,77:446,77:447,77:449,77:450,77:451,77:454,77:455,77:456,77:305,77:309,77:496,77:497,77:500,77:504,77:506,77:507,77:508,77:513,77:515,77:514,77:517,77:518,77:519,77:521,77:522,77:523,77:403,77:406,77:404,77:405,77:403,77:406,77:404,77:405,77:526,77:496,77:497,77:500,77:504,77:506,77:507,77:508,77:513,77:515,77:514,77:517,77:518,77:519,77:521,77:522,77:523,77:403,77:406,77:404,77:405,77:403,77:406,77:404,77:405,77:526,77:317,77:321,77:346,77:349,77:350,77:351,77:496,77:497,77:500,77:504,77:506,77:507,77:508,77:513,77:515,77:514,77:517,77:518,77:519,77:521,77:522,77:523,77:403,77:406,77:404,77:405,77:403,77:406,77:404,77:405,77:526,77:496,77:497,77:500,77:504,77:506,77:507,77:508,77:513,77:515,77:514,77:517,77:518,77:519,77:521,77:522,77:523,77:403,77:406,77:404,77:405,77:403,77:406,77:404,77:405,77:526,77:362,77:367,77:369,77:374,77:370,77:372,77:373,77:387,77:388,77:389,77:392,77:393,77:394,77:328,77:283,77:284,77:285,77:288,77:289,77:290,77:292,']
一切都很好,但我想删除元素中的重复元素,就像上面的情况一样。我尝试使用OrderedDict包或只是简单的列表(设置(输出)),但是他们两个都没有用。有没有人知道如何解决这个问题。
答案 0 :(得分:4)
列表中有一个元素。如果您希望将其视为单独的元素,则需要明确拆分它。
您可以将','
逗号字符上的字符串拆分为包含str.split()
的列表:
separate_elements = output[0].split(',')
之后您可以使用set()
(无序)或OrderedDict
(维护顺序)并重新加入字符串,如果您仍然只需要一个字符串对象:
','.join(set(separate_elements))
你可以把它放回一个只有一个元素的列表中,但如果你所处理的只是一个字符串,那就没什么意义了。