Skip to content

Commit b1544bc

Browse files
authored
Merge pull request Cheatsheet-lang#6 from corleone77/feature/Snippet-memory
New Snippet Added
2 parents 4c0df78 + 1a88698 commit b1544bc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,43 @@ import nltk
211211
nltk.download('punkt')
212212
nltk.download('averaged_perceptron_tagger')
213213
```
214+
215+
## Python Snippets
216+
217+
### Anagrams
218+
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
219+
```
220+
from collections import Counter
221+
def anagram(first, second):
222+
return Counter(first) == Counter(second)
223+
anagram("abcd3", "3acdb") # True
224+
```
225+
### Memory
226+
This snippet can be used to check the memory usage of an object.
227+
```
228+
import sys
229+
230+
variable = 30
231+
print(sys.getsizeof(variable)) # 24
232+
```
233+
### Print a string N times
234+
This snippet can be used to print a string n times without having to use loops to do it.
235+
```
236+
n = 2
237+
s ="Programming"
238+
print(s * n) # ProgrammingProgramming
239+
```
240+
### Chunk
241+
This method chunks a list into smaller lists of a specified size.
242+
```
243+
def chunk(list, size):
244+
return [list[i:i+size] for i in range(0,len(list), size)]
245+
```
246+
### Get vowels
247+
This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.
248+
```
249+
def get_vowels(string):
250+
return [each for each in string if each in 'aeiou']
251+
get_vowels('foobar') # ['o', 'o', 'a']
252+
get_vowels('gym') # []
253+
```

0 commit comments

Comments
 (0)