欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python实现将列表中各个值快速赋值给多个变量

时间:2021-05-27 08:43:15|栏目:Python代码|点击:

我就废话不多说啦,还是直接看代码吧!

list1 = [1,2,3,4]
a,b,c,d = list1

a = 1
b =2

这种方式只有当左边的操作数个数和list1长度相同时,才可以这么做,不然报错.

我们假设我们有一个list对象List,它的长度足够长,想把它从下标i开始的k个元素赋给k个元素,可以这么做:

v1, v2, v3, …, vk = List[i : i + k] #默认i=0, k=len(List)

补充知识:python 将某个字段存储为列表类型

实现存储数据格式为

{
    "_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)

上一篇:PyQt5 QListWidget选择多项并返回的实例

栏    目:Python代码

下一篇:解决reload(sys)后print失效的问题

本文标题:python实现将列表中各个值快速赋值给多个变量

本文地址:http://www.codeinn.net/misctech/129783.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有