brennerm / pytricks Goto Github PK
View Code? Open in Web Editor NEWCollection of less popular features and tricks for the Python programming language
License: MIT License
Collection of less popular features and tricks for the Python programming language
License: MIT License
Why not use the <true> if <predicate> else <false>
structure instead?
It is a designed feature of the language, not a hack of the type system.
Line 6 in 19f773c
At first I want to thank all of you for your help, didn't expect this to become that big :).
All those contributions lead to fact that we need some kind of structuring.
When I enter this repo I feel a little bit overwhelmed with this amount of files.
The question that raises is "What's the right way?". Order them by data types, differentiate between function, performance and readability tricks, etc.. Maybe you guys even decide, that we should leave it as it is.
Feel free to add your opinion :)
#! /usr/bin/env python
'''there is a list of tuple, and if you want to judge an object in this list by an elegant way, then...'''
from more_itertools import flatten # pip install more_itertools
types = [('E', 'I'), ('S', 'N'), ('T', 'F'), ('J', 'P')]
if __name__ == '__main__':
assert 'E' in flatten(types)
assert 'X' in flatten(types)
I've made a pull request - #73.
Thought it would be worth an issue to go along side in case it warranted a discussion.
As stated in the pull request:
I think we could justify having a separate file from reverselist for reversing strings and ints.
Some newbies don't have the conceptual leap from slicing lists to slicing strings.
Other possibility is to merge the two into a reverseiterable.py but I feel this is less explicit than two separate files.
Question as title.
https://github.com/brennerm/PyTricks/blob/master/switch.py -
This is not fallthrough as you can expect from switch-case statement ^^"
''' Printing the pattern * ** *** **** '''
for i in range(1,4+1): for j in range(i): print('*', end='') print()
print('\n'.join(['*' * i for i in range(1, 4+1)])
grouping adjacent use zip and iter...
some code like this:
lst = range(10)
group_n = lambda lst, n: zip(*([iter(lst)] * n))
print group(lst, 3)
#output
#[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
Even though it's just an example, sum()
function shouldn't be used. Overwriting built-in functions is bad practice (unexpected behavior if wanting to use Python sum()
).
How about changing it to product or difference or concat?
https://github.com/brennerm/PyTricks/blame/master/compoundboolstatement.py#L6
Absolutely would not work.
Replace with if 1 and 6 and not 4 in l:
to see that the answer is still true.
Python will check only if not 4 in l
, as "1 and 6" is True.
"""To find the number of times characters are repeated in a string """
from collections import Counter
Counter('abracadabra').most_common(3)
Counter('abracadabra').most_common(1)
I am asking because I am not seeing any response to PR's or issues for quite some time?
Any ideas guys?
Some method of organization, or "index" in the README might be useful. See https://github.com/vinta/awesome-python for an example.
Perhaps part of the users PR to add a trick would be to add an entry in the README?
if a > 7: b = '>7' elif a == 7: b = '7' else: b = '<7'
b = '>7' if a > 7 else '7' if a == 7 else '<7'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.