python list的简单命令

来源:互联网 发布:labtool48uxp软件下载 编辑:程序博客网 时间:2024/06/06 12:50

1.修改 ,或者说是替换,可以用函数replace实现:

def censor(text,word):
    text=text.split()
    if word in text:
        text.replace(word,'*'*len(word))
        
            
    text=" ".join(text)
    return text

是一个把text内出现特定word时,替换成*字符的函数,replace的用法就是text.replace(word,'*'*len(word))。‘*’ * len(word)可以将word全部替换成*

个人觉得python很方便的另外一点是利用in这个词,可以利用它来控制字符串内的元素,对付一串数据时加上range()是对付循环的利器。


2.删除和增加

2.1删除函数用del remove和pop都可以,用法以及获得各有不同

2.1.1del: 用来删除特定的第几个元素 del a[i], 删除a列表中的第i+1个元素

2.1.2 remove:

def anti_vowel(text):
    for i in text:
        if i.upper() in "AEIOU" :
            text.remove(i)
    return text

用来找出字符串的元音并将它删除。remove 的用法就是a.remove(i),i是一个特定的值

i.upper() 和i.lower() 可以将字符i的大小写改正,非常简单

2.1.3 pop没怎么用过

a.pop() 


3增加用append

a.append()末尾添加

原创粉丝点击