Pharmaceutical company researchers summarized Python's data structures

Introduction

Here, we will explain "data structure" for Python beginners. It is supposed to use Python3 series.

List

Creating a list

list_1.py


list_int = [1, 2, 3, 4, 5] #A list of all numbers
print(list_int)

list_str = ['Python', 'R', 'Java', 'Ruby', 'PHP'] #A list of all strings
print(list_str)

list_bool = [True, False, True, True, False] #List of all Boolean values
print(list_bool)

list_mix = [1, 2, 3, 4, 'GO'] #You can also mix elements of multiple data types in a single list.
print(list_mix)

list_2d = [[1, 2], [3, 4]] #You can also create a list of two or more dimensions.
print(list_2d)

list_empty = [] #Create an empty list.
print(list_empty)

If you want to store consecutive numbers in a list, you can also write as follows.

list_2.py


list_range = list(range(1, 100)) # range(m, n)Is from m to n-It points to a continuous integer of 1 and is converted to a list type.
print(list_range)

list_range_odd = list(range(1, 100, 2)) #You can do it even if it is not in 1 increments.
print(list_range_odd)

Reference of list element values

list_3.py


list_int = [1, 2, 3, 4, 5]

list_int_1 = list_int[1] #Refer to the value of the element at index 1 (second).
print(list_int_1)

list_int_0 = list_int[0] #Refer to the value of the element at index 0 (1st).
print(list_int_0)

list_int_minus1 = list_int[-1] #Refer to the value of the first element counting from the back.
print(list_int_minus1)

list_2d = [[1, 2], [3, 4]]

list_2d_0 = list_2d[0]
print(list_2d_0) #Refer to the value of the element at index 0 (1st).

list_2d_0_0 = list_2d[0][0]
print(list_2d_0_0) #Refer to the value of the element of index 0 (1st) in index 0 (1st).

#If you specify an index value that does not contain an element, an error will occur.
list_int_12 = list_int[12]
print(list_int_12)

list_int_slice1to3 = list_int[1:3] #Slice (take out elements of a specific section continuously)
print(list_int_slice1to3)

You can treat strings like a list (you can refer to the Xth character from the front, etc.).

list_4.py


var_str = 'abcde'
print(var_str[3])
print(var_str[1:3])

Update list values

list_5.py


list_int = [1, 2, 3, 4, 5]
list_int[2] = 6 #Update the value at index 2.
print(list_int)

in operator

You can use the in operator to determine if a value is included as an element of the list (returns True if it is included, False if it is not).

list_6.py


list_int = [1, 2, 3, 4, 5]
print(2 in list_int) #True is output
print(12 in list_int) #False is output

Get the number of elements in the list

You can use len () to get the number of elements in the list.

list_7.py


list_int = [1, 2, 3, 4, 5]
list_int_length = len(list_int)
print(list_int_length)

You can use it for strings as follows (you can count the number of characters).

list_8.py


var_str = 'abcde'
var_str_length = len(var_str)
print(var_str_length)

List calculation

list_9.py


ones = [1] * 5 #Repeat the same list a specified number of times.
print(ones)

rep = [1, 2, 3] * 4
print(rep)

nums_1 = [1, 2, 3]
nums_2 = [4, 5]

nums_3 = nums_1 + nums_2 #Combine lists.
print(nums_3)

List method

list_10.py


nums = [1, 2, 3, 4, 5]

nums.append(6) #Add an element to the end of the list.
print(nums)

nums.extend([7, 8, 9]) #Add all iterable elements to the end of the list.
print(nums)

nums.insert(5, 10) #Add element 10 before the fifth element in the index.
print(nums)

nums.remove(10) #Delete element 10.
print(nums)

print(nums.pop(1)) #Deletes the first element of the index and returns it.

print(nums.pop()) #If the index number is not specified, the last element is deleted and returned.

print(nums.index(5)) #Finds the specified element and returns its position.

print(nums.count(3)) #Returns the number of occurrences of the specified element.

nums.sort(key=None, reverse=False) #Sort the list. You can specify the comparison function with key. reverse=If True, sort in descending order.
print(nums)

nums.reverse() #Reverse order with in-place operation.
print(nums)

nums_rev = nums[::-1] #Reverse the order.
print(nums_rev)

nums_copy = nums.copy() #Make a copy.
print(nums_copy)

del nums[6] #Deletes the element with the specified index number.
print(nums)

nums.clear() #Delete all elements of the list.
print(nums)

Tuple

Creating tuples

tupple_1.py


tupple_int = (1, 2, 3, 4, 5)
print(tupple_int)

Reference of tuple element values

tupple_2.py


tupple_int_1 = tupple_int[1] #See the value of the element at index 1 (2nd)
print(tupple_int_1)

tupple_int_0 = tupple_int[0] #See the value of the element at index 0 (1st)
print(tupple_int_0)

tupple_single = (1,) #Tuple with 1 element
print(tupple_single)

Tuple values cannot be updated (this is a big difference from the list). Conversely, tuples are a good way to convert values that you don't want to update in the middle of processing into an array format.

tupple_3.py


tupple_int = (1, 2, 3, 4, 5)
tupple[2] = 6 #An error will occur.

