python | 基本数据类型函数
mark一下
Sequence
Operations on Any Sequence
Operation Name | Operator | Explanation |
---|---|---|
indexing | [ ] | Access an element of a sequence |
concatenation | + | Combine sequences together |
repetition | * | Concatenate a repeated number of times |
membership | in | Ask whether an item is in a sequence |
length | len | Ask the number of items in the sequence |
slicing | [ : ] | Extract a part of a sequence |
List
Methods Provided by Lists
Method Name | Use | Explanation |
---|---|---|
append | a_list.append(item) | Adds a new item to the end of a list |
insert | a_list.insert(i,item) | Inserts an item at the i^th position in a list |
pop | a_list.pop() | Removes and returns the last item in a list |
pop | a_list.pop(i) | Removes and returns the i^th item in a list |
sort | a_list.sort() | Modifies a list to be sorted |
reverse | a_list.reverse() | Modifies a list to be in reverse order |
del | del a_list[i] | Deletes the item in the i t h i^th ith position |
index | a_list.index(item) | Returns the index of the first occurrence of item |
count | a_list.count(item) | Returns the number of occurrences of item |
remove | a_list.remove(item) | Removes the first occurrence of item |
String
Methods Provided by Strings
Method Name | Use | Explanation |
---|---|---|
center | a_string.center(w) | Returns a string centered in a field of size w |
count | a_string.count(item) | Returns the number of occurrences of item in the string |
ljust | a_string.ljust(w) | Returns a string left-justified in a field of size w |
lower | a_string.lower() | Returns a string in all lowercase |
rjust | a_string.rjust(w) | Returns a string right-justified in a field of size w |
find | a_string.find(item) | Returns the index of the first occurrence of item |
split | a_string.split(s_char) | Splits a string into substrings at s_char |
Set
Operations on a Set
Operator | Use | Explanation |
---|---|---|
in | x.in(set) | Set membership |
len | len(set) | Returns the cardinality (i.e. the length) of the set |
| | set1 | set2 | Returns a new set with all elements from both sets |
& | set1& set2 | Returns a new set with only the elements common to both sets |
- | set1 - set2 | Returns a new set with all items from the first set not in second |
<= | set1<=set2 | Asks whether all elements of the first set are in the second |
Methods Provided by Sets
Method Name | Use | Explanation |
---|---|---|
union | set1.union(set2) | Returns a new set with all elements from both sets |
intersection | set1.intersection(set2) | Returns a new set with only the elements common to both sets |
difference | set1.difference(set2) | Returns a new set with all items from first set not in second |
issubset | set1.issubset(set2) | Asks whether all elements of one set are in the other |
add | set.add(item) | Adds item to the set |
remove | set.remove(item) | Removes item from the set |
pop | set.pop() | Removes an arbitrary element from the set |
clear | set.clear() | Removes all elements from the set |
Dict
Operators Provided by Dictionaries
Operator | Use | Explanation |
---|---|---|
[] | my_dict[k] | Returns the value associated with k, error otherwise |
in | key in my_dict | Returns True if key is in the dictionary, False otherwise |
del | del my_dict[key] | Removes the entry from the dictionary |
Methods Provided by Dictionaries
Method Name | Use | Explanation |
---|---|---|
keys | my_dict.keys() | Returns the keys of the dictionary in a dict_keys object |
values | my_dict.values() | Returns the values of the dictionary in a dict_values object |
items | my_dict.items() | Returns the key-value pairs in a dict_items object |
get | my_dict.get(k) | Returns the value associated with k, None otherwise |
get | my_dict.get(k,alt) | Returns the value associated with k, alt otherwise |