簡體   English   中英

如何在另一個列表中使用一個列表中的值?

[英]How to use a value from one list in another list?

我如何遍歷一個列表中的值並在另一個列表中使用它們? 假設我有一個名為fruit 的列表和另一個名為jobs 的列表

fruit = ['apple','pear','banana']
jobs =['clean the fruit', 'cut the fruit', 'eat the fruit']

我希望它所說的水果是列表水果中的水果。 所以它會返回“清理蘋果”、“清理梨”、“清理香蕉”

用:

fruits = ['apple','pear','banana']
jobs =['clean the fruit', 'cut the fruit', 'eat the fruit']

for job in jobs:
    for fruit in fruits:
        print(job.replace("fruit", fruit))

輸出

clean the apple
clean the pear
clean the banana
cut the apple
cut the pear
cut the banana
eat the apple
eat the pear
eat the banana

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM