Advertisement

Python:去除重复单词并按字母顺序排列字符串

阅读量:

①首先通过split()函数对单词进行分割处理

②接着运用set()函数将重复的单词去除,保留唯一值

③随后借助list()函数将去重后的集合转换为列表形式

④再利用sort()方法对列表内容进行排序操作

⑤采用islower()方法筛选出所有小写字母组成的单词

⑥通过istitle()方法识别并提取出标题格式的单词


复制代码
 print('|{:`^69}|'.format('定义列表并输出'))

    
 list1="hello java world hello go world Hello python World "
    
 list1=list1.split()
    
 list1=set(list1)
    
 list1=list(list1)
    
 print("|排序前:",list1,' '*4,'|')
    
  
    
 print('|{:-^72}|'.format('排序后'))
    
 list1 = sorted(list1,key=lambda x:x[0])
    
 print("|排序后:",list1,' '*4,'|')
    
  
    
 print('|{:-^65}|'.format('题目要求:大写排在小写的后面'))
    
 #筛

全部评论 (0)

还没有任何评论哟~