Python获取嵌套列表的元素下标

题目可以描述为:

已经定义如下列表:

lista = [1, 8, [4, 5, 8, 5, 8, 7, 6], [3, [10, 8, 11, [12, 13, 8], 8]], 8]

求元素8在嵌套列表lista中的下标。

解决方法——递归:

lista = [1, 8, [4, 5, 8, 5, 8, 7, 6], [3, [10, 8, 11, [12, 13, 8], 8]], 8]


def judge_find(templist, pathlist, x):
    for i in pathlist:
        templist = templist[i]
        if templist == x:
            return True
    else:
        return False


def find_multiple_index(originlist, templist, x, pathlist, resultlist):
    for i in range(len(templist)):
        if templist[i] == x:
            pathlist.append(i)
            resultlist.append(tuple(pathlist[:]))
            pathlist.pop()
        else:
            if type(templist[i]) is list:
                pathlist.append(i)
                if not find_multiple_index(originlist, templist[i], x, pathlist, resultlist):
                    if len(pathlist) != 0:
                        pathlist.pop()

pathlist = []
resultlist = []
find_multiple_index(lista[:], lista, 8, pathlist, resultlist)
print(resultlist)

运行结果:

>> long@mylife:~/Downloads$ python3 tree_find_any_position.py
>> [(1,), (2, 2), (2, 4), (3, 1, 1), (3, 1, 3, 2), (3, 1, 4), (4,)]

Ubuntu修改默认程序

Ubuntu默认的PDF阅读器Evince没有KDE内置的Okular功能强大,安装上Okular后,我们需要修改系统默认程序。

修改方法如下:

>> sudo gedit /etc/gnome/defaults.list

application/pdf=evince.desktop 替换成 application/pdf=okular.desktop

如果需要替换其他功能的默认程序,按照类似的操作即可。

Ubuntu W: GPG error NO_PUBKEY

Ubuntu系统在执行sudo apt-get update之后,可能会出现如下错误:

>> W: GPG error: http://extras.ubuntu.com trusty Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 16126D3A3E5C1192

解决办法:

>> gpg --keyserver keyserver.ubuntu.com --recv 16126D3A3E5C1192
>> gpg --export --armor 16126D3A3E5C1192 | sudo apt-key add -

Ubuntu修改启动动画

1、安装启动动画

>> long@happytime:~$ sudo apt-get install plymouth-theme-*

2、修改默认启动动画

>> long@happytime:~$ sudo update-alternatives --config default.plymouth

执行之后可以得到所有可用的启动动画,如下:

>> 有 15 个候选项可用于替换 default.plymouth (提供 /lib/plymouth/themes/default.plymouth)。
>>  选择       路径                                                             优先级  状态
>> ------------------------------------------------------------
>>   0            /lib/plymouth/themes/edubuntu-logo/edubuntu-logo.plymouth           150       自动模式
>>   1            /lib/plymouth/themes/edubuntu-logo/edubuntu-logo.plymouth           150       手动模式
>>   2            /lib/plymouth/themes/fade-in/fade-in.plymouth                       10        手动模式
>>   3            /lib/plymouth/themes/glow/glow.plymouth                             10        手动模式
>>   4            /lib/plymouth/themes/kubuntu-logo/kubuntu-logo.plymouth             150       手动模式
>>   5            /lib/plymouth/themes/lubuntu-logo/lubuntu-logo.plymouth             150       手动模式
>>   6            /lib/plymouth/themes/numix/numix.plymouth                           100       手动模式
>>   7            /lib/plymouth/themes/sabily/sabily.plymouth                         60        手动模式
>>   8            /lib/plymouth/themes/script/script.plymouth                         10        手动模式
>> * 9            /lib/plymouth/themes/solar/solar.plymouth                           10        手动模式
>>   10           /lib/plymouth/themes/spinfinity/spinfinity.plymouth                 10        手动模式
>>   11           /lib/plymouth/themes/ubuntu-gnome-logo/ubuntu-gnome-logo.plymouth   150       手动模式
>>   12           /lib/plymouth/themes/ubuntu-logo/ubuntu-logo-scale-2.plymouth       99        手动模式
>>   13           /lib/plymouth/themes/ubuntu-logo/ubuntu-logo.plymouth               100       手动模式
>>   14           /lib/plymouth/themes/ubuntustudio-logo/ubuntustudio-logo.plymouth   150       手动模式
>>   15           /lib/plymouth/themes/xubuntu-logo/xubuntu-logo.plymouth             150       手动模式
>>
>> 要维持当前值[*]请按回车键,或者键入选择的编号:

3、输入编号

>> 要维持当前值[*]请按回车键,或者键入选择的编号:10
>> update-alternatives: using /lib/plymouth/themes/spinfinity/spinfinity.plymouth to provide /lib/plymouth/themes/default.plymouth (default.plymouth) in 手动模式

4、更新设置

>> sudo update-initramfs -u

重启即可看到切换之后的启动画面。

忘记Mysql密码

1、修改配置文件

>> long@happytime:~$ sudo emacs /etc/mysql/my.cnf

在[mysqld]部分加入: skip-grant-tables,然后保存退出。

2、重新启动mysql服务

>> sudo service mysql restart

3、进入mysql修改密码

>> long@happytime:~$ mysql
>> Welcome to the MySQL monitor.  Commands end with ; or \g.
>> Your MySQL connection id is 36
>> Server version: 5.5.43-0ubuntu0.14.04.1 (Ubuntu)
>>
>> Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
>>
>> Oracle is a registered trademark of Oracle Corporation and/or its
>> affiliates. Other names may be trademarks of their respective
>> owners.
>>
>> Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
>>
>> mysql> use mysql;
>> Reading table information for completion of table and column names
>> You can turn off this feature to get a quicker startup with -A
>>
>> Database changed
>> mysql> update user set Password=password('newpassword') where User='root';
>> Query OK, 4 rows affected (0.00 sec)
>> Rows matched: 4  Changed: 4  Warnings: 0
>>
>> mysql> flush privileges;
>> Query OK, 0 rows affected (0.00 sec)
>>
>> mysql> quit
>> Bye

4、记得把配置文件改回来

5、重启mysql服务