python-3.x 有人能解释一下代码吗??为什么使用wE和wF?[副本]

tf7tbtn2  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(137)

此问题在此处已有答案

Don't understand this python For loop(4个答案)
What is print(f"...")(7个答案)
4天前关闭。

listFrench = ['un', 'deux', 'trois', 'quatre', 'cinq']
listEnglish = ['one', 'two', 'three', 'four', 'five']
for wF, wE in zip(listFrench, listEnglish):
    print(f"The French word for {wE} is {wF}")

字符串
我不明白wE和aF的用法,以及print语句中使用的f

rpppsulh

rpppsulh1#

打开python shell并进行实验。首先,zip返回的是:

>>> listFrench = ['un', 'deux', 'trois', 'quatre', 'cinq']
>>> listEnglish = ['one', 'two', 'three', 'four', 'five']
>>> for x in zip(listFrench, listEnglish):
...     print(x)
... 
('un', 'one')
('deux', 'two')
('trois', 'three')
('quatre', 'four')
('cinq', 'five')

字符串
zip返回元组,每个元组中有2个值。事实上,它从每个输入列表中产生项目。它可以接受任意数量的输入列表(任何可迭代的东西,真的),并依次返回列表中每个项目的元组。
现在,让我们看看第一个元组会发生什么

>>> wF, wE = ('un', 'one')
>>> wF
'un'
>>> wE
'one'


这是元组解包。我们在左手有两个变量,在右边有一个包含两个元素的元组。所以python分配了变量。
最后,“f”代表Augmented String Interpolation。它允许您从程序中已经定义的数据格式化字符串。

>>> f"The French word for {wE} is {wF}"
'The French word for one is un'

相关问题