# python练习 2020/07/08

2020/7/9 5:12:56 人评论 次浏览 分类：学习教程

``````18.要求实现一函数，该函数用于求两个集合的差集，结果集合中包含所有属于第一个集合但不属于第二个集合

def func(set1,set2):
dif_list=[]
for i in set1:
if i not in set2:
dif_list.append(i)

return dif_list

set1={1,2,3,4}
set2={2,3,4,5}
print(func(set1,set2))

19.找出一段句子中最长的单词及其索引位置，以list返回
import string
s = "i am a good boy,huhongqiang!"
for i in s:
if i in string.punctuation:
s=s.replace(i,' ')
print(s)

max_word=sorted(s.split(),key=len,reverse=True)[0]
print(max_word)

max_word_length=len(max_word)

for i in range(len(s)):
if s[i:i+max_word_length]==max_word:
max_length_index=i

print(max_length_index,max_word_length)

#用函数
s = "i am a good boy,huhongqiang!"
def find_word_index(s):
for i in s:
if not i.isalpha() and not i.isspace():
s=s.replace(i,' ')
#print(s)

max_word=sorted(s.split(),key=len,reverse=True)[0]
max_word_length=len(max_word)
for i in range(len(s)):
if s[i:i+max_word_length]==max_word:
max_index=i
return max_index,max_word

print(find_word_index(s))

20.返回序列中的最大数
def find_max_number(s):
if not isinstance(s,(str,list,tuple)):
return False
max_number=0.0
if isinstance(s,(list,tuple)):
for i in s:
if i>max_number:
max_number=i
else:
for i in s:
if float(i)>max_number:
max_number=float(i)

return max_number

print(find_max_number('12345673'))
print(find_max_number((1,3,5,7,5)))
print(find_max_number([3,5,6,4,6]))

``````

暂无相关的资讯...

-->