python實(shí)現(xiàn)將列表中各個(gè)值快速賦值給多個(gè)變量
我就廢話(huà)不多說(shuō)啦,還是直接看代碼吧!
list1 = [1,2,3,4]
a,b,c,d = list1
則
a = 1
b =2
等
這種方式只有當(dāng)左邊的操作數(shù)個(gè)數(shù)和list1長(zhǎng)度相同時(shí),才可以這么做,不然報(bào)錯(cuò).
我們假設(shè)我們有一個(gè)list對(duì)象List,它的長(zhǎng)度足夠長(zhǎng),想把它從下標(biāo)i開(kāi)始的k個(gè)元素賦給k個(gè)元素,可以這么做:
v1, v2, v3, …, vk = List[i : i + k] #默認(rèn)i=0, k=len(List)
補(bǔ)充知識(shí):python 將某個(gè)字段存儲(chǔ)為列表類(lèi)型
實(shí)現(xiàn)存儲(chǔ)數(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實(shí)現(xiàn)將列表中各個(gè)值快速賦值給多個(gè)變量就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
分享8點(diǎn)超級(jí)有用的Python編程建議(推薦)
這篇文章主要介紹了分享8點(diǎn)超級(jí)有用的Python編程建議(推薦),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5打開(kāi)保存對(duì)話(huà)框QFileDialog詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5打開(kāi)保存對(duì)話(huà)框QFileDialog詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02
python flask解析json數(shù)據(jù)不完整的解決方法
這篇文章主要介紹了python flask解析json數(shù)據(jù)不完整的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
python錯(cuò)誤SyntaxError:?invalid?syntax的解決方法總結(jié)

