前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python实现将列表中各个值快速赋值给多个变量

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

作者头像
砸漏
发布2020-11-04 15:00:06
4.3K0
发布2020-11-04 15:00:06
举报
文章被收录于专栏:恩蓝脚本

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

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 将某个字段存储为列表类型

实现存储数据格式为

代码语言:javascript
复制
{
    "_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"
      }
     ]
    }
   }

代码:

代码语言:javascript
复制
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实现将列表中各个值快速赋值给多个变量就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档