Question about Python list.

Asked 2 years ago, Updated 2 years ago, 48 views

import random

#Types of food 
food_kind = ["Korean food", "Chinese food", "Western food", "Japanese food", "Asian food", "Snack food"]
food_kind_detail = ["food_kor", "food_chi", "food_west", "food_jpa", "food_asia", "food_sfod"]
food_recipe = ["Stir-fried", "Steamed/Tang/Jjigae", "Fried", "Grilled", "Bread/Rice/Noodle"]
food_recipe_detail = ["food_stir", "food_soup", "food_fry", "food_rst", "food_meal"]

print("Select 1: ", food_kind[a])
print("Select 1: ", food_kind_detail[a])
print("Select 2: ", food_recipe[b])
print("Select 2: ", food_recipe_detail[b])

#Types of food
food_kind_detail = ["food_kor", "food_chi", "food_jpa", "food_west", "food_asia", "food_sfod"]
#Korean food
food_kor = ["Grilled mackerel", "Pork belly", "Kimchi stew", "Samgyetang", "Steamed short ribs", "Beef bulgogi with rice", "Squid with rice", "Stir-fried spicy pork"]
#Chinese food
food_chi = ["Mara Xiangguo", "Tangsuyuk", "Yusanseul", "Jajangmyeon", "Mapa Tofu", "Jjamppong", "Woolmyeon", "Malatang"]
#Japanese food
food_jpa = ["Sushi", "Udon", "Ramen", "Donkatsu", "Nabe", "Soba", "Takoyaki", "Oden"]
#Form
food_west = ["Steak", "Burger", "Pasta", "Pizza", "Sandwich", "Bread", "Resorto", "Chicken"]

#Asian
food_asia = ["Curry", "Rice noodles", "Dumpling", "Wolnamssam", "Bun cha", "Nasi goreng", "Pad thai", "Chajo"

#Snacks
food_sfod = ["Dumpling", "Gimbap", "Tteokbokki", "Rapbokki", "Rap rice", "Hot dog", "Chicken skewers", "Udon"]

#How to cook food
food_recipe_detail = ["food_stir", "food_soup", "food_fry", "food_rst", "food_meal"]

#Stir-fried
food_stir = ["Stir-fried spicy pork", "Malaxianguo", "Mapa tofu", "Nasi goreng", "Pad thai", "Tteokbokki", "Rapokki", "Yusanseul"]
#Steamed/tang/jjigae
food_soup = ["Kimchi stew", "Samgyetang", "Soba", "Nabe", "Dumpling", "Steamed Short Ribs", "Jjamppong", "Woolmyeon", "Malatang", "Rice Noodles", "Udon", "Ramen", "Odeng"]
#Fried food
food_fry = ["Tangsuyuk", "Donkatsu", "Chicken", "Chajo", "Hot dog", "Chicken skewer"]
#Grilled
food_rst = ["Grilled mackerel", "Pork belly", "Takoyaki", "Steak", "Rice rolls"]
#Bread/Rice/Noodle
food_meal = ["Squid Rice with Rice", "Beef Bulgogi Rice", "Jajangmyeon", "Sushi", "Crying", "Jjamppong", "Udon", "Ramen", "Sashimi", "Hamburger", "Pasta", "Sandwich",
             "Bread", "Resort", "Rice noodles", "Rice noodles", "Bun cha", "Nasi goreng", "Pad Thai", "Gimbap", "Rice balls"]

menu = (set(food_kind_detail[a]) & set(food_recipe_detail))
print(menu)
print(food_kind_detail[a])

Question

For example, if the value food_kind_detail[a] is food_kor and the value food_recipe_detail[b] is food_stir I knew that food_kor and food_stir would be converted to their own list, respectively.
In other words, we tried to derive overlapping parts by converting the food_kor and food_stir lists into sets.
However, when you run the code, only the overlapping parts of the food_kor and food_stir characters are derived.
What I want is to use the value shown in food_kind_detatil[a] as a list, what should I do?

python list

2022-09-20 13:33

1 Answers

food_kind_detail = ["food_kor", "food_chi", "food_jpa", "food_west", "food_asia", "food_sfod"]

Cut this line and paste it behind the food_sfod = blah line and erase all the quotes. Then food_kind_detail will refer to variable such as food_kor.
Modify food_recipe_detail similarly, and then study it by fixing it like this.

But you know, in the real world, these materials are typically designed and managed in this way. If you can change the data structure now, please refer to the information below seriously.

# This configuration is often referred to as an "array of objects".
all_menu = [
    {'name': "pork belly", 'kind': "kor", 'recipe': "roast"},
    {'name': "Takoyaki", 'kind': "jpa", 'recipe': "roast"}
    # In this way, the same menu dict is connected in a line.
]

# As you can see, it's intuitive and well organized.
the_random_recipe = "roast"
for menu in all_menu :
    if menu['kind'] is "kor" and menu['recipe'] is the_random_recipe :
        print(menu['name'])


2022-09-20 13:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.