简体   繁体   English

Python 3.3列表协助

[英]Python 3.3 list assistance

I am trying to write a code that will basically ask the user to input 5 names. 我正在尝试编写一个代码,该代码将基本上要求用户输入5个名称。 It will then create a list of those names, and print them out. 然后,它将创建这些名称的列表,并打印出来。 Subsequently, it prints a sorted version of the list. 随后,它打印列表的排序版本。 Next, the code will print the third name in the list. 接下来,代码将在列表中打印第三个名称。 After this step, the code asks the user which of the names to change whereupon the user then inputs a new name for the one they chose to replace. 此步骤之后,代码会询问用户要更改的名称中的哪一个,然后用户为他们选择替换的名称输入一个新名称。

This should be the output, example: 这应该是输出,例如:

The names are: "Sal", "Jane", "Fred", "Bob", "Cole" 名称是: "Sal", "Jane", "Fred", "Bob", "Cole"

The sorted names are: "Bob", "Cole", "Fred", "Jane", "Sal" 排序的名称是: "Bob", "Cole", "Fred", "Jane", "Sal"

The third name in the list is: "Fred" (This will be the third name from the sorted list) 列表中的第三个名称是: "Fred" (这是已排序列表中的第三个名称)

The names are: "Bob", "Joe", "Fred", "Jane", "Sal" (Assuming the user chose to replace the second name. 名称是: "Bob", "Joe", "Fred", "Jane", "Sal" (假设用户选择替换第二个名字。

So far this is what I have for code: 到目前为止,这就是我要编写的代码:

name_list = input ("Please list 5 names here:")
name_list = name_list.split()
name_list = [name_list]
print ("The names in your list are:", name_list)
print ("The sorted list is:", name_list)
print(name_list[2:3])

The issues I'm having are for one, I cant figure out why the sorted list isn't "sorting" correctly, and secondly, the last line should be printing out the third name, but prints [] instead. 我遇到的问题是一个,我无法弄清楚为什么排序后的列表未正确“排序”,其次,最后一行应该打印出第三个名称,而打印[]

First of all you are not sorting the list before printing out the sorted list , you should call name_list.sort() before printing sorted list. 首先,在打印出已排序的列表之前,您name_list.sort()列表进行排序,应该在打印已排序的列表之前调用name_list.sort()

Secondly , name_list[<start>:<end>] prints out the list starting at <start> index , and ending at <end> - 1 index , hence you are getting the empty list for name_list[2:3] . 其次, name_list[<start>:<end>]打印出从<start>索引<start> ,到<end> - 1索引<end> - 1 ,因此您将获得name_list[2:3]的空列表。 You should do name_list[2] instead. 您应该name_list[2]

It isn't sorting because you haven't sorted it, you've just placed it inside another list. 它不是在排序,因为您尚未对其进行排序,只是将其放置在另一个列表中。 Just call name_list.sort() instead. 只需调用name_list.sort()

The slicing [2:3] isn't working because after calling name_list = [name_list] it's a list with a single element, which is itself a list of names. 切片[2:3]不起作用,因为调用name_list = [name_list]它是一个包含单个元素的列表,它本身就是一个名称列表。 This list does not have an element in index 2, and hence an empty list is returned when you slice it like that. 该列表在索引2中没有元素,因此,当您对它进行切片时,将返回一个空列表。

name_list=input("enter the list of names ")
name_list.sort()
print "sorted name list is ",name_list
print "Third name is ",name_list[2:3]
item= input("enter the name to be modified ")
print "position of the name to be modified is",name_list.index(item)
print("enter a name to modify "+item+ "!")
newname= input("name: ")
position=name_list.index(item)
name_list[position]=newname
name_list.sort()
print"modifified list is ",name_list

This should work. 这应该工作。

name_list = input ("Please list 5 names here:")
name_list = name_list.split()
print ("The names in your list are:", name_list)
print ("The sorted list is:", name_list.sort())  #sorting list
print(name_list[2])   # printing 3rd element of the list as indices start from 0

To find the index of element to be replaced (say "Fred") you can do: 要找到要替换的元素的索引(例如“ Fred”),可以执行以下操作:

index_to_be_replaced = name_list.index("Fred")

Once you find the index just execute : 找到索引后,只需执行:

name_list[index_to_be_replaced] = new_element to be inserted

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

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