繁体   English   中英

如何将此 Python 变量分配给我正在循环的列表?

[英]How can I assign this Python variable to the list I am looping through?

感谢所有让我的第一篇 StackOverflow 帖子变得非常有用和有趣的人。

我一整天都在解决这个问题,并且已经到了这一点。 我几乎想通了。

说明:您的任务是将变量 names_and_ranks 分配给一个列表,其中每个元素都等于城市名称及其对应的排名。 例如,第一个元素是“1. Buenos Aires”,第二个元素是“2. Toronto”。 使用 for 循环和列表city_indicescity_names来完成此操作。 我们需要执行一些漂亮的字符串插值来正确格式化我们的字符串。 查看 f-string 插值以了解我们如何将值传递到字符串中。 请记住,列表索引从 0 开始,但我们希望我们的names_and_ranks列表从 1 开始!

这是我所拥有的:

代码city_indices只给我“11”所以我把它做成了一个列表

city_indices = list(range(0, len(cities)))
city_indices

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
city_names

['Buenos Aires',
 'Toronto',
 'Marakesh',
 'Albuquerque',
 'Los Cabos',
 'Greenville',
 'Archipelago Sea',
 'Pyeongchang',
 'Walla Walla Valley',
 'Salina Island',
 'Solta',
 'Iguazu Falls']

生成正确列表但未分配给正确变量的原始代码。 生成的列表应该分配给names_and_ranks

city_indices = -1
names_and_ranks = [city_names[city_indices]]
for elements in city_names:
        city_indices += 1
        print(f'{city_indices+1}. {city_names[city_indices]}')
# write a for loop that adds the properly formatted string to the names_and_ranks list

Returns:

1. Buenos Aires
2. Toronto
3. Marakesh
4. Albuquerque
5. Los Cabos
6. Greenville
7. Archipelago Sea
8. Pyeongchang
9. Walla Walla Valley
10. Salina Island
11. Solta
12. Iguazu Falls

此列表正确且编号正确。 但是,它似乎没有正确对应 names_and_ranks 变量。 事实上,当我运行时:

names_and_ranks[0]

我得到了列表中的最后一项 (#12).... 请记住,列表索引是 0 - 11,但我将输出编号为 0 - 12。

有任何想法吗? 一旦我分配了这个变量并运行一个成功的循环来显示一个从 1 开始的城市列表。我需要在新列表中打印不同的元素。

names_and_ranks[0]应返回“1.布宜诺斯艾利斯”

当前运行结果:

names_and_ranks[0] # should return '1. Buenos Aires'



IndexErrorTraceback (most recent call last)
<ipython-input-152-871e7a906308> in <module>
----> 1 names_and_ranks[0] # '1. Buenos Aires'
      2  # '2. Toronto'
      3  # '12. Iguazu Falls'

IndexError: list index out of range

使用枚举怎么样? 例如,您可以轻松使用索引,

for index, value in enumerate(['foo', 'bar', 'baz']):
    print(index, value)

这将返回 0 foo 1 bar 2 baz

这里使用 f'strings,返回与上面相同:

city_names = ['Buenos Aires',
 'Toronto',
 'Marakesh',
 'Albuquerque',
 'Los Cabos',
 'Greenville',
 'Archipelago Sea',
 'Pyeongchang',
 'Walla Walla Valley',
 'Salina Island',
 'Solta',
 'Iguazu Falls']

names_and_ranks = []
for index,city in enumerate(city_names):
        names_and_ranks.append(f'{index+1}. {city}')
print(names_and_ranks)

输出:['1。 布宜诺斯艾利斯','2。 多伦多','3。 马拉喀什','4。 阿尔伯克基','5。 洛斯卡沃斯','6。 格林维尔','7。 群岛海','8。 平昌', '9. 瓦拉瓦拉谷','10。 萨利纳岛','11。 索尔塔','12。 伊瓜苏瀑布']

编辑:因为提问者希望 names_and_ranks[8] 返回 '8。 平昌'这是编辑:

city_names = ['Buenos Aires',
 'Toronto',
 'Marakesh',
 'Albuquerque',
 'Los Cabos',
 'Greenville',
 'Archipelago Sea',
 'Pyeongchang',
 'Walla Walla Valley',
 'Salina Island',
 'Solta',
 'Iguazu Falls']

names_and_ranks = [0]
city_indices = 1
for city in city_names:
        names_and_ranks.insert(city_indices,f'{city_indices}. {city}')
        city_indices+=1

print(names_and_ranks[8])

这是一种方法:

names_and_ranks = [f'{i}. {x}' for i,x in enumerate(l, 1)]

print(names_and_ranks[0])
1. Buenos Aires

除了第一行之外,您的整个代码都是正确的:

city_indices = -1

哪个(我猜是一个索引)应该从 0 开始而不是从 -1 开始。

原因,

在 Python 中,-1 指向 list/string/.. 的最后一个元素,-2 作为倒数第二个元素,依此类推。 所以,我建议你应该只改变:city_indices = 0

然后根据这一行更改进一步的代码。

city_indices = 0
names_and_ranks = [city_names[city_indices]]
for elements in city_names:
    print(f'{city_indices+1}. {city_names[city_indices]}')
    city_indices += 1

在意识到我可能在相对简单的事情上花费了太多时间后,我继续前进。 我找到了解决方案手册。 这是正确答案。 我希望这可以帮助其他人在 Learn.co Flatiron School Data Science Pre-Work 上工作......

names_and_ranks = []
for elements in city_indices:
        names_and_ranks.append(f"{elements + 1}. {city_names[elements]}")

names_and_ranks

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM