0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】 2つのListを並行処理する方法

Last updated at Posted at 2021-03-06

概要

  • 2つのListを並行処理する方法
     

1: for文を使用する方法
2: zip関数を使用する方法
※ 2 の方が可読性が上がるので、2 を使用することを推奨。

検証環境

OS:18.04.5 LTS
Python:3.6.9

方法

2つのlistをで並行処理

1: for文を使用する方法

基本形

for 変数1, 変数2 in enumerate( list_1 ):
    print('{0}, {1}').format(変数2, list_2[変数1]) 

実行結果

list_1 = [1,2,3,4,5,6,7,8,9,10];
list_2 = ['1','2','3','4','5','6','7','8','9','10'];

# 並行処理
for i, j in enumerate( list_1 ):
    print('{0}, {1}'.format( j, list_2[i] ));

# 結果
1, 1
2, 2
3, 3
4, 4
5, 5
6, 6
7, 7
8, 8
9, 9
10, 10

2: zip関数を使用する方法

基本形

for 変数1, 変数2 in zip(list_1, list_2):
    print('{0}, {1}'.format( 変数1, 変数2 ))

実行結果


list_1 = [1,2,3,4,5,6,7,8,9,10]
list_2 = ['1','2','3','4','5','6','7','8','9','10']

# 並行処理
for i, j in zip(list_1, list_2):
    print('{0}, {1}'.format( i, j ))

# 結果
1, 1
2, 2
3, 3
4, 4
5, 5
6, 6
7, 7
8, 8
9, 9
10, 10

0
1
2

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?