字符串、字典、集合小结
发布日期:2021-05-14 15:55:21 浏览次数:20 分类:精选文章

本文共 5409 字,大约阅读时间需要 18 分钟。

������������Python���������������2

1. ���������

1.1 ������������������������������

capitalize()

���������������������������������������������������������������������

s = 'dOG'
print(s.capitalize()) # ������: Dog

���������������������

lower()

������������������������������������������������������������

s = "DAXIExiaoxie"
print(s.lower()) # ������: daxiexiaoxie
print(s.upper()) # ������: DAXIEXIAOXIE
print(s.swapcase()) # ������: daxieXIAOXIE
count()

������������������������������������������������������������

s = 'ABabacab'
print(s.count('ab')) # ������: 2
endswith/

���������������������������������������������������

s = "DAXIExiaoxie"
print(s.endswith('ie')) # ������: True
print(s.endswith('xi')) # ������: False
print(s.startswith('DA')) # ������: True
print(s.startswith('da')) # ������: False
isnumeric()

���������������������������������������������

s = '12345'
print(s.isnumeric()) # ������: True
s += 'a'
print(s.isnumeric()) # ������: False
find()���rfind()

������������������������������������������

s = "DAXIExiaoxie"
print(s.find('xi')) # ������: 5
print(s.find('ix')) # ������: -1
print(s.rfind('xi')) # ������: 9
ljust()���rjust()

������������������������������������������������������������������

s = 'abcd'
print(s.ljust(8, 'z')) # ������: abcdzzzz
print(s.rjust(8, 'z')) # ������: zzzzabcd
partition()���rpartition()

���������������������������������������������

s = ' I Love Python '
print(s.strip().partition('o')) # ������: ('I L', 'o', 've Python')
print(s.strip().partition('m')) # ������: ('I Love Python', '', '')
print(s.strip().rpartition('o')) # ������: ('I Love Pyth', 'o', 'n')
replace()

���������������������������������������������������������������������

s = ' I Love Python '
print(s.strip().replace('I', 'We')) # ������: We Love Pytho
split()

������������������������������������������������������

s = "www.baidu.com"
print((s.split(".", 1))) # ������: ['www', 'baidu.com']
print((s.split(".", 2)[1])) # ������: baidu
s1, s2, s3 = s.split(".", 2)
print(s1) # www
print(s2) # baidu
print(s3) # com
splitlines()

���������������������������������

s = 'I \n Love \n Python'
print(s.splitlines()) # ������: ['I ', ' Love ', ' Python']
print(s.splitlines(True)) # ������: ['I \n', ' Love \n', ' Python']

1.2 ������������������

format()

������������������������������������������������������������������

s = "{0} Love {1}".format('I', 'Python')    # ������: I Love Python
s = "{a} Love {b}".format(a='I', b='Python') # ������: I Love Pythn
s = "{0} Love {b}".format('I', b='Python') # ������: I Love Lsgroups

2. ������������������������������

������������������

������ id() ��� hash() ������������������������������������������

������������������ id()

���������������������id���������������������������������������������id���������������

i = 1
print(id(i)) # ������: 140729976002208
i = i + 2
print(id(i)) # ������: 140729976002272
lst = [1, 2]
print(id(lst)) # ������: 2503130251328
lst.append(3)
print(id(lst)) # ������: 2503130251328

������������������ hash()

���������������������������������������������������������������������

print(hash('hello'))    # 7468458903063305606
print(hash((1, 2, 'Python'))) # 2319195748284645456
print(hash([1, 2, 'Python'])) # TypeError: unhashable type: 'list'
print(hash({1, 2, 3})) # TypeError: unhashable type: 'set'

3. ������

3.1 ������������������������

������������������������������������������������������

dic = { 'a': 1, 'b': 2, 'c': 3 }
print(dic) # {'a': 1, 'b': 2, 'c': 3}
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dic1 = dict(zip(keys, values))
print(dic1) # {'a': 1, 'b': 2, 'c': 3}

3.2 ���������������������

������������

dic.get(key, default=None)    # ���������������������������������������������
dic.setdefault(key, default=None) # ������������������������������������������������default
# ������������
dic.pop(key, default=None) # ������������������������������������������
del dic[key] # ���������������������
dic.popitem() # ���������������������������
# ������������
dic.clear() # ���������������������
# ������������
dic.copy() # ���������������

������������

dic.update(dict2)    # ���������������������������������������

4. ������

4.1 ������������������������

������������������������������������������������������

a = set()
a.add('b')
a.add('c')
a.add('c')
print(a) # {'b', 'c'}
a = set('abracadabra')
print(a) # {'a', 'c', 'd', 'b', 'r'}

4.2 ���������������������

������������

a.add('element')    # ������������
a.update(other_set) # ������������
# ������������
a.remove('element') # ������������
a.discard('element') # ������������������
# ������������
a.pop() # ������������������������
# ������������
a.intersection(other_set) # ������������
a.intersection_update(other_set) # ���������������������������������
# ������������
a.union(other_set) # ������������
# ������������
a.difference(other_set) # ������������
a.difference_update(other_set) # ���������������������������������
# ������������
a.symmetric_difference(other_set) # ���������������
a.symmetric_difference_update(other_set) # ���������������������������������

������������������������������Python���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:函数、类和对象、魔法方法小结
下一篇:利用位运算求“只出现一次的数字”

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月09日 13时39分01秒