简体   繁体   English

我想分解一个字符串列表,以将“ /”和“和”拆分为唯一字符串列表

[英]I'd like to disaggregate a list of strings to split on “/” and “ and ” to a list of unique strings

I have a list like ["Alex Smith", "John Jones/John Jones and Anna Conner", "James O'Brien"]. 我有一个列表,例如[“ Alex Smith”,“ John Jones / John Jones和Anna Conner”,“ James O'Brien”]。 I'd like to convert it to a list of individual unique individuals: ["Alex Smith", "John Jones", "Anna Conner", "James O'Brien"]. 我想将其转换为一个单独的个人列表:[“ Alex Smith”,“ John Jones”,“ Anna Conner”,“ James O'Brien”]。 I can use list(set(vector)) to get the uniqueness that I want, but the splitting is giving me a headache. 我可以使用list(set(vector))获得我想要的唯一性,但是拆分让我头疼。

I looked at Flattening a shallow list in Python and it looked good, but it disggregated down to the indivual letters rather than the combination of first and last names. 我看了用Python整理一个浅表 ,它看起来不错,但是它分解为单个字母,而不是名字和姓氏的组合。

Pick a delimiter, join on that delimiter, convert all delimiters to that one, split on that delimiter, then use set() as you were planning on to remove the duplicates: 选择一个定界符,加入该定界符,将所有定界符转换为该定界符,在该定界符上拆分,然后按计划使用set()删除重复项:

l = ["Alex Smith", "John Jones/John Jones and Anna Conner", "James O'Brien"]
new_set = set('/'.join(l).replace(' and ', '/').split('/'))

Result: 结果:

>>> new_set
{"James O'Brien", 'Alex Smith', 'John Jones', 'Anna Conner'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM