MTG / pymtg

Python research utils that some of us use at the MTG and eventually everyone will use :)
https://mtg.github.io/pymtg/
MIT License
7 stars 1 forks source link

Python tip of the week #4

Open alastair opened 5 years ago

alastair commented 5 years ago

Some suggestions of tips to email people about how to do things better in python

LPorcaro commented 5 years ago

I like to use pyflakes for checking my code while testing https://pypi.org/project/pyflakes/ Maybe it can be in the same tip with stylechecks (even if it is not properly a stylecheck)

jordipons commented 5 years ago

student_tuples = [ ('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10), ] sorted(student_tuples, key=lambda student: student[2]) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Sort with the lambda function. https://wiki.python.org/moin/HowTo/Sorting

alastair commented 5 years ago

sorting is a great item! especially using lambdas or the operator module for the key parameter.

xavierfav commented 5 years ago

More suggestions:

wuaalb commented 5 years ago

example of splitting note sequence into phrases using itertools.groupby:

>>> import itertools
>>> notes = ['sil', 'g', 'g', 'd', 'd', 'e', 'e', 'd', 'sil', 'c', 'c', 'b', 'b', 'a', 'a', 'g', 'sil']
>>> phrases = []
>>> for is_sil, group in itertools.groupby(notes, key=lambda note: note == 'sil'):
...     if not is_sil:
...         phrases.append(list(group))
... 
>>> print(phrases)
[['g', 'g', 'd', 'd', 'e', 'e', 'd'], ['c', 'c', 'b', 'b', 'a', 'a', 'g']]