您的位置:首页 > 编程语言 > Python开发

20191114Python打卡习--dict,set和String常用方法

2020-01-13 05:09 357 查看

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’}

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Land欧阳 发布了15 篇原创文章 · 获赞 0 · 访问量 424 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: