python / Make a dict from a list.

Introduction

Make a Dict from the list. Since I use numpy's loadtxt, which I often use, it is numpy_arrray type, but I can use it with list type without any problem. You can create dictionary types such as {"hoge": (hoge1, hoge2, hoge3, ...}.

(memorandum)

  1. Make an appropriate array. Here, it is a, b, c. --A is the key and b and c are the values.
  2. First, zip () to tuple b and c. (As _bc)
  3. After that, use dict () to attach a and _bc.

list2dict.py


import numpy as np
a,b,c = np.arange(10), np.arange(10)*2, np.arange(10)*3

#-> a: [0 1 2 3 4 5 6 7 8 9]
#-> b: [ 0  2  4  6  8 10 12 14 16 18]
#-> c: [ 0  3  6  9 12 15 18 21 24 27]

_bc = zip(b,c)

result = dict(zip(a,_bc))
#-> {0: (0, 0), 1: (2, 3), 2: (4, 6), 3: (6, 9), 4: (8, 12), 5: (10, 15), 6: (12, 18), 7: (14, 21), 8: (16, 24), 9: (18, 27)}

# result = dict(zip(a,zip(b,c)))But good.

That's it.

Recommended Posts

python / Make a dict from a list.
Make a copy of the list in Python
Make MeCab available from Python3
Python list is not a list
Make a bookmarklet in Python
Make a fortune with Python
Make a copy of a Google Drive file from Python
[Python] I want to make a nested list a tuple
Call a Python function from p5.js.
Make a Santa classifier from a Santa image
[python] Manage functions in a list
Touch a Python object from Elixir
[Python] How to make a list of character strings character by character
Let's make a GUI with python.
python Condition extraction from a list that I often forget
Make Python dict accessible by Attribute
[Python] Make the function a lambda function
Make a recommender system with python
Python: Create a dictionary from a list of keys and values
[Python] list
Let's make a graph with python! !!
Python3> List generation from iterable> list (range (5))
Extract the value closest to a value from a Python list element
Make a request from Device Farm (appium python) to API Gateway
I want to make a parameter list from CloudFormation code (yaml)
Pass a list by reference from Python to C ++ with pybind11
How to remove duplicates from a Python list while preserving order.
Let's make a shiritori game with Python
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
Python> empty XXX (XXX: dict, list, tuple, set)> {} / [] / () / set ()
Display a list of alphabets in Python 3
Send a message from Python to Slack
Make a relation diagram of Python module
# 5 [python3] Extract characters from a character string
Let's make a voice slowly with Python
Make Qt for Python app a desktop app
Download images from URL list in Python
[python] Get a list of instance variables
Create a deb file from a python package
Generate a class from a string in Python
Use Django from a local Python script
Manipulate BigQuery tables from a Python client
To make a dict type property read-only
Let's make a web framework with Python! (1)
Let's make a combination calculation in Python
Call a command from Python (Windows version)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
[Python] Get a list of folders only
Let's make a web framework with Python! (2)
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
sql from python
Python Dict category
MeCab from Python
Python> Comprehension / Comprehension> List comprehension
Make a decision tree from 0 with Python and understand it (4. Data structure)
Python list manipulation
Send a message from Slack to a Python server
[Python] List Comprehension Various ways to create a list
Run a Python script from a C # GUI application