下面的代码是列出相同索引位置中两个列表中较大数量的列表。 如何使用 while 循环而不是 for 循环重写此代码?
a = [7,12,9,14,15,18,12]
b = [9,14,8,3,15,17,15]
big = []
for i in range(len(a)):
big.append(max(a[i],b[i]))
print(big)
[9, 14, 9, 14, 15, 18, 15]
答案 0 :(得分:2)
您可以使用pop()
每次点到两个列表的第一项,直到a
或b
评估为True(它包含项目):
In [15]: while a:
big.append(max(a.pop(0),b.pop(0)))
....:
In [16]: big
Out[16]: [9, 14, 9, 14, 15, 18, 15]
答案 1 :(得分:1)
使用Private Sub MultiPage1_Change()
' activate the sheet according to the caption of the multi-page selected item
Worksheets(MultiPage1.SelectedItem.Caption).Activate
End Sub
和zip
:
list comprehension
使用a = [7, 12, 9, 14, 15, 18, 12]
b = [9, 14, 8, 3, 15, 17, 15]
big = [max(t) for t in zip(a, b)]
:
while
答案 2 :(得分:1)
其中一种方式是
!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body ng-app="app">
<div ng-controller="Controller">
<table border='1'>
<tr>
<th rowspan="2">Tipo de Contenido</th>
<th colspan="{{aTipoUsuarios.length}}">Tipo de Usuarios</th>
</tr>
<tr>
<td ng-repeat="usu in aTipoUsuarios">{{usu}}</td>
</tr>
</table>
</div>
<script type=" text/javascript " src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js "></script>
<script type="text/javascript " src="MainViewController.js "></script>
</body>
</html>
答案 3 :(得分:0)
你可以试试这个:
a = [7,12,9,14,15,18,12]
b = [9,14,8,3,15,17,15]
big = []
i=0
while i<len(a):
if a[i]<b[i]:
big.append(b[i])
i+=1
else:
big.append(a[i])
i+=1
print(big)
答案 4 :(得分:0)
你也可以试试zip()
并列出理解,如下:
a = [7,12,9,14,15,18,12]
b = [9,14,8,3,15,17,15]
big = [max(c) for c in zip(a,b)]
print big
输出:
[9, 14, 9, 14, 15, 18, 15]