3 ms·
Most languages have these type of functions, once you've read the file into an array its just array/list manipulation. I haven't compile checked this but here's
by matthewking 10y ago
Most languages have these type of functions, once you've read the file into an array its just array/list manipulation. I haven't compile checked this but here's the same as a C# one liner:
List<string> lines = File.ReadAllLines("/file.txt").Distinct().ToList().Select(l => l.Trim()).Sort();
- imauld 10y agoAnd in Python: with open("foo.txt.") as f: foo = sorted(list(set(line.strip() for line in f]))) with is a context manager that will automatically close the file once it's out of scope, not required but it's much cleaner and safer. Should the code change you don't have to worry about the call to close the file getting lost. The call to set() could be replaced with a set comprehension, {i for i in sequence}, but for clarity I used set(). It's certainly not the most efficient way to do it but it's simple and clear.