python實現(xiàn)將列表中各個值快速賦值給多個變量
我就廢話不多說啦,還是直接看代碼吧!
list1 = [1,2,3,4]
a,b,c,d = list1
則
a = 1
b =2
等
這種方式只有當(dāng)左邊的操作數(shù)個數(shù)和list1長度相同時,才可以這么做,不然報錯.
我們假設(shè)我們有一個list對象List,它的長度足夠長,想把它從下標i開始的k個元素賦給k個元素,可以這么做:
v1, v2, v3, …, vk = List[i : i + k] #默認i=0, k=len(List)
補充知識:python 將某個字段存儲為列表類型
實現(xiàn)存儲數(shù)據(jù)格式為
{ "_index": "nested-20180815", "_type": "stb-iptv-montor-m-gather-apk", "_id": "AWU8sZboGQQbsn0rAW4J", "_score": 1, "_source": { "mdiNested": [ { "mdiMLR": 0, "mdiType": "0" }, { "mdiMLR": 0, "mdiType": "1" }, { "mdiMLR": 0, "mdiType": "2" }, { "mdiMLR": 0, "mdiType": "3" }, { "mdiMLR": 0, "mdiType": "4" }, { "mdiMLR": 0, "mdiType": "5" } ] } }
代碼:
from elasticsearch import Elasticsearch from elasticsearch import helpers import json es_20 = Elasticsearch(hosts="1.0.0.0", port=9200, timeout=15000) time_ = "20180815" index_20 = "nested-{0}".format(time_) type_20 = "stb-iptv-montor-m-gather-apk" def set_mapping(): my_mappping = { type_20: { "properties": { "mdiNested": { "properties": { "mdiMLR": { "type": "short" }, "mdiType": { "type": "keyword" } } } } } } create_index = es_20.indices.create(index=index_20, body=None) create_mapping = es_20.indices.put_mapping(index=index_20, body=my_mappping, doc_type=type_20) mdiMLR = [0,1,2,3,4] mdiType = ["0","1","2","3","4","5"] actions = [] dict_ ={} for mdiMLR_ in mdiMLR: dict_list = [] for type in mdiType: t1 ={'mdiMLR': mdiMLR_, 'mdiType': type} dict_list.append(t1) action = { "_index": index_20, "_type": type_20, "_source": { "mdiNested": dict_list } } actions.append(action) helpers.bulk(es_20, actions)
以上這篇python實現(xiàn)將列表中各個值快速賦值給多個變量就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談keras中的batch_dot,dot方法和TensorFlow的matmul
這篇文章主要介紹了淺談keras中的batch_dot,dot方法和TensorFlow的matmul,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python內(nèi)置函數(shù)input()示例詳解
input()函數(shù)是Python中用于獲取用戶輸入的一個簡單而強大的工具,它在創(chuàng)建需要用戶交互的程序時非常有用,這篇文章主要介紹了Python內(nèi)置函數(shù)input()詳解,需要的朋友可以參考下2024-04-04Python中的None與 NULL(即空字符)的區(qū)別詳解
這篇文章主要介紹了Python中的None與 NULL(即空字符)的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09解決springboot yml配置 logging.level 報錯問題
今天小編就為大家分享一篇解決springboot yml配置 logging.level 報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python3.6簡單的操作Mysql數(shù)據(jù)庫的三個實例
今天小編就為大家分享一篇關(guān)于Python3.6簡單的操作Mysql數(shù)據(jù)庫的三個實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10