简体   繁体   English

如何在 Python 中创建将产生以下结果的列表 []

[英]How to create a list in Python that will yield the following result []

This a question on coursera practices excess.这是一个关于coursera实践过度的问题。

Create a new list using the 9th through 12th elements (four items in all) of new_lst and assign it to the variable sub_lst.使用 new_lst 的第 9 到第 12 个元素(共四项)创建一个新列表,并将其分配给变量 sub_lst。

link to the question: https://runestone.academy/runestone/books/published/fopp/Sequences/TheSliceOperator.html The last question on the list.问题链接: https : //runestone.academy/runestone/books/published/fopp/Sequences/TheSliceOperator.html列表中的最后一个问题。

How is my code我的代码怎么样

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", 
["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, 
"party", "cake", 5], "rain", "thunderstorm", "top down"]
new_lst = new_lst[9:12]
sub_lst = new_lst
print(sub_lst)

My output:我的输出:

[['water', 'air', 'fire', 'earth'], 'games', 2.7]

But here is the expected output:但这是预期的输出:

['sun', ['water', 'air', 'fire', 'earth'], 'games', 2.7]

Please why am I not getting the expected output?请问为什么我没有得到预期的输出?

You need to start indexing at 8 as The 9th element is at index 8.您需要从 8 开始索引,因为第 9 个元素位于索引 8。

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"]

sub_lst=new_lst[8:8+4]

Resulting output will be -结果输出将是 -

['sun', ['water', 'air', 'fire', 'earth'], 'games', 2.7]

你的第 9 个元素是 ['water', 'air', 'fire', 'earth'] (它被包括在内)而第 12 个元素是“代码”(它被排除在 python 中)所以结果是 [['water', '空气', '火', '地球'], '游戏', 2.7]

If you do a simple output of the 9th through 12th items of the list如果你对列表的第 9 到 12 项做一个简单的输出

item_count = 0
for item in new_lst:
    item_count += 1
    if 9<=item_count<=12:
        print(str(item_count)+" "+str(item))

you get the output你得到输出

9 sun
10 ['water', 'air', 'fire', 'earth']
11 games
12 2.7

which is not an empty list.这不是一个空列表。

To get the sub-list of the 9th through 12th items use a slice that starts at 8 based on a zero-based index and ends at 12 because the last item is not included in the output slice.要获取第 9 到第 12 项的子列表,请使用基于从 0 开始的索引从 8 开始并以 12 结束的切片,因为最后一项不包含在输出切片中。

sub_lst = new_lst[8:12]

which prints as打印为

print(sub_lst)
['sun', ['water', 'air', 'fire', 'earth'], 'games', 2.7]

Here is the code I executed in the link you have provided:这是我在您提供的链接中执行的代码:

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"]
sub_lst = new_lst[8:12]
print(sub_lst)

This line in the code you have written, you have updated the value of new_lst to only 4 elements.您编写的代码中的这一行,您已将 new_lst 的值更新为仅 4 个元素。

new_lst = new_lst[9:12]

So, it is showing the expected value as [].因此,它将预期值显示为 []。

I think you are assigning more than the question asked and here is the solution我认为您分配的问题超出了所提出的问题,这是解决方案

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"]
sub_lst = new_lst[8:12]

just do "sub_lst = new_lst[8:12]" assign just sliced list in sub_lst.只需执行“sub_lst = new_lst[8:12]”在sub_lst 中分配刚刚切片的列表。

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

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