Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- x = [1,2,3,4,5,6]
- new_x = [i for i in x] # instead for traditional for loop
- print(new_x)
- new_x1 = [i * i for i in x] # square of elements
- print(new_x1)
- new_x2 = [i for i in x if i % 2 == 0] # total even
- print(new_x2)
- new_x3 = [(i, j) for i in x for j in 'abcd'] # all pair
- print(new_x3)
- # dictionary comprehensions
- # ---------------------------------
- # for lang,frame in zip( language,frameWork) :
- # combine[lang] = frame
- # print(combine)
- language = ['js', 'php', 'python']
- frameWork = ['vetur', 'cakephp', 'flask']
- combine = {lang: frame for lang, frame in zip(language, frameWork)}
- print(combine)
- # set comprehensions
- # ---------------------------------
- y = [1, 2, 3, 3, 4, 547, 9, 9, -1, 2, 3, 8, 5, 6, 1]
- # new_y = set()
- # for i in y:
- # new_y.add(i)
- # print(new_y)
- # or,
- new_y = {i for i in y} # get unique number
- print(new_y)
- repeated_numbers = dict(Counter(y)) # all repeated number
- print(repeated_numbers)
Advertisement
Add Comment
Please, Sign In to add comment