如何在另一个列表中使用一个列表中的值?
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']
我希望它说水果的地方是水果列表中的水果。
所以它会 return
'clean the apple'、'clean the pear'、'clean the banana'
使用:
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
如何遍历一个列表中的值并在另一个列表中使用它们? 假设我有一个名为 fruit 的列表和另一个名为 jobs
的列表fruit = ['apple','pear','banana']
jobs =['clean the fruit', 'cut the fruit', 'eat the fruit']
我希望它说水果的地方是水果列表中的水果。 所以它会 return 'clean the apple'、'clean the pear'、'clean the banana'
使用:
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