Python 如何“交替”显示两个列表的内容?
我试图实现能够以以下方式显示两个列表的内容,或者至少类似于以下方式:Python 如何“交替”显示两个列表的内容?,python,list,for-loop,Python,List,For Loop,我试图实现能够以以下方式显示两个列表的内容,或者至少类似于以下方式: list_1 = [price1, price2, price3, price_n] list_2 = [concept1, concept2, concept3, concept_n] 打印时,我希望显示如下信息: price1 concept1 price2 concept2 你明白了吗 我在使用for循环,但是,我不知道如何包含第二个列表,以便在同一个for上显示,或者如何实现这一点 感谢您提供的任何反馈。
list_1 = [price1, price2, price3, price_n]
list_2 = [concept1, concept2, concept3, concept_n]
打印时,我希望显示如下信息:
price1
concept1
price2
concept2
你明白了吗
我在使用for循环,但是,我不知道如何包含第二个列表,以便在同一个for上显示,或者如何实现这一点
感谢您提供的任何反馈。函数zip是专门为此而设计的-并排迭代两个序列:
for price, concept in zip(list_1, list_2):
print(price)
print(concept)
函数zip是专门为此而设计的-并排迭代两个序列:
for price, concept in zip(list_1, list_2):
print(price)
print(concept)
你在找这个吗
你在找这个吗?那么,当两个列表的长度不一样时会发生什么呢?那么,当两个列表的长度不一样时会发生什么呢?它就像一个符咒,我很感激,我不知道这是一件事。我也会理解这个概念。它就像一个符咒,我很感激我没有意识到这是一件事。我也会理解这个概念。