簡體   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