python中两个for循环之间的交替执行

时间:2017-09-11 13:08:08

标签: python

我有两个包含应该连接在一起的值的字符串,这意味着string1中的值为1,string2中的值为2。这是我尝试过的代码:

/* ==========================================================================
   7. How It Works Section
========================================================================== */
.how-it-works {
    background: #f5f5fa;
	padding-bottom: 30px;
}

.board{
	/*width: 75%;
	margin: 60px auto;
	margin-bottom: 0;
	box-shadow: 10px 10px #ccc,-10px 20px #ddd;*/
}
.board .nav-tabs {
	position: relative;
	/* border-bottom: 0; */
	/* width: 80%; */
	margin: 40px auto;
	margin-bottom: 0;
	box-sizing: border-box;

}

.board > div.board-inner > .nav-tabs {
	border: none;
}

p.narrow{
	width: 60%;
	margin: 10px auto;
}

.liner{
	height: 2px;
	background: #ddd;
	position: absolute;
	width: 80%;
	margin: 0 auto;
	left: 0;
	right: 0;
	top: 50%;
	z-index: 1;
}

.nav-tabs > li.active > a, .nav-tabs > li.active > a:hover, .nav-tabs > li.active > a:focus {
	color: #555555;
	cursor: default;
	/* background-color: #ffffff; */
	border: 0;
	border-bottom-color: transparent;
	outline: 0;
}

span.round-tabs{
	width: 70px;
	height: 70px;
	line-height: 70px;
	display: inline-block;
	border-radius: 100px;
	background: white;
	z-index: 2;
	position: absolute;
	left: 0;
	text-align: center;
	font-size: 25px;
}

span.round-tabs.one{
	border: 2px solid #ddd;
	color: #ddd;
}

li.active span.round-tabs.one, li.active span.round-tabs.two, li.active span.round-tabs.three, li.active span.round-tabs.four, li.active span.round-tabs.five {
	background: #0072c6 !important;
	border: 2px solid #0072c6;
	color: #fff;
}
li.active span.round-tabs.one h4, li.active span.round-tabs.two h4, li.active span.round-tabs.three h4, li.active span.round-tabs.four h4, li.active span.round-tabs.five h4{
	color: #0072c6;
}
span.round-tabs.two{
	border: 2px solid #ddd;
	color: #ddd;
}

span.round-tabs.three{
	border: 2px solid #ddd;
	color: #ddd;
}

span.round-tabs.four{
	border: 2px solid #ddd;
	color: #ddd;
}

span.round-tabs.five{
	border: 2px solid #ddd;
	color: #ddd;
}

.nav-tabs > li.active > a span.round-tabs{
	background: #fafafa;
}
.nav-tabs > li {
	width: 20%;
}

.nav-tabs > li a{
	width: 70px;
	height: 70px;
	margin: 20px auto;
	border-radius: 100%;
	padding: 0;
}
.nav-tabs>li>a:hover {border-color: transparent !important;}
.nav>li>a:focus, .nav>li>a:hover {background-color: transparent;}
.nav>li>a:focus, .nav>li>a:hover {text-decoration: none; background-color: transparent !important;}
.nav>li>a:focus, .nav>li>a:hover {background-color: transparent !important;}
.nav-tabs>li.active>a, .nav-tabs>li.active>a:focus, .nav-tabs>li.active>a:hover {border:none !important;}
.nav-tabs > li a:hover{
	background: transparent !important;
}
.tab-content{
}
.tab-pane{
	position: relative;
	padding-top: 50px;
}

.btn-outline-rounded{
	padding: 10px 40px;
	margin: 20px 0;
	border: 2px solid transparent;
	border-radius: 25px;
}

.btn.green{
	background-color:#69cb95;
	/*border: 2px solid #5cb85c;*/
	color: #ffffff;
}

@media( max-width : 585px ){

	.board {
		width: 90%;
		height:auto !important;
	}
	span.round-tabs {
		font-size:16px;
		width: 50px;
		height: 50px;
		line-height: 50px;
	}
	.tab-content .head{
		font-size:20px;
	}
	.nav-tabs > li a {
		width: 50px;
		height: 50px;
		line-height:50px;
	}

	li.active:after {
		content: " ";
		position: absolute;
		left: 35%;
	}

	.btn-outline-rounded {
		padding:12px 20px;
	}
}

我收到了这个错误:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-md-12 board">
    <!-- <h2>Welcome to IGHALO!<sup>™</sup></h2>-->
    <div class="board-inner">
        <ul class="nav nav-tabs" id="myTab">
            <div class="liner"></div>
            <li class="active">
                <a href="#" aria-controls="home" id="DivPatientDetails" name="PatientDetails" >
                    <span class="round-tabs one">
                        <i class="icon icon-profile-male"></i>01
                        <h4>Patient's Details</h4>
                    </span>
                  
                </a>
            </li>
            <li>
                <a href="#" aria-controls="profile" id="DivPatientEligibility" name="PatientEligibility" class="disabled">
                    <span class="round-tabs two">
                        <i class="icon icon-pencil"></i>02
                        <h4>Patient's Eligibility</h4>
                    </span>
                </a>
            </li>
            <li>
                <a href="#" aria-controls="prototyping" id="DivPatientDeclaration" name="PatientDeclaration" class="disabled">
                    <span class="round-tabs three">
                        <i class="icon icon-layers"></i>03
                        <h4>Patient's Declaration</h4>
                    </span>
                </a>
            </li>
 </ul>
    </div>
</div>

3 个答案:

答案 0 :(得分:2)

你可以使用zip,它返回一个迭代器(在Python 2中只是一个列表),它会使用指定来源的值组合出来的元组:

str1 = 'John,Gabriel,Isaac,Albert'
str2 = 'Cena,Heins,Newton,Einstein'

for x, y in zip(str1.split(','), str2.split(',')):
    print(x + y)

答案 1 :(得分:1)

要解决实际问题,请注意以下几行:

for x,y in str1.split(','),str2.split(','):

不代表你的意图。

有两件事正在发生。

for i in s:

迭代s的元素,将它们分配给i。在您的情况下s是:

>>> str1.split(','),str2.split(',')
(['John', 'Gabriel', 'Isaac', 'Albert'], ['Cena', 'Heins', 'Newton', 'Einstein'])

即,一系列2个元素,每个元素都是一个列表。所以你的循环会迭代两次,每次都将i设置为其中一个列表。

最重要的是,您还尝试解压缩i,有效地尝试将x, y分配给其中一个列表,与此相同:

>>> x, y = ['John', 'Gabriel', 'Isaac', 'Albert']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

这不起作用,因为右侧的值比左侧的名称多。

解决方案是构造一个新序列,其中包含每个列表中的值对,并使用zip对其进行迭代,正如其他人所说的那样。

答案 2 :(得分:1)

如果每个字符串中的字段数可能不同,您可以对Python 3使用itertools.zip_longest(),或在Python 2中使用itertools.izip_longest()

from itertools import zip_longest

str1 = "John,Gabriel,Isaac,Albert,Aristotle"
str2 = "Cena,Heins,Newton,Einstein"

for firstname, surname in zip_longest(str1.split(','), str2.split(','), fillvalue=''):
    print('{}{}{}'.format(firstname, ' ' if firstname and surname else '', surname))

输出:

John Cena
Gabriel Heins
Isaac Newton
Albert Einstein
Aristotle

否则,如果您确定名称始终是配对的,则可以使用zip()