karpathy / makemore

An autoregressive character-level language model for making more things
MIT License
2.59k stars 681 forks source link

Simplify code for collecting + sorting of all possible characters. #12

Open jonasreinsch opened 1 year ago

jonasreinsch commented 1 year ago

Small code simplification:

the line

chars = sorted(list(set(''.join(words))))

can be simplified to

chars = sorted(set(''.join(words)))

because sorted(...) accepts any iterable and set(...) returns an iterable.

I noticed this in your building makemore video (thank you for that!) at 15:56

ahasan85 commented 1 year ago

@jonasreinsch set is unordered https://docs.python.org/3.11/library/stdtypes.html#set-types-set-frozenset

jonasreinsch commented 1 year ago

@jonasreinsch set is unordered https://docs.python.org/3.11/library/stdtypes.html#set-types-set-frozenset

@ahasan85 yes, but sorted then returns an ordered / sorted list.

Just try it out:

>>> sorted(set(list(''.join(['hello', 'my', 'friends']))))
['d', 'e', 'f', 'h', 'i', 'l', 'm', 'n', 'o', 'r', 's', 'y']
>>> sorted(set(''.join(['hello', 'my', 'friends'])))
['d', 'e', 'f', 'h', 'i', 'l', 'm', 'n', 'o', 'r', 's', 'y']