Last active
April 14, 2024 22:07
-
-
Save Lazzlo2096/a9e1edfa5fc7b10fea0c0117fbd6a139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = ["apple", "pear", "orange"] | |
b = ["apple", "orange", "grape"] | |
""" | |
This program matches fruits between two lists, noting their indices or marking them as None if absent in either list. | |
Output will be: | |
[(0, 0, 'apple'), (1, None, 'pear'), (2, 1, 'orange'), (None, 2, 'grape')] | |
""" | |
enumerated_a = list(enumerate(a)) | |
enumerated_b = list(enumerate(b)) | |
def find_fruit_number(fruit, enumerated_list): | |
for index, value in enumerated_list: | |
if value == fruit: | |
return index | |
return None | |
#print("test", find_fruit_number("pear", enumerated_b)) | |
result = [] | |
for i in range(len(enumerated_a)): | |
a_index, a_value = enumerated_a[i] | |
b_index = find_fruit_number(a_value, enumerated_b) | |
result.append((a_index, b_index, a_value)) | |
for i in range(len(enumerated_b)): | |
b_index, b_value = enumerated_b[i] | |
a_index = find_fruit_number(b_value, enumerated_a) | |
if a_index is None: | |
result.append((None, b_index, b_value)) | |
print(result) | |
#result.sort(key=lambda x: x[0] if x[0] else 1000 ) | |
def print_table(result): | |
print("+-------+-------+--------+") | |
print("| Index A | Index B| Fruit |") | |
print("+-------+-------+--------+") | |
for item in result: | |
a_index, b_index, fruit = item | |
#print(f"| {a_index if a_index is not None else '-':^6} | {fruit:^6} | {b_index if b_index is not None else '-':^6} |") | |
print(a_index, "\t", b_index, "\t", fruit) | |
print("+-------+-------+--------+") | |
print_table(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment