I have a list of input variables as:
field_list = ['u', 'w', 'uu', 'uv', 'uw']
I would like to get a list as output containing all the unique characters. If I know beforehand that I'll only be dealing with 'u', 'v' and 'w', I can write something like
indv_fields = []
for field in field_list:
if "u" in field:
indv_fields.append("u")
if "v" in field:
indv_fields.append("v")
if "w" in field:
indv_fields.append("w")
indv_fields = list(set(indv_fields))
print(indv_fields)
I would like to extend this (and possibly make it more efficient) for a case where I do not know the variables to be extracted beforehand. Let's say if I received input like
field_list = ['u', 'v', 'w', 'T', 'ww']
I also want to extract 'T' which won't be possible from the code above. Also, I do not know beforehand what all variables might be there in this list. In such scenario, how do I efficiently extract all the unique elements?
CodePudding user response:
you could just join all the strings together then call set on it.
field_list = ['u', 'w', 'uu', 'uv', 'uw']
print(set("".join(field_list)))
field_list = ['u', 'tw', 'ugu', 'uzv', 'uxw']
print(set("".join(field_list)))
OUTPUT
{'w', 'v', 'u'}
{'t', 'u', 'w', 'x', 'g', 'z', 'v'}