99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀利用python求相鄰數(shù)的方法示例
利用python求相鄰數(shù)的方法示例
2017-09-11
收藏

利用python求相鄰數(shù)的方法示例

本文主要給大家介紹了關(guān)于利用python求相鄰數(shù)的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹:

什么是相鄰數(shù)?

比如5,相鄰數(shù)為4和6,和5相差1的數(shù),連續(xù)相差為1的一組數(shù)

需求:

遍歷inputList 所有數(shù)字,取出所有數(shù)字,判斷是否有相鄰數(shù), 不相鄰數(shù)字 和 相鄰數(shù)字 都以 “數(shù)組”形式 添加到 outputList 中, 并且 每個(gè)“數(shù)組” 里 第一位 遞減 補(bǔ)全兩位數(shù),末位 遞增 補(bǔ)全兩位數(shù), 每一個(gè)數(shù)不能小于0, 不能大于 400

( 提示: 在inputList 中 "12,13" 是相鄰的數(shù)字,視為一組, 需要以[10, 11, 12, 13, 14, 15] 數(shù)組形式添加到outputList 中,而 “3”沒(méi)有相鄰的數(shù),也視為一組,需要以[1, 2, 3, 4, 5]數(shù)組形式添加到outputList中 )

輸入:    
inputList = [0, 3, 5, 6, 7, 9, 12, 13, 15, 16, 17, 19, 20, 21, 22, 25, 27, 29, 30, 32, 33, 36, 39, 40, 43, 44, 46, 47, 48, 53, 54, 57, 58, 60, 62, 64, 65, 66, 67, 72, 74, 75, 76, 77, 78, 80, 82, 84, 85, 86, 89, 95, 96, 97, 98, 103, 104, 107, 108, 110, 111, 114, 116, 117, 118,   120, 121, 122, 124, 127, 132, 135, 137, 138, 139, 140, 145, 146, 148, 149, 150, 151, 155, 156, 160, 161, 166, 167, 170, 171, 172, 175, 178, 179, 180, 181, 182, 183, 184, 186, 188, 189, 190, 193, 195, 196, 198, 202, 205, 208, 210, 211, 213, 214, 215, 217,   221, 226, 227, 228, 233, 234, 235, 240, 241, 246, 247, 249, 255, 257, 258, 261, 262, 263, 267, 268, 269, 270, 271, 272, 275, 278, 280, 282, 283, 284, 286, 287, 289, 291, 292, 295, 296, 298, 300, 302, 303, 304, 305, 306, 310, 315, 317, 319, 320, 321, 322,   323, 324, 325, 326, 328, 331, 336, 339, 341, 342, 344, 346, 349, 354, 355, 356, 362, 363, 365, 366, 367, 368, 371, 374, 376, 378, 382, 383, 388, 390, 393, 396, 399]

輸出 :    
outputList = [[0, 1, 2] , [1, 2, 3, 4, 5], [3, 4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11],[10, 11, 12, 13, 14, 15] , ........此處省略]

那,如何解決這個(gè)問(wèn)題?
  1. 設(shè)置一個(gè)值,指向index=0, start_index = 0
  2. 初始化一個(gè)中間列表median = [ ] , 一個(gè)保存結(jié)果列表 result_l = [ ]
  3. for循環(huán)開(kāi)始, start_index 指向每一個(gè)相鄰數(shù)的開(kāi)頭
  4. 通過(guò)索引指向的值和索引后指向的值進(jìn)行差值比較,步長(zhǎng)不為1的,start_index移動(dòng)到這個(gè)值上
  5. 循環(huán)往復(fù),獲得相鄰列表
  6. 通過(guò)map函數(shù),對(duì)每一個(gè)相鄰列表進(jìn)行前后各插入兩個(gè)相鄰數(shù)
  7. 通過(guò)列表解析, 剔除不滿足條件的相鄰數(shù)

示例代碼    
#!/usr/bin/python3
 
__author__ = 'beimenchuixue'
__blog__ = 'http://www.cnblogs.com/2bjiujiu/'
 
 
def go_cha_ru(new_l):
 """往列表中前后個(gè)插入兩個(gè)相鄰數(shù),通過(guò)列表解析去除小于0的和大于400的數(shù)"""
 new_l.insert(0, new_l[0] - 1)
 new_l.insert(0, new_l[0] - 1)
 new_l.append(new_l[len(new_l) - 1] + 1)
 new_l.append(new_l[len(new_l) - 1] + 1)
 return [i for i in new_l if 0 <= i <= 400]
 
 
def go_xiang_lin(raw_l):
 """獲取相鄰數(shù)"""
 start_index = 0
 result_l = []
 median = []
   
 # 索引從start_index起,到最后
 for raw_index in range(len(raw_l)):
  # 判斷是否for循環(huán)到指定位置
  if start_index == raw_index:
   # 初始移動(dòng)位置參數(shù)
   index = 0
   while True:
    # 指針指向的起始值
    start_value = raw_l[start_index]
    # 如果指針指向最后一個(gè)位置,開(kāi)始值=最后一個(gè)值
    if start_index == len(raw_l)-1:
     end_value = start_value
    else:
     # 最后一個(gè)值 = 初始值 + 位置參數(shù)值
     end_value = raw_l[start_index + index]
    # 通過(guò)初始值 + 位置參數(shù)值 是否等于 最后一個(gè)值,判斷是否為相鄰數(shù),如果是,添加到中間列表
    if start_value + index == end_value:
     median.append(end_value)
     # 位置參數(shù) + 1
     index += 1
    else:
     # 如果不是,初始指針指向 移動(dòng)位置參數(shù)個(gè)單位
     start_index += index
     # 把每主相鄰數(shù)添加到結(jié)果列表
     result_l.append(median)
     median = []
     break
 # 通過(guò)高階函數(shù),對(duì)結(jié)果集中每個(gè)相鄰數(shù)列表進(jìn)行插值操作
 return map(go_cha_ru, result_l)
 
if __name__ == '__main__':
 input_list = [0, 3, 5, 6, 7, 9,
     12, 13, 15, 16, 17, 19, 20, 21, 22, 25,
     27, 29, 30, 32, 33, 36, 39, 40, 43, 44, 46, 47, 48, 53, 54,
     57, 58, 60, 62, 64, 65, 66, 67, 72, 74, 75, 76, 77, 78, 80, 82,
     84, 85, 86, 89, 95, 96, 97, 98, 103, 104, 107, 108, 110, 111, 114,
     116, 117, 118, 120, 121, 122, 124, 127, 132, 135, 137, 138, 139, 140,
     145, 146, 148, 149, 150, 151, 155, 156, 160, 161, 166, 167, 170, 171,
     172, 175, 178, 179, 180, 181, 182, 183, 184, 186, 188, 189, 190, 193,
     195, 196, 198, 202, 205, 208, 210, 211, 213, 214, 215, 217, 221, 226,
     227, 228, 233, 234, 235, 240, 241, 246, 247, 249, 255, 257, 258, 261,
     262, 263, 267, 268, 269, 270, 271, 272, 275, 278, 280, 282, 283, 284,
     286, 287, 289, 291, 292, 295, 296, 298, 300, 302, 303, 304, 305, 306,
     310, 315, 317, 319, 320, 321, 322, 323, 324, 325, 326, 328, 331, 336,
     339, 341, 342, 344, 346, 349, 354, 355, 356, 362, 363, 365, 366, 367,
     368, 371, 374, 376, 378, 382, 383, 388, 390, 393, 396, 399]
 # 結(jié)果
 output_list = list(go_xiang_lin(input_list))
 print(output_list)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了.

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }