Python循环遍历2个列表

2021-04-11 14:55:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一些几乎可以工作的代码,但是有一件事我无法理解。在

我需要遍历两个列表。我想循环执行,直到第一个列表完成;而第二个列表应该重复,直到第一个列表完成。在

我想输入数据库列表和流列表。然后我希望代码输出9行代码,替换它有“+stream+”和“+item+”的位置。item部分工作正常,但我不知道如何在遍历数据库列表时输出流列表。我想让水流循环,然后重复。所以它会从1a开始,然后达到3c,然后在1a处重新开始,我希望这个过程一直持续到列表中的最后一个数据库。在

databases=input("Enter databases: ")
streams="1a 2a 3a 1b 2b 3b 1c 2c 3c"
stream="1a"

db_list = databases.split()
streams_list= streams.split()

for item in db_list:
    print("unlink $ORACLE_BASE/admin/" + item + "/backup")
    print("unlink $ORACLE_BASE/admin/" + item + "/dpdump")
    print("unlink $ORACLE_BASE/admin/" + item + "/exp")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/backup")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/dpdump")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/exp")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/backup $ORACLE_BASE/admin/" + item + "/backup")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/dpdump $ORACLE_BASE/admin/" + item + "/dpdump")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/exp    $ORACLE_BASE/admin/" + item + "/exp")

输出如下:

^{pr2}$

我的输出每次都有STREAM1a,但我希望它在1a、2a、3a、1b、2b、3b、1c、2c和3c的模式中循环。如前所述,如果我有超过9个数据库,那么它将再次从1a开始。我怎样才能做到这一点?在

我的代码中唯一没有按我想要的方式工作的部分是它显示“+stream+”。在

2条回答
网友
1楼 ·

您应该使用itertools.cycle。这将允许您以无休止的重复循环获取列表中的元素。在循环开始处添加以下代码:

stream_cycle = itertools.cycle(streams_list)
for item in db_list:
    stream = stream_cycle.next()

或者正如@HughBothwell建议的那样,您可以使用zip并将其包含在for循环的定义中:

^{pr2}$

任何一种方式都将在循环的每次迭代中为您提供下一个流。如果dbs多于streams,那么它将重复。在

网友
2楼 ·

诀窍是使用来自itertools的迭代器,称为cycle

from itertools import cycle
databases=input("Enter databases: ")
streams="1a 2a 3a 1b 2b 3b 1c 2c 3c"
stream="1a"

db_list = databases.split()
stream_list= streams.split()

for item, stream in zip(db_list, cycle(stream_list)):
    print("unlink $ORACLE_BASE/admin/" + item + "/backup")
    print("unlink $ORACLE_BASE/admin/" + item + "/dpdump")
    print("unlink $ORACLE_BASE/admin/" + item + "/exp")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/backup")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/dpdump")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/exp")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/backup $ORACLE_BASE/admin/" + item + "/backup")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/dpdump $ORACLE_BASE/admin/" + item + "/dpdump")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/exp    $ORACLE_BASE/admin/" + item + "/exp")

bonus-使用docopt解析命令行参数

小好处:让您可以调用脚本并从命令行方便地输入paremeters

安装docopt包(仅一次)

^{pr2}$

编写脚本的修改版本数据库

"""Usage:
    db.py -s <stream> (-s <stream>)... <db> <db>...
    db.py -h

For all database names <db> generates commands combining it with
names of <stream>s.
"""
from docopt import docopt
from itertools import cycle   

args = docopt(__doc__)

db_list = args["<db>"]
stream_list = args["<stream>"]

for item, stream in zip(db_list, cycle(stream_list)):
    print("unlink $ORACLE_BASE/admin/" + item + "/backup")
    print("unlink $ORACLE_BASE/admin/" + item + "/dpdump")
    print("unlink $ORACLE_BASE/admin/" + item + "/exp")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/backup")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/dpdump")
    print("mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM" + stream + "/" + item + "/exp")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/backup $ORACLE_BASE/admin/" + item + "/backup")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/dpdump $ORACLE_BASE/admin/" + item + "/dpdump")
    print("ln -s $ORACLE_BASE/acfsmounts/global/STREAM" + stream + "/" + item + "/exp    $ORACLE_BASE/admin/" + item + "/exp")

打印输出使用字符串:

$ python db.py
Usage:
    db.py -s <stream> (-s <stream>)... <db> <db>...
    db.py -h

显示一些帮助字符串:

$ python db.py -h
Usage:
    db.py -s <stream> (-s <stream>)... <db> <db>...
    db.py -h

For all database names <db> generates commands combining it with
names of <stream>s.

并称之为真正的用途:

$ python db.py -s s2 -s s2 db1 db2 db3
unlink $ORACLE_BASE/admin/db1/backup
unlink $ORACLE_BASE/admin/db1/dpdump
unlink $ORACLE_BASE/admin/db1/exp
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db1/backup
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db1/dpdump
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db1/exp
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db1/backup $ORACLE_BASE/admin/db1/backup
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db1/dpdump $ORACLE_BASE/admin/db1/dpdump
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db1/exp    $ORACLE_BASE/admin/db1/exp
unlink $ORACLE_BASE/admin/db2/backup
unlink $ORACLE_BASE/admin/db2/dpdump
unlink $ORACLE_BASE/admin/db2/exp
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db2/backup
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db2/dpdump
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db2/exp
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db2/backup $ORACLE_BASE/admin/db2/backup
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db2/dpdump $ORACLE_BASE/admin/db2/dpdump
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db2/exp    $ORACLE_BASE/admin/db2/exp
unlink $ORACLE_BASE/admin/db3/backup
unlink $ORACLE_BASE/admin/db3/dpdump
unlink $ORACLE_BASE/admin/db3/exp
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db3/backup
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db3/dpdump
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAMs2/db3/exp
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db3/backup $ORACLE_BASE/admin/db3/backup
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db3/dpdump $ORACLE_BASE/admin/db3/dpdump
ln -s $ORACLE_BASE/acfsmounts/global/STREAMs2/db3/exp    $ORACLE_BASE/admin/db3/exp

相关问题