查找元素在list中的位置以及折半查询
问题
查找某个值在list中的位置
解决思路
能够用折半查询的方法解决此问题。
解决(Python)
#! /usr/bin/env python
#coding:utf-8
#折半查找某个元素在list中的位置
def half_search(lst,value,left,right):
length = len(lst)
while left<right:
middle = (right-left)/2
if lst[middle]>value:
right = middle-1
elif lst[middle]<value:
left = middle+1
else:
return middle
if __name__=="__main__":
lst=sorted([2,4,5,9]) #折半算法中list要进行排序
length = len(lst)
left = 0
right = length-1
value =4
result = half_search(lst,value,left,right)
if result:
print result
else:
print "There is no the value that you want to search."
再思考
对于上面的折半方法,在python中,能够通过一个函数实现
lst = sorted([2,4,5,9]) #这里进行排序。主要是为了得到与上面方法一样的结果。其实,list.index()能够针对不论什么list操作,不一定非要排序
result = lst.index(4)
此外。假设遇到list中有多个同样的元素。应该怎样将这些元素的位置都查询出来呢?以下的方法是用python实现。
def find_value_location(lst,value):
result = [i for i in range(len(lst)) if value==lst[i]]
return result
很多其它用python实现的算法,请看:https://github.com/qiwsir/algorithm
qiwsir#gmail.com
查找元素在list中的位置以及折半查询的更多相关文章
- 我的前端工具集(八)获得html元素在页面中的位置
我的前端工具集(八)获得html元素在页面中的位置 liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 有时候需要用点击等操作,来获取某元素在页面中的位置,然后在该位置添加某些操作 如 ...
- 关于js获取元素在屏幕中的位置的方法
针对我们获取元素在页面中的位置的问题,我们还是用老师一峰老师的方法来解决吧 下面上HTML代码 <div class="left_footer"> <p data ...
- Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- C语言-查找一个元素在数组中的位置
#include<stdio.h> #include <stdlib.h> #include <time.h> int search(int key, int a[ ...
- jquery中找到元素在数组中位置,添加或者删除元素的新方法
一:查找元素在数组中的位置 jQuery.inArray()函数用于在数组中搜索指定的值,并返回其索引值.如果数组中不存在该值,则返回 -1. jQuery.inArray( value, array ...
- js能力测评——查找元素的位置
查找元素的位置 题目描述: 找出元素 item 在给定数组 arr 中的位置 输出描述: 如果数组中存在 item,则返回元素在数组中的位置,否则返回 -1 示例1 输入 [ 1, 2, 3, 4 ] ...
- jquery获取元素在文档中的位置信息以及滚动条位置(转)
jquery获取元素在文档中的位置信息以及滚动条位置 http://blog.csdn.net/qq_34095777/article/details/78750886 原文链接 原创 201 ...
- selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码
目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...
- JS中关于位置和尺寸的api
HTMLElement.offsetParent 由于offsetTop 和 offsetLeft 都是相对于 offsetParent 内边距边界的,故offsetParent的意义十分重大.off ...
随机推荐
- CF1213D Equalizing by Division
easy version hard version 问题分析 直接从hard version入手.不难发现从一个数\(x\)能得到的数个数是\(O(\log x)\)的.这样总共有\(O(n\log ...
- eclipse内存溢出 参数配置
http://blog.csdn.net/liuhenghui5201/article/details/50783444
- ubuntu16.04增大swap空间
参见->这里 参见->这里
- C++入门经典-例3.12-使用if-else语句实现根据输入的字符输出字符串
1:代码如下: // 3.12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- leetcode -1 count the path
- retrofit2+rxjava+okhttp网络请求实现
第一步:添加依赖: compile 'io.reactivex:rxandroid:1.2.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.1. ...
- [doker]ubuntu18安装doker
ubuntu安装doker很简单,分4个步骤: Step1:更新资源库并安装apt-transprot-https软件包. 在安装Docker前, 首拉取最新的软件资源库 wangju@wangju- ...
- 前端必须掌握的 nginx 技能(4)
概述 作为一个前端,我觉得必须要学会使用 nginx 干下面几件事: 代理静态资源 设置反向代理(添加https) 设置缓存 设置 log 部署 smtp 服务 设置 redis 缓存(选) 下面我按 ...
- jquery用formada发送文件到服务器
var formdata = new FormData(); formdata.append("file", $("#Input")[0].files[0]); ...
- LoadRunner 技巧之 检查点
LoadRunner 技巧之 检查点 判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的HTTP状态为 200 OK ,那么VuGen 就认为脚本正确地运行了,并且是运行通过的.在绝 ...