Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python循环遍历2个列表_Python_List_Loops_Output - Fatal编程技术网

Python循环遍历2个列表

Python循环遍历2个列表,python,list,loops,output,Python,List,Loops,Output,我正在用Python编写一些几乎可以正常工作的代码,但有一件事我无法理解 我需要循环浏览2个列表。我想循环直到第一个列表完成;而第二个列表应该重复,直到第一个列表完成 我想输入数据库列表和流列表。然后我希望代码通过替换其中的“+stream+”和“+item+”来输出这9行代码。item部分工作正常,但我不知道在遍历数据库列表时如何输出流列表。我想让水流循环,然后重复。所以它将从1a开始,然后达到3c,然后在1a重新开始。我希望这种情况发生,直到它到达列表中的最后一个数据库 databases=

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

我需要循环浏览2个列表。我想循环直到第一个列表完成;而第二个列表应该重复,直到第一个列表完成

我想输入数据库列表和流列表。然后我希望代码通过替换其中的“+stream+”和“+item+”来输出这9行代码。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")
以下是输出:

输入数据库:数据库1数据库2数据库3
取消$ORACLE_BASE/admin/database1/backup的链接
取消$ORACLE_BASE/admin/database1/dpdump的链接
取消$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\u BASE/acfsmounts/global/STREAM1a/database1/exp$ORACLE\u BASE/admin/database1/exp
取消$ORACLE_BASE/admin/database2/backup的链接
取消$ORACLE_BASE/admin/database2/dpdump的链接
取消$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\u BASE/acfsmounts/global/STREAM1a/database2/exp$ORACLE\u BASE/admin/database2/exp
取消$ORACLE_BASE/admin/database3/backup的链接
取消$ORACLE_BASE/admin/database3/dpdump的链接
取消$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\u BASE/acfsmounts/global/STREAM1a/database3/exp$ORACLE\u BASE/admin/database3/exp
我的输出每次都是1a流,但我希望它在1a、2a、3a、1b、2b、3b、1c、2c和3c的模式中循环。如前所述,如果我有9个以上的数据库,那么它将再次从1a开始。我怎样才能做到这一点

我的代码中唯一没有按我所希望的方式工作的部分是它在哪里说“+stream+”

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

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)):
无论哪种方式,都会在循环的每次迭代中为您提供下一个流。如果您的数据库多于流,那么它将重复。

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

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)):

无论哪种方式,都会在循环的每次迭代中为您提供下一个流。如果您的数据库多于流,那么它将重复。

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

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解析命令行参数 小好处:让您调用脚本并方便地从命令行输入参数

安装
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")

诀窍是使用来自
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解析命令行参数 小好处:让您调用脚本并方便地从命令行输入参数

安装
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")

您想让它在
db_列表
中的每个项目的所有
流中循环吗?例如,
database1a
database12a
database13a
,等等?是的,这正是我想要的。当它到达数据库9时,它将处于3c。当它到达数据库10时,它需要返回1a。可能是一个生成器功能您希望它在
db\u列表中每个项目的
流中循环吗?例如,
database1a
database12a
database13a
,等等?是的,这正是我想要的。当它到达数据库9时,它将处于3c。当它到达数据库10时,它需要返回1a。可能是一个生成器功能。。。或者
对于项目,在zip中流(db_列表,itertools.cycle(stream)):
很好的建议。我在中添加了它。我得到了错误:ImportError:无法导入名称“izip”@user2554129是的,
izip
在Python 3中不存在。这就是为什么我在示例中使用了
zip
,而不是
izip
。您也不需要导入它。这非常有效!谢谢!:)如果有人感兴趣,我有没有办法分享我完成的代码?。。。或者
对于项目,在zip中流(db_列表,itertools.cycle(stream)):
很好的建议。我已将其添加到中。我得到错误:ImportError:无法导入名称“izip”