【问题标题】:How to use a value from one list in another list?如何在另一个列表中使用一个列表中的值?
【发布时间】:2021-11-01 16:51:42
【问题描述】:

如何遍历一个列表中的值并在另一个列表中使用它们? 假设我有一个名为fruit 的列表和另一个名为jobs 的列表

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

我希望它说水果的地方是水果列表中的水果。 所以它会返回 '清理苹果'、'清理梨'、'清理香蕉'

【问题讨论】:

  • [j.replace('fruit',f) for j,f in zip(jobs,fruit)]

标签: python list loops parameterized


【解决方案1】:

用途:

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

【讨论】:

  • 固定@Barmar。谢谢!
猜你喜欢
  • 2015-02-07
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多