该程序与
的预期功能一起使用例如:
Student_Names= []
Marks= []
StudentNames_Marks= []
for i in range (5): #Go through and append the student names into Student_Names list by requesting the information five times and appending each time
_name= input("Please enter a name for the student records list. ")
Student_Names.append(_name)
_sum=0 #This is used to create a second for loop under the event that they do not enter a digit within the first list, allowing them to continue forward
for i in range (5):
_marks= input("Please enter marks in digit format. ")
if _marks.isalpha():
print("Please enter a digit, such as 10 instead of ten.")
_sum=_sum+1 #Add one to the sum to create the amount of loops in the second for loop
else:
Marks.append(_marks)
if _sum>0:
for i in range(_sum):
_marks=input("Please enter marks in digit format. ")
if _marks.isalpha():
print("Please enter a digit, such as 10 instead of ten. The program will not run properly. Please restart the program.")
else:
Marks.append(_marks)
_numbera= len(Student_Names) #Using the lengths of the list to determine the length of the for loop
_numberb= len(Marks)
_length= _numbera+_numberb
for i in range (5):
_value=Student_Names.pop()
StudentNames_Marks.append(_value)
_value=Marks.pop()
StudentNames_Marks.append(_value)
print(StudentNames_Marks)
如果在提示符中输入以下内容,则会在控制台上生成以下内容:
姓名提示回复:James,Jack,Ivy,Eva,Lee Marks:10,64,36, 86,35 Student_Names = ['James','Jack','Ivy','Eva','Lee'] Marks = ['10','64','36','86','35'] StudentNames_Marks = ['Lee','35', '伊娃','86','常春藤','36','杰克','64','詹姆斯','10'
然而,我坚持开发最后一步。我已经获得了一个列表,该列表以正确的顺序从前两个列表中获取元素,如上所示。我不确定如何将最终列表中的元素组合成一个破折号,所以看起来更像是这样:
StudentNames_Marks = ['Lee-35','Eva-86','Ivy-36','Jack-64', '詹姆斯10']
我该怎么做呢?
答案 0 :(得分:1)
使用zip
和list comprehension
的一个班轮:
>>> ['-'.join(ele) for ele in zip(student_names, marks)]
此处,zip
命令用于创建组合student_name
和marks
的元组,然后使用join
函数将它们连接在一起。
#driver values:
IN : student_names= ['James', 'Jack', 'Ivy', 'Eva', 'Lee']
IN : marks = ['10', '64', '36', '86', '35']
OUT : ['James-10', 'Jack-64', 'Ivy-36', 'Eva-86', 'Lee-35']
答案 1 :(得分:1)
替代方式(仅使用一个列表):
您可以使用zip
一次从一个列表中获取两个项目,并在它们之间添加-
。
StudentNames_Marks= ['Lee', '35', 'Eva', '86', 'Ivy', '36', 'Jack', '64', 'James', '10']
def group(iterable, n):
return zip(*[iter(iterable)]*n)
StudentNames_Marks_new = []
for x, y in group(StudentNames_Marks, 2):
StudentNames_Marks_new.append('{}-{}'.format(x, y))
print(StudentNames_Marks_new)
# ['Lee-35', 'Eva-86', 'Ivy-36', 'Jack-64', 'James-10']
答案 2 :(得分:1)
student_names= ['James', 'Jack', 'Ivy', 'Eva', 'Lee']
marks = ['10', '64', '36', '86', '35']
result = ["{}-{}".format(name, mark) for name, mark in zip(student_names, marks)]
答案 3 :(得分:1)
要将最终输出转换为所需的输出,您可以使用:
StudentNames_Marks = ['Lee', '35', 'Eva', '86', 'Ivy', '36', 'Jack', '64', 'James', '10']
StudentNames_Marks = ['-'.join(nm) for nm in zip(StudentNames_Marks[::2],StudentNames_Marks[1::2]]
StudentNames_Marks
#['Lee- 35', 'Eva- 86', 'Ivy- 36', 'Jack- 64', 'James- 10']
答案 4 :(得分:1)
你可以使用理解力:
# combine every name with the corresponding mark
def combine(names, marks):
if len(names) != len(marks):
# throw an exception or return some error value
pass
else:
return [name + '-' + mark for name, mark in zip(names, marks)]