GVKun编程网logo

python:remove,pop和del方法的区别(python中remove和pop的区别)

4

本篇文章给大家谈谈python:remove,pop和del方法的区别,以及python中remove和pop的区别的知识点,同时本文还将给你拓展301.RemoveInvalidParenthese

本篇文章给大家谈谈python:remove,pop和del方法的区别,以及python中remove和pop的区别的知识点,同时本文还将给你拓展301. Remove Invalid Parentheses 去掉不合理的括号、316. Remove Duplicate Letters and 402. Remove K Digits、centos – 如何撤消“yum remove python”、centos – 服务器在“yum remove python”之后擦除等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

python:remove,pop和del方法的区别(python中remove和pop的区别)

python:remove,pop和del方法的区别(python中remove和pop的区别)

  1. remove
    remove(item)方法是直接对可变序中的元素进行检索删除,返回的是删除后的列表,不返回删除值(返回None)

list1=[1,3,6,7,8]
print list1.remove(3) #对列表元素进行搜索删除,而不是下表
print list1

None
[1,8]

  1. pop
    pop(index)方法是对可变序列中元素下标进行检索删除,返回删除值

list1=[1,8]
print list1.pop(3),#对列表下表进行检索删除
print list1

7
[1,8]

dict1={‘Abby‘:21,‘Bob‘:22,‘cindy‘:21}
print dict1.pop(1)
print dict1

  1. del
    del(list[index])方法是对可变序列中元素下边进行检索删除,不返回删除值

list1=[1,8]
del list[3]
print list1

[1.3,8]

301. Remove Invalid Parentheses 去掉不合理的括号

301. Remove Invalid Parentheses 去掉不合理的括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses ( and ).

Examples:

"()())()" -> ["()()()", "(())()"] "(a)())()" -> ["(a)()()", "(a())()"] ")(" -> [""]


解题过程:

用BFS,因为这样的话,对于一个初始s,遍历它,并减去一个不合理的符号,得到的第一层就是全是n-1长度的,当减去一些后得到一个合理的字符串时,停止剪。那么这个层数就是减去的个数也是最小减去个数得到的结果。

当root层时s有长度n,则到第二层时,有c(n,n-1)个长度为n-1的子串,同时进行验证是时间为O(n-1),因此第一层的时间复杂度为O(n-1)*c(n,n-1);同理第二层的时间复杂度为O(n-2)*c(n-1,n-2)。。。。

一共的时间复杂度为 

T(n) = n x C(n, n) + (n-1) x C(n, n-1) + ... + 1 x C(n, 1) = n x 2^(n-1).


注意下面代码有个地方要注意:

就是当验证到正确的字符串时,要使found等于true,因为这样的话,就等于不用再对队列中的字符串进行剪了,只要把队列中剩下的字符串进行验证就行了。

开始时,我将continue写到if(isvalid(str))的范围里了,这样的话,当队列中的其他字符串取出进行验证发现不正确时,还会继续往下减,这样就不对了,因为已经得到了进行最少步剪的步数的到的结果了。


代码如下:


