简体   繁体   中英

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"]. I'd like to convert it to a list of individual unique individuals: ["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.

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.

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:

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'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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