Dictionary

Creating a dictionary

dictionary_1.py


dict_human = {'height': 200, 'body weight': 100, 'BMI': 25} # {key: value}Create in the form of.
print(dict_human)

dict_empty = {} #Create an empty dictionary.
print(dict_empty)

Reference of dictionary element values

dictionary_2.py


dict_human = {'height': 200, 'body weight': 100, 'BMI': 25}
dict_human_height = dict_human['height']
print(dict_human_height)

Update dictionary element values

dictionary_3.py


dict_human = {'height': 200, 'body weight': 100, 'BMI': 25}
dict_human['height'] = 180
print(dict_human)

Adding new items to the dictionary

If you specify a key that is not in the dictionary and assign a value, a new key will be added.

dictionary_4.py


dict_human = {'height': 200, 'body weight': 100, 'BMI': 25}
dict_human['sex'] = 'Man'
print(dict_human)

Dictionary method

dictionary_5.py


dict_human = {'height': 200, 'body weight': 100, 'BMI': 25}

print(dict_human.keys()) #Returns a list of keys that exist in the dictionary.

print(dict_human.values()) #Returns a list of values that exist in the dictionary.

print(dict_human.items()) #Returns a list of key / value combinations that exist in the dictionary.

print(dict_human.get('height', 'unknown')) #If there is a key specified by the first argument, its value is returned.
print(dict_human.get('blood type', 'unknown')) #If the key specified by the first argument does not exist, the value specified by the second argument is returned.

dict_human_pop = dict_human.pop('BMI') #Deletes the specified key and returns it.
print(dict_human_pop) #Returns the value of the deleted key.
print(dict_human) #Returns the deleted dictionary.

del dict_human['body weight'] #Delete the specified key.
print(dict_human)

dict_human.clear() #Delete all elements of the dictionary.
print(dict_human)

Set

Creating a set

set_1.py


set_int = {1, 2, 3, 4, 5}
print(set_int)

set_empty = set()
print(set_empty)

Set method

set_2.py


set_int = {1, 2, 3, 4, 5}

set_int.add(6) #Add items in parentheses.
print(set_int)

set_int.update([7, 8, 9, 10]) #Add all the iterable elements in parentheses.
print(set_int)

set_int.pop() #Deletes one of the elements and returns it.
print(set_int)

set_int.remove(10) #Delete the item in parentheses. An error will occur if the specified item does not exist.
print(set_int)

set_int.discard(10) #Delete the item in parentheses. No error occurs even if the specified item does not exist.
print(set_int)

set_int_2 = {1, 2, 3}

print(set_int.isdisjoint(set_int_2)) # set_int_True is returned if there is no element in common with 2.

print(set_int.issubset(set_int_2)) # set_int_If it is a subset of 2, True is returned.

print(set_int.issuperset(set_int_2)) # set_int_If 2 is a subset, True is returned.

print(set_int.difference(set_int_2)) # set_int_Returns the set (difference set) with the elements of 2 removed.

set_int.difference_update(set_int_2) # set_int_Replace with the set of differences from 2.
print(set_int)

print(set_int.intersection(set_int_2)) # set_int_Returns the intersection (product set) with 2.

set_int.intersection_update(set_int_2) # set_int_Replace with an intersection with 2.
print(set_int)

print(set_int.symmetric_difference(set_int_2)) #Returns a set of elements (symmetric difference) contained in only one.

set_int.symmetric_difference_update(set_int_2) # set_int_Replace with symmetric difference with 2.
print(set_int)

print(set_int.union(set_int_2)) # set_int_Returns a set (union) with 2 elements added.

set_int.update(set_int_2) # set_int_Replace with the union with 2.
print(set_int)

set_int_copy = set_int.copy()
print(set_int_copy)

set_int.clear()
print(set_int)

Judgment of the set

set_3.py


set_1 = {3, 4, 5}
set_2 = {1, 2, 3, 4, 5}

print(set_1 <= set_2) # set_1 is set_Whether it is a subset of 2
print(set_1.issubset(set_2) or set_2.issuperset(set_1))

print(set_1 < set_2) # set_1 is set_Is it a subset of 2 and not the same set?
print(set_1 <= set_2 and set_1 != set_2)

print(set_1 == set_2) # set_1 and ser_Whether 2 is the same set
print(set_1 <= set_2 and set_1 >= set_2)

print(3 in set_1) #Whether the item is included in the set

Set arithmetic

set_4.py


set_1 = {3, 4, 5}
set_2 = {1, 2, 3, 4, 5}

print(set_1 - set_2) #Difference set
print(set_1.difference(set_2))

print(set_1 & set_2) #Intersection
print(set_1.intersection(set_2))

print(set_1 | set_2) #Union
print(set_1.union(set_2))

print(set_1 ^ set_2) #Symmetric difference
print(set_1.symmetric_difference(set_2))

Summary

Here, we have explained "list", "tupple", "dictionary", and "set" as data types in Python. For the time being, I think it would be good if I could understand the "list" and "dictionary".

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized Python's data structures
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers have summarized the operators used in Python
How to install Python for pharmaceutical company researchers