结城知花甫,成县,pdf官网
from collections import defaultdict a = defaultdict(int) a[0]
''
a = [1,2,3] b = a a.append(4) b
[1, 2, 3, 4]
a = 7 isinstance(a, float)
False
a = [1,2,3] b = a c = list(a) a is not c
True
s = r'e\\e' print(s)
e\\e
from datetime import datetime a = datetime.now() a.strftime('%m/%d/%Y %H:%M')
'02/09/2018 21:07'
datetime.strptime('20091009', '%Y%m%d')
datetime.datetime(2009, 10, 9, 0, 0)
pass 空操作
range可以用在for循环中,range始终返回迭代器
a = 0 for i in range(4): a += i a
6
x = 5 'positive' if x > 0 else 'negative'
'positive'
tup = (1, 2, 3) a, b, c = tup b
2
a = (2,3,4,4,4,5) a.count(4)
3
b = ['a', 'b', 'c'] b.append('d') b
['a', 'b', 'c', 'd']
b.pop(2) b
['a', 'b', 'd']
b.insert(2, 'c') b
['a', 'b', 'c', 'd']
b.remove('d') b
['a', 'b', 'c']
b.extend(('d', 'e')) b
['a', 'b', 'c', 'd', 'e']
del b[1] b
python切片方式:数中间的空格
切片第二个冒号为步长
za = [1,2,3,4,5] za[::2]
[1, 3, 5]
za[::-1]
[5, 4, 3, 2, 1]
aa = dict((v, i) for i, v in enumerate(b)) aa
{'e': 1, 'w': 0}
sorted(range(4))
[0, 1, 2, 3]
list(reversed(range(4)))
[3, 2, 1, 0]
a = ['a','d'] b = ['w', 'e'] for i in zip(a, b): print(i)
('a', 'w') ('d', 'e')
c = dict(zip(range(5), reversed(range(5)))) c
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
c.update({'d' : 'ww'}) c
{0: 4, 1: 'ww', 2: 2, 3: 1, 4: 0, 5: 'ww', 'd': 'ww'}
from collections import defaultdict count = defaultdict(lambda:4) count['w']
4
strings = ['foo', 'card', 'bar', 'aaaa', 'abab'] strings.sort(key = lambda x : len(set(list(x)))) strings
['aaaa', 'foo', 'abab', 'bar', 'card']
tuples = [(1,2,3), (4,5,6), (7,8,9)] [x for tup in tuples for x in tup]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[[x for x in tup] for tup in tuples]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
somedict = {'a' : 1, 'b' : 2} for key in somedict: print(key)
a b
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
python求numpy中array按列非零元素的平均值案例
网友评论