20191114Python打卡习--dict,set和String常用方法
dict
dict是一系列无序的成对的对象,一个是key,一个是value. key不可重复。key和value可以是不同的对象,key必须是不可变对象。
dict可以添加或者删除。
ab = {'smaroop':'smaroop@smaroopch.com', 'Larry':'larry@wall.org', 'Matsumoto':'matz@ruby-lang.org', 'Spamer':'spammer@hotmail.com'} print("smaroop's address is {}".format(ab['smaroop'])) del ab['smaroop'] print('There are {} contact in the address book'.format(len(ab))) print('Name list is %s'%','.join(ab)) for name,address in ab.items(): print('Contact {} at {}'.format(name,address)) ab['Guido']= 'guido@python.org' if 'Guido' in ab: print('Guido\'s address is {}'.format(ab['Guido']))
输出:
smaroop’s address is smaroop@smaroopch.com
There are 3 contact in the address book
Name list is Spamer,Matsumoto,Larry
Contact Spamer at spammer@hotmail.com
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Guido’s address is guido@python.org
set
具有一组对象,无顺序,不可重复。
可以添加和删除。
其中的元素不需要是同类型,但必须是不可变对象。
bri = set(['brazil','russia','india']) bri = {'brazil','russia','india'} print('india' in bri) print('usa' in bri)
输出:
True
False
bric = bri.copy() bric.add('china') print(bric) bric.issuperset(bri)
输出:
{‘china’, ‘russia’, ‘india’, ‘brazil’}
True
bric.remove('russia') print(bric)
输出:
{‘china’, ‘india’, ‘brazil’}
bri & bric
输出:
{‘brazil’, ‘india’}
bric | bric
输出:
{‘brazil’, ‘china’, ‘india’}
set,list,tuple区别
set:无序,不重复,可修改
list:有序,可重复,可修改
tuple:有序,可重复,不可修改
String的一些方法
join():
split():
find():
in():
name = 'Swaroop' if name.startswith('Swa'): print('Yes, the string start with "Swa"') if 'a' in name: print('Yes, it conains the string"a"') pos = name.find("war") if pos != -1: print('position of the string "war" is %d' %pos) print('the string repace war to preace is %s' %name.replace('war','peace')) print('upper case: is %s' % name.upper()) print('lower case : is %s' %name.lower()) delimiter = '_*_' mylist = ['Brazil','Russia','India','China'] print(delimiter.join(mylist)) print(delimiter.join(mylist).split(delimiter)) print(delimiter.join(mylist).split('*'))
输出:
Yes, the string start with “Swa”
Yes, it conains the string"a"
position of the string “war” is 1
the string repace war to preace is Speaceoop
upper case: is SWAROOP
lower case : is swaroop
Brazil_RussiaIndia*China
[‘Brazil’, ‘Russia’, ‘India’, ‘China’]
['Brazil’, ‘Russia’, ‘India’, ‘_China’]
练习
找出以下字符串里含有多少个国家,各国家代码如下:
国家代码: ‘+30’:‘Greece’,’+45’:‘Demmark’,’+51’:‘Peru’,’+65’:‘Singapore’,’+86’:‘China’
字符串:+86 11233 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935’
area_code = {'+30':'Greece','+45':'Demmark','+51':'Peru','+65':'Singapore','+86':'China'} phone_str = '+86 11233 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935' for code,name in area_code.items(): if phone_str.find(code) != -1: print('Phone include name is %s' %name) numbers = phone_str.split() country_set = set([area_code [n] for n in numbers if n.startswith('+')]) print(country_set)
输出:
Phone include name is China
Phone include name is Singapore
Phone include name is Peru
Phone include name is Greece
{‘Greece’, ‘Singapore’, ‘Peru’, ‘China’}
- 点赞
- 收藏
- 分享
- 文章举报
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 的基本方法
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python基本数据类型list,tuple,set,dict用法以及遍历方法
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python中set集合常用方法
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python string 常用方法
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python---字典dict的常用方法和操作
- Python全栈之路:集合set常用方法
- python基础31[list+tuple+set+dict+str+file的成员方法]
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python基础31[list+tuple+set+dict+str+file的成员方法]
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python实现字典(dict)和字符串(string)的相互转换方法
- python---集合set常用方法和操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结