You want to write the following Python dictionary values in a spreadsheet in the form of a table.
pattern = {
'area': ['Japan', 'America', 'England'],
'sex': ['Man', 'woman'],
'height': ['100 cm or more', 'Less than 100 cm']
}
If you want to create a table that multiplies the following elements in a spreadsheet,
area | sex | height | |
---|---|---|---|
1 | Japan | Man | 100 cm or more |
2 | Japan | Man | Less than 100 cm |
3 | Japan | woman | 100 cm or more |
4 | Japan | woman | Less than 100 cm |
5 | America | Man | 100 cm or more |
6 | America | Man | Less than 100 cm |
7 | America | woman | 100 cm or more |
8 | America | woman | Less than 100 cm |
9 | England | Man | 100 cm or more |
10 | England | Man | Less than 100 cm |
11 | England | woman | 100 cm or more |
12 | England | woman | Less than 100 cm |
If you write the following in Python, comma-separated ones will be output.
from itertools import product
if __name__ == '__main__':
pattern = {
'area': ['Japan', 'America', 'England'],
'sex': ['Man', 'woman'],
'height': ['100 cm or more', 'Less than 100 cm']
}
delimiter = ','
result = delimiter + delimiter.join(pattern.keys()) + '\n'
i = 0
for item in product(*pattern.values()):
i += 1
result += str(i) + delimiter + delimiter.join(item) + '\n'
print(result)
Paste it into a spreadsheet to create a nice table.
,area,sex,height
1,Japan,Man,100 cm or more
2,Japan,Man,Less than 100 cm
3,Japan,woman,100 cm or more
4,Japan,woman,Less than 100 cm
5,America,Man,100 cm or more
6,America,Man,Less than 100 cm
7,America,woman,100 cm or more
8,America,woman,Less than 100 cm
9,England,Man,100 cm or more
10,England,Man,Less than 100 cm
11,England,woman,100 cm or more
12,England,woman,Less than 100 cm
itertools.product
to make a product of multiple lists. Since this function is a variadic argument, it is written as * pattern.values ()
to expand the list.i
used in for
can also be created using emumerate
. The following code uses enumerate
.If you want to create the following table for each item and its value in the above multiplication table
item | value |
---|---|
area | Japan |
America | |
England | |
sex | Man |
woman | |
height | 100 cm or more |
Less than 100 cm |
If you write the following in Python, comma-separated ones will be output.
if __name__ == '__main__':
pattern = {
'area': ['Japan', 'America', 'England'],
'sex': ['Man', 'woman'],
'height': ['100 cm or more', 'Less than 100 cm']
}
delimiter = ','
result = ''
for key, value in pattern.items():
for index, item in enumerate(value):
if index == 0:
result += key
result += delimiter + item + '\n'
print(result)
Paste it into a spreadsheet to create a nice table.
area,Japan
,America
,England
sex,Man
,woman
height,100 cm or more
,Less than 100 cm
If this is made into one Python file that can be advanced interactively, it will be as follows.
from itertools import product
def get_input(default: str = ''):
value: str = input().rstrip("\n")
if value:
return value
return default
def configure():
print("""
Please configure
delimiter [,]:
""")
delimiter = get_input(',')
print(f"""
prefix:
""")
prefix = get_input()
print(f"""
suffix:
""")
suffix = get_input()
return delimiter, prefix, suffix
def print_product_table(pattern: dict, delimiter: str, prefix: str, suffix: str):
result = prefix + delimiter + delimiter.join(pattern.keys()) + suffix + '\n'
i = 0
for item in product(*pattern.values()):
i += 1
result += prefix + str(i) + delimiter + delimiter.join(item) + suffix + '\n'
print(result)
def print_category_key_value(pattern, delimiter, prefix, suffix):
result = ''
for key, value in pattern.items():
for index, item in enumerate(value):
line = prefix
if index == 0:
line += key
line += delimiter + item + suffix
result += line.strip() + '\n'
print(result)
if __name__ == '__main__':
pattern = {
'area': ['Japan', 'America', 'England'],
'sex': ['Man', 'woman'],
'height': ['100 cm or more', 'Less than 100 cm']
}
print_pattern: int = None
while True:
print("""
Print dict structure.
Enter print pattern:
1. Product table
2. Category key-value
""")
print_pattern = int(get_input())
if print_pattern in [1, 2]:
break;
delimiter, prefix, suffix = configure()
if print_pattern == 1:
print_product_table(pattern, delimiter, prefix, suffix)
elif print_pattern == 2:
print_category_key_value(pattern, delimiter, prefix, suffix)
Recommended Posts