-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuilt_In_Functions
More file actions
99 lines (74 loc) · 3.5 KB
/
Built_In_Functions
File metadata and controls
99 lines (74 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
1. any and all
all(): # return True if all elements of the iterable are truthy(or if the iterable is empty)
print(all([0,1,2,3])) #False
print(all([])) #True
print(all([char for char in "eio" if char in "aeiou"])) #True
print(all([num for num in [1,2,3] if num %2 == 0 ])) #True
print(all([num for num in [0,2,3] if num %2 == 0 ])) #False
people =["calr", "cass"]
print(all (name[0] == "c" for name in people)) ##True
any(): return True if any elements of the iterable are truthy (if the iterable is empty--false)
print(any([0,1,2,3])) #True
print(any([])) #False
2.generator expression
print(type((name[0] == "c" for name in people))) #<class 'generator'>
# save space, memory, if you only need it once, don't need a list(comprehension) to store then you can use it
import sys
# A simple comparison of size (in Bytes)
list_comp = sys.getsizeof([x * 10 for x in range(1000)]) #List Comprehension: 9024 byte
gen_exp = sys.getsizeof(x * 10 for x in range(1000)) # Generator Expression: 120 bytes
#Using a Generator Expression
def is_all_strings(lst):
return all(type(l) == str for l in lst)
#Using a List Comprehension
def is_all_strings(lst):
return all([type(l) == str for l in lst])
3.sorted()
#sorted() returns a new sorted list from the items in iterable
#remember nums.sort() is inplace()!!! -- which is also a list specific method, can't use on other objects
#sorted method can accept iterable stuff even it is a tuple
more_numbers = [6,1,8,2]
new_numbers = sorted(more_numbers)
new_numbers2 = sorted(more_numbers, reverse = True)
print(more_numbers) #[6, 1, 8, 2]
print(new_numbers) #[1, 2, 6, 8]
print(new_numbers2) # [8, 6, 2, 1]
print(sorted((1,56,3,76))) # [1, 3, 56, 76]
users = [
{"username": "samuel", "tweets": ["I love cake", "I love pie", "hello world!"]},
{"username": "katie", "tweets": ["I love my cat"]},
{"username": "jeff", "tweets": [], "color": "purple"},
{"username": "bob123", "tweets": [], "num": 10, "color": "teal"},
{"username": "doggo_luvr", "tweets": ["dogs are the best", "I'm hungry"]},
{"username": "guitar_gal", "tweets": []}
]
sorted(users, key = len)
# To sort users by their username---for each user in users check the ["username"]
sorted(users,key=lambda user: user['username'])
# Finding our most active users...
# Sort users by number of tweets, descending
sorted(users,key=lambda user: len(user["tweets"]), reverse=True)
sorted(iterable, key=key, reverse=reverse) -> return a new list
iterable Required. The sequence to sort, list, dictionary, tuple etc.
key Optional. A Function to execute to decide the order. Default is None
reverse Optional. A Boolean. False will sort ascending, True will sort descending. Default is False
4. max() and min()
max() # return the largest item in an iterable or the largest of two or more arguments
print(max(1,43,6)) #43
print(max("awesome world")) #w
print(min("awesome world")) #" " ---空格更小
names = ['Arya', "Samson", "Dora", "Tim"]
print(min(names)) #Arya
print(max(names)) #Tim--直接还是按照alphabetally的不是按照字符长度先排
print(min(names, key = lambda n: len(n))) #Tim
print(min(len(n) for n in names)) #3
5.reversed() #not reverse !
reversed(0 # return a reverse iterator
#reverse is just for list, also inplace
#这俩是一样的都是4-0
for x in reversed(range(0,5)) :
print(x)
for x in range(4,-1,-1): #注意range倒数也还是需要顺序反着写,包左不包右依然存在
print(x)
5.len()
'hello'__len__() //double underscore ---you can implement it by yourself; this is called by len() -- define your own len()