class Solution {
  • public:
  • bool isvalid(string s){
  • int count = 0;
  • for(int i = 0; i < s.size(); i++){
  • if(s[i] == '(')
  • count++;
  • else if(s[i] == ')')
  • count--;
  • if(count < 0)
  • return false;
  • }
  • return count == 0;
  • }
  • vector<string> removeInvalidParentheses(string s) {
  • set<string> sets;
  • vector<string>res;
  • queue<string>q;
  • q.push(s);
  • sets.insert(s);
  • bool found =false;
  • while(!q.empty()){
  • string str = q.front();
  • q.pop();
  • if(isvalid(str)){
  • res.push_back(str);
  • found = true;
  • }
  • if(found) continue;
  • for(int i = 0; i < s.size(); i++){
  • if(str[i] == '(' || str[i] == ')'){
  • string strs = str.substr(0,i) + str.substr(i+1);
  • if(sets.find(strs) == sets.end()){
  • sets.insert(strs);
  • q.push(strs);
  • }
  • }
  • }
  • }
  • return res;
  • }
  • };




  • 316. Remove Duplicate Letters and 402. Remove K Digits

    316. Remove Duplicate Letters and 402. Remove K Digits

    316 Remove Duplicate

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and 
    only once. You must make sure your result is the smallest in lexicographical order among all possible results.
    
    Given "bcabc"
    Return "abc"
    
    Given "cbacdcbc"
    Return "acdb"
    每个字母只留一个,而且保证字典序最小。
    从前往后扫描,还要往前看一个检出是否删除的,要用stack解。stack里记录的就是待用的字母。
    首先要有一个counter记录下每个字母的总个数,stack删除字母的时候如果后面还有余量,则可以放心删除。
    还有个关键词: once and only once。需要一个数据结构记录是否使用这个字母,可以用boolean。
    
    public class Solution {
        public String removeDuplicateLetters(String s) {
            ArrayDeque<Character> stk = new ArrayDeque<Character>();
            int[] counter = new int[26];
            boolean[] visited = new boolean[26];
            char[] chs = s.toCharArray();
            
            for(char c: chs){
                counter[c - ''a'']++;
            }
            
            
            for(char c : chs){
                counter[c - ''a'']--;
                if(visited[c - ''a'']) continue;
                
                while(!stk.isEmpty() && stk.peekLast() > c && counter[stk.peekLast() -''a''] > 0){
                    visited[stk.peekLast() - ''a''] = false;
                    stk.pollLast();
                }
                stk.addLast(c);
                visited[c-''a''] = true;
            }
            
            StringBuilder sb = new StringBuilder();
            for(char c: stk){
                sb.append(c);
            }
            
            return sb.toString();
        }
    }

    402 Remove K Digits

    Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible
    Input: num = "1432219", k = 3
    Output: "1219"
    Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
    
    Input: num = "10200", k = 1
    Output: "200"
    Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
    
    Input: num = "10", k = 2
    Output: "0"
    Explanation: Remove all the digits from the number and it is left with nothing which is 0.

    stk结构也可以用数组加顶点指针模拟。用法如下。

    public class Solution {
        public String removeKdigits(String num, int k) {
            int digit = num.length() - k;
            char[] stk = new char[num.length()];
            int top = 0;
            for(char c: num.toCharArray()){
                while(top > 0 && stk[top-1] > c && k > 0){
                    k--;
                    top--;
                }
                stk[top++] = c;
            }
            
            int idx = 0;
            while(idx < digit && stk[idx] == ''0'') idx++;
            
            return idx == digit ? "0" : new String(stk, idx, digit-idx);
        }
    }

    centos – 如何撤消“yum remove python”

    centos – 如何撤消“yum remove python”

    我有一个基于Centos 6的VPS与ssh连接.最近想要安装 python 2.7.2,我勇敢地跑了:
    yum remove python

    这显然删除了yum本身正在编写或取决于python.

    现在我不知道该怎么办,更糟糕的是,我是一个完整的Linux新手(命令行或其他).

    运行rpm -iv http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-2.6.5-3.el6.x86_64.rpm给出:

    Retrieving http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-2.6.5-3.el6.x86_64.rpm
    Preparing packages for installation...
            package python-2.6.5-3.el6.x86_64 is already installed

    它的x86_64服务器毫无疑问,因为“uname -a”给出:

    Linux xxxxxx 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27 19:49:27 BST 2011 x86_64 x86_64 x86_64   GNU/Linux

    还有一件事:ls /usr/bin/pyth * -la给出:

    lrwxrwxrwx 1 root root    6 Dec  4 20:31 python2 -> python
    -rwxr-xr-x 2 root root 4864 Nov 12  2010 python2.6
    -rwxr-xr-x 2 root root 4864 Nov 12  2010 python;4edbd894
    从 CentOS 6 mirrors列表中选择一个镜像,然后使用rpm安装所需的软件包.您需要处理依赖项.幸运的是,我有一个安装了CentOS 6 x86_64的裸机.

    我快照VM,运行yum删除python并获得一些有趣的通过一些循环依赖.最后结果:

    # --nodeps: because of circular dependency between python and python-libs
    rpm -i --nodeps http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-libs-2.6.5-3.el6.x86_64.rpm
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-2.6.5-3.el6.x86_64.rpm
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/pygpgme-0.1-18.20090824bzr68.el6.x86_64.rpm
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-pycurl-7.19.0-5.el6.x86_64.rpm 
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-urlgrabber-3.9.1-7.el6.noarch.rpm 
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/rpm-python-4.8.0-12.el6.x86_64.rpm 
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/yum-Metadata-parser-1.1.2-14.1.el6.x86_64.rpm
    # --nodeps: because of circular dependency between yum and yum-plugin-fastestmirror
    rpm -i --nodeps http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.26-11.el6.noarch.rpm
    rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/yum-3.2.27-14.el6.centos.noarch.rpm

    更换任何你想要的镜子.此外,请确保架构匹配.那应该可以做到这一点!

    哦,还有一件事:如果我是你,我会在你完成安装RPM后立即进行yum升级.

    centos – 服务器在“yum remove python”之后擦除

    centos – 服务器在“yum remove python”之后擦除

    我运行命令yum remove python之后发生了一场灾难,现在我再也无法启动服务器了.

    它是如何发生的:我尝试通过我的CentOS 5 VPS上的yum更新一些应用程序,并且命令因为一些奇怪的python 2.4错误而失败.我注意到我的python版本很旧,我尝试通过先删除它重新安装它,所以我做了yum删除python.

    在那之后,它问了我一些关于删除依赖关系的事情,看起来我什么都不想错过,所以我点击了Y.

    所以后果就是我无法执行任何命令.我甚至尝试过cd / var / www,但它说“/usr/bin中不存在命令”.当我使用tab查看文件夹导航建议时,文件结构似乎仍然存在(至少/ var / www位对我来说非常重要).
    之后我尝试重新启动vps(从管理面板,因为重启命令不起作用),现在它不再启动了.

    现在我的问题是:像这样的命令怎么可能像这样毁坏我的服务器?

    我真诚地抱歉:让服务器无法启动/无法使用会让我感到痛苦.

    但是,在阅读时我迷路了:

    After that it asked me something about removing dependencies and it
    looked like nothing I Could miss so I clicked [Y]

    要删除的软件包列表肯定非常庞大,因为python是RHEL / CentOS的重要组成部分.你永远不应该确认一些你不太了解的警告信息.

    正如已经建议的那样,你可以做的最好的事情是通过恢复媒体(即:livecd)启动,提取所需的数据文件,并使用更新的CentOS版本重新安装你的机器(并且因为CentOS 6很老,我强烈建议你在CentOS 7上进行改装.

    关于python:remove,pop和del方法的区别python中remove和pop的区别的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于301. Remove Invalid Parentheses 去掉不合理的括号、316. Remove Duplicate Letters and 402. Remove K Digits、centos – 如何撤消“yum remove python”、centos – 服务器在“yum remove python”之后擦除的相关信息,请在本站寻找。

    本文标签:

    上一篇python 类(3) property

    下一篇python 分支结构 循环结构(python循环结构分为)