Advertisement
here2share

# all4python.py

Jul 16th, 2020
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # all4python.py
  4.  
  5. from collections import Counter
  6.  
  7. def anagram(first, second):
  8.     return Counter(first) == Counter(second)
  9.  
  10. anagram("abcd3", "3acdb") # True
  11.  
  12. def all_unique(lst):
  13.     return len(lst) == len(set(lst))
  14.  
  15. x = [1,1,2,2,3,2,3,4,5,6]
  16. y = [1,2,3,4,5]
  17. all_unique(x) # False
  18. all_unique(y) # True
  19.  
  20. def compact(lst):
  21.     return list(filter(None, lst))
  22.  
  23. compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]
  24.  
  25. array = [['a', 'b'], ['c', 'd'], ['e', 'f']]
  26. transposed = zip(*array)
  27. print(transposed) # [('a', 'c', 'e'), ('b', 'd', 'f')]
  28.  
  29. a = 3
  30. print( 2 < a < 8) # True
  31. print(1 == a < 2) # False
  32.  
  33. hobbies = ["basketball", "football", "swimming"]
  34.  
  35. print("My hobbies are:") # My hobbies are:
  36. print(", ".join(hobbies)) # basketball, football, swimming
  37.  
  38. def get_vowels(string):
  39.     return [each for each in string if each in 'aeiou']
  40.  
  41. get_vowels('foobar') # ['o', 'o', 'a']
  42. get_vowels('gym') # []
  43. def decapitalize(str):
  44.     return str[:1].lower() + str[1:]
  45.  
  46. decapitalize('FooBar') # 'fooBar'
  47.  
  48. def deep_flatten(xs):
  49.     flat_list = []
  50.     [flat_list.extend(deep_flatten(x)) for x in xs] if isinstance(xs, list) else flat_list.append(xs)
  51.     return flat_list
  52.  
  53. deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
  54.  
  55. def difference(a, b):
  56.     set_a = set(a)
  57.     set_b = set(b)
  58.     comparison = set_a.difference(set_b)
  59.     return list(comparison)
  60.    
  61. ###
  62.  
  63. def difference_by(a, b, fn):
  64.     b = set(map(fn, b))
  65.     return [item for item in a if fn(item) not in b]
  66.  
  67. from math import floor
  68. difference_by([2.1, 1.2], [2.3, 3.4], floor) # [1.2]
  69. difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ]
  70.  
  71. def add(a, b):
  72.     return a + b
  73.  
  74. def subtract(a, b):
  75.     return a - b
  76.  
  77. a, b = 4, 5
  78. print((subtract if a > b else add)(a, b)) # 9  
  79.  
  80. def has_duplicates(lst):
  81.     return len(lst) != len(set(lst))
  82.    
  83. x = [1,2,3,4,5,5]
  84. y = [1,2,3,4,5]
  85. has_duplicates(x) # True
  86. has_duplicates(y) # False
  87.  
  88. def merge_two_dicts(a, b):
  89.     c = a.copy()   # make a copy of a
  90.     c.update(b)    # modify keys and values of a with the ones from b
  91.     return c
  92.  
  93. a = { 'x': 1, 'y': 2}
  94. b = { 'y': 3, 'z': 4}
  95. print(merge_two_dicts(a, b)) # {'y': 3, 'x': 1, 'z': 4}
  96.  
  97. def to_dictionary(keys, values):
  98.     return dict(zip(keys, values))
  99.  
  100. keys = ["a", "b", "c"]    
  101. values = [2, 3, 4]
  102. print(to_dictionary(keys, values)) # {'a': 2, 'c': 4, 'b': 3}
  103.  
  104. list = ["a", "b", "c", "d"]
  105. for index, element in enumerate(list):
  106.     print("Value", element, "Index ", index, )
  107. # ('Value', 'a', 'Index ', 0)
  108. # ('Value', 'b', 'Index ', 1)
  109. # ('Value', 'c', 'Index ', 2)
  110. # ('Value', 'd', 'Index ', 3)  
  111.  
  112. import time
  113.  
  114. start_time = time.time()
  115.  
  116. a = 1
  117. b = 2
  118. c = a + b
  119. print(c) #3
  120.  
  121. end_time = time.time()
  122. total_time = end_time - start_time
  123. print("Time: ", total_time)
  124.  
  125. # ('Time: ', 1.1205673217773438e-05)
  126.  
  127. try:
  128.     2*3
  129. except TypeError:
  130.     print("An exception was raised")
  131. else:
  132.     print("No exceptions were raised")
  133.  
  134. # No exceptions were raised
  135.  
  136. def most_frequent(list):
  137.     return max(set(list), key = list.count)
  138.  
  139. numbers = [1,2,1,2,3,2,1,4,2]
  140. most_frequent(numbers)  
  141.  
  142. def palindrome(a):
  143.     return a == a[::-1]
  144.  
  145. palindrome('refer') # True
  146.  
  147. from random import shuffle
  148.  
  149. foo = [1, 2, 3, 4]
  150. shuffle(foo)
  151. print(foo) # [1, 4, 3, 2] , foo = [1, 2, 3, 4]
  152.  
  153. a, b = -1, 14
  154. a, b = b, a
  155.  
  156. print(a) # 14
  157. print(b) # -1
  158.  
  159. d = {'a': 1, 'b': 2}
  160.  
  161. print(d.get('c', 3)) # 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement