我有一个数组和一个输入,如果我输入一些我想用<html>
<head>
<title>Typehead</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.2/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.2/angular-animate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.1.2/ui-bootstrap-tpls.min.js"></script>
</head>
<body ng-app="ui.bootstrap.demo" >
<style>
.full button span {
background-color: limegreen;
border-radius: 32px;
color: black;
}
.partially button span {
background-color: orange;
border-radius: 32px;
color: black;
}
.appointment>button {
color: white;
background-color: red;
}
</style>
<div ng-controller="DatepickerDemoCtrl">
<pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>
<h4>Popup</h4>
<div class="row">
<div class="col-md-6">
<p class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" ng-change="dateSelected()"is-open="popup1.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" date-disabled="disabled(date, mode)" custom-class="dayClass(date, mode)" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
</div>
</body>
</html>
和我的数组的东西,例如,如果我有这个:
.startswith()
如果我输入Array = ['foo','bar']
,我希望它与"fo"
匹配,然后返回索引,在本例中为"foo"
。我该怎么做?
答案 0 :(得分:2)
MaryPython的答案一般都很好。或者,在O(n)而不是O(n ^ 2)中,您可以使用
for index, item in enumerate(my_list):
if item.startswith('fo'):
print(index)
我已使用enumerate使用项目
来遍历索引请注意,Marky的实现在此阵列上失败
['fo','fo','fobar','fobar','hi']
因为.index
总是返回重复出现的first instance(但是否则他的解决方案很好而直观)
答案 1 :(得分:1)
这是一个解决方案。我遍历列表并检查每个项目是否以字符串'fo'
开头(或者您想要检查的任何内容)。如果它以该字符串开头,则打印该项的索引。我希望这有帮助!
Array = ['foo', 'bar']
for item in Array:
if item.startswith('fo'):
print(Array.index(item))
答案 2 :(得分:0)
#!/usr/bin/python
# -*- coding: ascii -*-
Data = ['bleem', 'flargh', 'foo', 'bar' 'beep']
def array_startswith(search, array):
"""Search an iterable object in order to find the index of the first
.startswith(search) match of the items."""
for index, item in enumerate(array):
if item.startswith(search):
return(index)
raise(KeyError)
## Give some sort of error. You probably want to raise an error rather
## than returning None, as this might cause a logic error in the
## later program. I think KeyError is correct, based on the question.
## See Effective Python by Brett Slatkin, Page 29...
if __name__ == '__main__':
lookfor='fo'
try:
result=array_startswith(lookfor, Data)
except KeyError as e:
print("'{0}' not found in Data, Sorry...".format(lookfor))
else:
print("Index where '{0}' is found is: {1}. Found:{2}".format(lookfor,
result, Data[result]))
finally:
print("Try/Except/Else/Finally Cleanup happens here...")
print("Program done.")