LoginSignup
6
7

More than 5 years have passed since last update.

Python > list > リスト要素を加算してPythonリストとして返す > 方法1.Numpy使用 | 方法2.Pythonのみでの実装

Last updated at Posted at 2018-05-20
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0

処理概要

  • Pythonリストを使う関数がある
  • 2つのリストを要素ごとに加算して、その関数で使いたい

方法1. Numpy使用

  • Numpyとしてリストを要素ごとに加算
  • Pythonリストとして返す
    • tolist()を使う

code

test_list_elemadd_180520.py
import numpy as np


def python_list_add(in1, in2):
    wrk = np.array(in1) + np.array(in2)
    return wrk.tolist()

org = [3, 1, 4]  # origin
pos = [2, 7, 1]

wrk = python_list_add(org, pos)

print(wrk)

run
$ python3 test_list_elemadd_180520.py 
[5, 8, 5]

方法2. Pythonのみでの実装

Numpyを使わずPythonのみでの実装について、@shiracamus さんのコメントの方法にて教えていただきました。

情報感謝です。

micoiPythonの環境ではNumpyがないという情報もありますね。
音を1サンプル毎に扱う音響合成環境をPythonでつくる(2)

なぜならPythonにはnumpyはあるけどmicroPythonには無いからです!

6
7
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7