为什么要创建 x 变量以及此代码中使用的逻辑是什么
Why x variable is created and what is the logic used in this code
names = [ "Rachael Green", "Goodfellow Ian", "Tedd Crock", "Mina Joseph"]
salaries = [10260 , 41571 , 71211 , 52141 , 35781]
people_salaries = []
for i , j in enumerate(names):
x = j + " $" + str(salaries[i])
people_salaries.append(x)
这看起来很简单,但需要查看枚举的实现:
enumerate()
是 python 内置模块提供了以生成器格式管理列表索引的能力,这是一种节省内存的方式。
现在检查枚举在这种情况下是如何工作的:
列表(枚举(姓名))
[(0, 'Rachael Green'),
(1, 'Goodfellow Ian'),
(2, 'Tedd Crock'),
(3, 'Mina Joseph')]
- 它是一个元组列表,索引从“0”开始分配给名称列表
- for 循环正在遍历此列表并创建一个字符串[在 'x'] 中标记名称与薪水
- 追加空列表 'people_salaries'
最后你会得到列表'people_salaries',其中提到了姓名和薪水
此处创建变量 x 以创建将附加到列表的临时字符串。枚举将创建一个元组,其中包含一个数字,例如与迭代的每个项目配对的索引(这就是循环需要 2 个值 (i,j)
的原因)。然后,代码将采用枚举数字并将其用作薪水的索引。
我会建议 1. 创建一个包含姓名和薪水的字典,以及 2. 您不需要 x 作为代码,只需执行 people_salaries.append( j + '$' + str(salaries[i]))
将for循环而不是枚举更改为for i in salaries
或简单地使用字典方法
for i in people_dict.keys()
然后追加
people_salaries.append(i + '$' + str(people_dict[i]))
在上面的代码中,
names = [ "Rachael Green", "Goodfellow Ian", "Tedd Crock", "Mina Joseph"]
salaries = [10260 , 41571 , 71211 , 52141 , 35781]
people_salaries = []
for i , j in enumerate(names):
x = j + " $" + str(salaries[i])
people_salaries.append(x)
来自for循环的解释:
for i , j in enumerate(names):
# this enumerate defragment the items of the list you give here name has several
# name and all the names will will be passed to j one by one and
# index value will be passed to i
x = j + " $" + str(salaries[i])
# In this statemnt x is string value in which name and salary along with us currency
#is assigned.
#for eg: in first iteration
# x = "Rachael Green" + "$" + str(10260)
现在在最后一个声明中:
people_salaries.append(x) # in this all x string will be appended
这发生在名字的末尾
names = [ "Rachael Green", "Goodfellow Ian", "Tedd Crock", "Mina Joseph"]
salaries = [10260 , 41571 , 71211 , 52141 , 35781]
people_salaries = []
for i , j in enumerate(names):
x = j + " $" + str(salaries[i])
people_salaries.append(x)
这看起来很简单,但需要查看枚举的实现:
enumerate()
是 python 内置模块提供了以生成器格式管理列表索引的能力,这是一种节省内存的方式。
现在检查枚举在这种情况下是如何工作的:
列表(枚举(姓名))
[(0, 'Rachael Green'), (1, 'Goodfellow Ian'), (2, 'Tedd Crock'), (3, 'Mina Joseph')]
- 它是一个元组列表,索引从“0”开始分配给名称列表
- for 循环正在遍历此列表并创建一个字符串[在 'x'] 中标记名称与薪水
- 追加空列表 'people_salaries'
最后你会得到列表'people_salaries',其中提到了姓名和薪水
此处创建变量 x 以创建将附加到列表的临时字符串。枚举将创建一个元组,其中包含一个数字,例如与迭代的每个项目配对的索引(这就是循环需要 2 个值 (i,j)
的原因)。然后,代码将采用枚举数字并将其用作薪水的索引。
我会建议 1. 创建一个包含姓名和薪水的字典,以及 2. 您不需要 x 作为代码,只需执行 people_salaries.append( j + '$' + str(salaries[i]))
将for循环而不是枚举更改为for i in salaries
或简单地使用字典方法
for i in people_dict.keys()
然后追加
people_salaries.append(i + '$' + str(people_dict[i]))
在上面的代码中,
names = [ "Rachael Green", "Goodfellow Ian", "Tedd Crock", "Mina Joseph"]
salaries = [10260 , 41571 , 71211 , 52141 , 35781]
people_salaries = []
for i , j in enumerate(names):
x = j + " $" + str(salaries[i])
people_salaries.append(x)
来自for循环的解释:
for i , j in enumerate(names):
# this enumerate defragment the items of the list you give here name has several
# name and all the names will will be passed to j one by one and
# index value will be passed to i
x = j + " $" + str(salaries[i])
# In this statemnt x is string value in which name and salary along with us currency
#is assigned.
#for eg: in first iteration
# x = "Rachael Green" + "$" + str(10260)
现在在最后一个声明中:
people_salaries.append(x) # in this all x string will be appended
这发生在名字的末尾