我正在用Python编写一些几乎可以工作的代码,但有一点我无法弄清楚。
我需要遍历2个列表。我想循环直到第一个列表完成;而第二个列表应该重复,直到第一个列表完成。
我想输入数据库列表和流列表。然后我希望代码通过替换“+ stream +”和“+ item +”的位置来输出9行。项目部分工作正常,但我不知道如何在迭代数据库列表时输出流列表。我希望流循环然后重复。所以它将从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")
这是输出:
Enter databases: database1 database2 database3
unlink $ORACLE_BASE/admin/database1/backup
unlink $ORACLE_BASE/admin/database1/dpdump
unlink $ORACLE_BASE/admin/database1/exp
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database1/backup
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database1/dpdump
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database1/exp
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database1/backup $ORACLE_BASE/admin/database1/backup
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database1/dpdump $ORACLE_BASE/admin/database1/dpdump
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database1/exp $ORACLE_BASE/admin/database1/exp
unlink $ORACLE_BASE/admin/database2/backup
unlink $ORACLE_BASE/admin/database2/dpdump
unlink $ORACLE_BASE/admin/database2/exp
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database2/backup
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database2/dpdump
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database2/exp
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database2/backup $ORACLE_BASE/admin/database2/backup
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database2/dpdump $ORACLE_BASE/admin/database2/dpdump
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database2/exp $ORACLE_BASE/admin/database2/exp
unlink $ORACLE_BASE/admin/database3/backup
unlink $ORACLE_BASE/admin/database3/dpdump
unlink $ORACLE_BASE/admin/database3/exp
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database3/backup
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database3/dpdump
mkdir -p /oradba/app/oracle/acfsmounts/global/STREAM1a/database3/exp
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database3/backup $ORACLE_BASE/admin/database3/backup
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database3/dpdump $ORACLE_BASE/admin/database3/dpdump
ln -s $ORACLE_BASE/acfsmounts/global/STREAM1a/database3/exp $ORACLE_BASE/admin/database3/exp
我的输出每次都有STREAM1a,但我希望它循环通过1a,2a,3a,1b,2b,3b,1c,2c和3c的模式。如上所述,如果我有超过9个数据库,那么它将再次从1a开始。我怎样才能做到这一点?
我的代码中唯一没有按照我想要的方式运行的部分是“+ stream +”。
答案 0 :(得分:3)
您应该使用itertools.cycle。这将允许您在无休止的重复循环中获取列表的元素。在循环开始时添加此代码:
stream_cycle = itertools.cycle(streams_list)
for item in db_list:
stream = stream_cycle.next()
或@HughBothwell建议,您可以使用zip并将其包含在for
循环的定义中:
for item, stream in zip(db_list, itertools.cycle(streams_list)):
无论哪种方式都会在循环的每次迭代中为您提供下一个流。如果你有比流更多的dbs,那么它会重复。
答案 1 :(得分: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")
小额奖励:让您调用脚本并方便地从命令行输入参数
安装docopt
包(仅一次)
$ pip install docopt
编写修改后的脚本版本 db.py :
"""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