① Python中的Sortpython
Python中的內建排序函數有 sort()和sorted()兩個函數
list.sort(func=None, key=None, reverse=False(or True))spa
e.g..net
>>>
list
=
[
2
,
8
,
4
,
6
,
9
,
1
,
3
]
code
>>>
list
.sort()
blog
>>>
list
[
1
,
2
,
3
,
4
,
6
,
8
,
9
]
e.g.排序
>>>
list
=
[
2
,
8
,
4
,
1
,
5
,
7
,
3
]
rem
>>> other
=
sorted
(
list
)
get
>>> other
[
1
,
2
,
3
,
4
,
5
,
7
,
8
]
sort()方法僅定義在list中,而sorted()方法對全部的可迭代序列都有效博客
sorted()不會改變原來的list,而是會返回一個新的已經排序好的list
②dataframe 訪問元素
貼一篇寫的很好的博客:https://blog.csdn.net/wr339988/article/details/65446138/
③Python中的集合
用途:
建立:
s
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
} (可變集合)
注意:建立空集合,必須用set(),不能僅定義s={},這樣默認定義的是字典
n_set_test
=
frozenset
(set_test) (不可變集合)
添加|刪除 元素:
add 向集合中添加元素 s.add(1)
clear 清空集合 s.clear()
copy 返回集合淺拷貝 ss=s.copy()
remove 刪除指定元素 s.remove(3)
pop 隨機刪除一個元素 s.pop()
集合間運算:
子集:issubset()
>>> C < A
True
# C 是 A 的子集
>>> C.issubset(A)
True
>>> A | B
{
'c'
,
'b'
,
'f'
,
'd'
,
'e'
,
'a'
}
>>> A.union(B)
{
'c'
,
'b'
,
'f'
,
'd'
,
'e'
,
'a'
}
>>> A & B
{
'c'
,
'd'
}
>>> A.intersection(B)
{
'c'
,
'd'
}
>>> A
-
B
{
'b'
,
'a'
}
>>> A.difference(B)
{
'b'
,
'a'
}
>>> A ^ B
{
'b'
,
'f'
,
'e'
,
'a'
}
>>> A.symmetric_difference(B)
{
'b'
,
'f'
,
'e'
,
'a'
}