问题

查找某个值在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中的位置以及折半查询的更多相关文章

  1. 我的前端工具集(八)获得html元素在页面中的位置

    我的前端工具集(八)获得html元素在页面中的位置   liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 有时候需要用点击等操作,来获取某元素在页面中的位置,然后在该位置添加某些操作 如 ...

  2. 关于js获取元素在屏幕中的位置的方法

    针对我们获取元素在页面中的位置的问题,我们还是用老师一峰老师的方法来解决吧 下面上HTML代码 <div class="left_footer"> <p data ...

  3. Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. C语言-查找一个元素在数组中的位置

    #include<stdio.h> #include <stdlib.h> #include <time.h> int search(int key, int a[ ...

  5. jquery中找到元素在数组中位置,添加或者删除元素的新方法

    一:查找元素在数组中的位置 jQuery.inArray()函数用于在数组中搜索指定的值,并返回其索引值.如果数组中不存在该值,则返回 -1. jQuery.inArray( value, array ...

  6. js能力测评——查找元素的位置

    查找元素的位置 题目描述: 找出元素 item 在给定数组 arr 中的位置 输出描述: 如果数组中存在 item,则返回元素在数组中的位置,否则返回 -1 示例1 输入 [ 1, 2, 3, 4 ] ...

  7. jquery获取元素在文档中的位置信息以及滚动条位置(转)

    jquery获取元素在文档中的位置信息以及滚动条位置 http://blog.csdn.net/qq_34095777/article/details/78750886     原文链接 原创 201 ...

  8. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...

  9. JS中关于位置和尺寸的api

    HTMLElement.offsetParent 由于offsetTop 和 offsetLeft 都是相对于 offsetParent 内边距边界的,故offsetParent的意义十分重大.off ...

随机推荐

  1. Linux扩展swap分区

    一.将sda磁盘进行分区: 先查看sda磁盘已经使用了多少主分区,如下图所示,主分区已使用3个,所以应选择扩展分区: 二.再将扩展分区进行分区: 三.分区完成后执行partprobe使系统重新识别分区 ...

  2. css彩色(渐变)文字

    css彩色文字也称渐变文字 在张鑫旭博客首页看到这效果,就自己研究了一下. 实现方法加个背景然后在根据文本剪切,再把文本填充为透明色让之前设置的背景颜色显示出来即可. -webkit-backgrou ...

  3. spring整合之后运行报什么只读错误。Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

    解决办法, 再大dao的实现类上添加注解: @Transactional(readOnly = false ) 不让它只读就行了

  4. In an ASP.NET website with a codebehind at what point are the .cs files compiled?

    In an ASP.NET website with a codebehind at what point are the .cs files compiled? This applies to We ...

  5. 【转】数组指针&指针数组

    转自:https://www.cnblogs.com/mq0036/p/3382732.html 数组指针和指针数组的区别 数组指针(也称行指针)定义 int (*p)[n];()优先级高,首先说明p ...

  6. C# 利用AForge.NET开源类库实现 图像素描效果

    引入DLL: using AForge.Imaging; using AForge.Imaging.Filters; //using AForge.Video.DirectShow;可以使用摄像头图像 ...

  7. BeanDefinition 实例

    BeanDefinition BeanDefinition /** * BeanDefinition 用于描述一个 bean 实例,包括属性值.构造参数和补充信息. */ public interfa ...

  8. Kotlin之定义函数

    java: int add (int m ,int n){ return m+n; } void process(int m){ Systrm.out.println(m); } kotlin: fu ...

  9. C#规范整理·多线程\异步\并行\任务

    有一个领域的工作处理起来几乎总是最棘手的,这就是多线程编码.多线程编码是所有开发人员前进途中的一个坎,现在,该是尝试克服它的时候了. 1.区分异步和多线程应用场景 先看一个例子 private voi ...

  10. UI自动化关于图片验证码识别的解决方法

    def __save_screenshot(self): self.driver.save_screenshot('full_snap.png') self.page_snap_obj = Image ...