[Python] Three methods to compare the list of one-dimensional array and the list of two-dimensional array and extract only the matching values [json]

Overview

As the title says. ** How to get the value in the [name] column of B when the value in the list (A) of the one-dimensional array exists in the [ID] column of the list (B) of the two-dimensional array. ** ** The above is what I learned while playing with the slack api, and I've summarized it below.

A.list


print(name_list)
['U012JDYRD2T', 'U012X478FNZ']

B.list #It has been modified to make it easier to see.


print(data)
[['name',     'id'],
 ['Slackbot', 'USLACKBOT'],
 ['test_1',   'U012JDYRD2T'], #I want here
 ['test_5',   'U012JDYSN07'],
 ['kita',     'U012X478FNZ'],#I want here
 ['test_4',   'U012XQBE63X'],
 ['test_bot', 'U012YTNNPB5'],
 ['test_3',   'U012ZC8AQ8K'],
 ['tese_2',   'U013BQDLK6V']]

Expected results.py


print(result)
['test_1','kita']

Method 1

1.py


result = []
for data_id in data:
    if data_id[1] in name_list:
        result.append(data_id[0])

(1) For loops [data_id in data], data is stored in data_id one by one. ② [if data_id [1] in name_list] is a condition that the [id] column (data_id [1]) and name_list match. ③ [result.append (data_id [0])] adds the [name] column (data_id [0]) to the list called [result]

Method 2

1.py


result = []
result = [data_id[0] for data_id in data if data_id[1] in name_list]

The theory is the same as method 1. smart.

Method 3

3.py


r = []
r2 = []
for i in data:
    r = r + i
for i in name_list:
    tmp = (r.index(i)) - 1
    r2.append(r[tmp])

① Store data one by one in r. It is stored as ['name','id','Slackbot','USLACKBOT'…]. (2) Use the fact that name and id are stored in order. In other words, the one before " U012X478FNZ " is " kita ", and the one before " U012JDYRD2T " is " test_1 ". ③ So, in [tmp = (r.index (i)) -1], you can get the index of" kita "immediately before that by [-1] from the index (number) of" U012X478FNZ ". it can. ④ And the value of the acquired index is added to [r2].

What I found

You need to understand more about loops and conditionals.

Recommended Posts

[Python] Three methods to compare the list of one-dimensional array and the list of two-dimensional array and extract only the matching values [json]
I made a function to see the movement of a two-dimensional array (Python)
Just add the python array to the json data
Compare the speed of Python append and map
List of Python code to move and remember
Extract images and tables from pdf with python to reduce the burden of reporting
I tried to compare the processing speed with dplyr of R and pandas of Python
[Python] How to remove duplicate values from the list
[Python] How to output the list values in order
[Python] Types of statistical values (features) and calculation methods
[python] plot the values ​​before and after the conversion of yeojohnson conversion
Python: Create a dictionary from a list of keys and values
I want to know the features of Python and pip
Extract the value closest to a value from a Python list element
Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query
Extract the index of the original set list that corresponds to the list of subsets.
[Python] Display only the elements of the list side by side [Vertical, horizontal]
[Python] How to get the first and last days of the month
[python] Get the rank of the values in List in ascending / descending order
Convert the result of python optparse to dict and utilize it
[Python] A program that rotates the contents of the list to the left
Example of batch commit creation (some methods to process while ticking the array) and sample of batch writing to Firestore
Python> Extract (unpack) the value of list> Add *> You taught me the difference between Python 2 and Python 3 regarding print (* mylist) / print ().
The story of Python and the story of NaN
[Python] How to swap array values
Python logging and dump to json
Compare Python and JavaScript array loops
About the basics list of Python basics
[Python] How to specify the window display position and size of matplotlib
Python> sys.path> List of strings indicating the path to search for modules
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I measured 6 methods to get the index of the maximum value (minimum value) of the list
[Python memo] Be careful when creating a two-dimensional array (list of lists)
Extract every n elements from an array (list) in Python and Ruby
[Python] How to use the enumerate function (extract the index number and element)
I tried to extract and illustrate the stage of the story using COTOHA
I tried to verify and analyze the acceleration of Python by Cython
I measured the speed of list comprehension, for and while with python2.7.
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
Python> set> Convert with set ()> dictionary is only key> I was taught how to convert the values of dictionary to set / dir ({}) / help ({}) / help ({} .values)