问题

查找某个值在list中的位置

解决思路

能够用折半查询的方法解决此问题。

解决(Python)

  1. #! /usr/bin/env python
  2. #coding:utf-8
  3. #折半查找某个元素在list中的位置
  4. def half_search(lst,value,left,right):
  5. length = len(lst)
  6. while left<right:
  7. middle = (right-left)/2
  8. if lst[middle]>value:
  9. right = middle-1
  10. elif lst[middle]<value:
  11. left = middle+1
  12. else:
  13. return middle
  14. if __name__=="__main__":
  15. lst=sorted([2,4,5,9])    #折半算法中list要进行排序
  16. length = len(lst)
  17. left = 0
  18. right = length-1
  19. value =4
  20. result = half_search(lst,value,left,right)
  21. if result:
  22. print result
  23. else:
  24. print "There is no the value that you want to search."

再思考

对于上面的折半方法,在python中,能够通过一个函数实现

  1. lst = sorted([2,4,5,9])    #这里进行排序。主要是为了得到与上面方法一样的结果。其实,list.index()能够针对不论什么list操作,不一定非要排序
  2. result = lst.index(4)

此外。假设遇到list中有多个同样的元素。应该怎样将这些元素的位置都查询出来呢?以下的方法是用python实现。

  1. def find_value_location(lst,value):
  2. result = [i for i in range(len(lst)) if value==lst[i]]
  3. 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. Apicloud_(项目)网上书城01_前端搭建

    [本文皆在记录自己开发Apicloud项目过程,不具备教学水平性文章] 参考书籍<30天App开发从0到1> Apicloud_(项目)网上书城01_前端页面开发 传送门 Apicloud ...

  2. 如何快速查询中科院JCR分区和汤森路透JCR分区

    参考: https://blog.csdn.net/chichuhe/article/details/83054624 https://blog.csdn.net/Sunflower02/articl ...

  3. EL表达式里面不能直接使用list.size()得到长度,

    在jsp页面中不能通过${list.size}取列表长度, 而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pre ...

  4. oracle存储过程错误跟踪

    1.首先创建用于保存错误信息的表 1 2 3 4 5 6 7 8 CREATE TABLE TBL_PROC_ERRMSG (   BIZ_CODE VARCHAR2(50),   ERR_LINE ...

  5. python模块------pyautogui

    安装 pip install pyautogui 基本使用 查询 screenWidth, screenHeight = pyautogui.size() # 屏幕尺寸 mouseX, mouseY ...

  6. Anaconda快捷键

    ctr+1  注释多行 ctr+4  包裹注释多行 ctr+d  删除一行

  7. Raspbian 编译安装 PHP 7.2

    原文地址:Raspbian 编译安装 PHP 7.2 0x00 配置 开发板: Raspberry Pi 3B 系统: Raspbian 2019-04-08 stretch 0x01 下载源码 20 ...

  8. Android6.0运行时权限的处理Demo

    MainActivity.java package com.loaderman.permissionsdemo; import android.Manifest; import android.con ...

  9. Difference Between Currency Swap and FX Swap

    [z]https://www.differencebetween.com/difference-between-currency-swap-and-vs-fx-swap/ Currency Swap ...

  10. iOS 图表工具charts介绍

    charts是一个很好的绘图工具,功能非常强大,可以用来绘制折线,柱状图,饼状图,k线图,k线分时图,雷达图,气泡图等等,charts是一款仿照安卓 MPAndroidChart而来的一个基于swif ...