(这是Python 3)
我试图写一个简单的函数来返回一个数字列表的平均值。在打印出平均值之前,我使用for
循环打印出列表中的值。我在列表中设置倒数第二个值时遇到问题,以不同于其他值的方式打印出来。我想要所有列表值之间的逗号直到倒数第二个值,其中我只需要一个空格,并在最后一个值之后使用一个fullstop。
如果我将列表定义为
list1 = [1, 3, 5, 7]
然后我想返回一个字符串:
"The values contained in the list are as follows: 1, 3, 5 and 7."
不幸的是,我设法得到的是:
"The values contained in the list are as follows: 1, 3, 5, and 7."
换句话说,我还没有能够区分倒数第二个列表值与其他列表值(我还没能在5之后删除逗号);虽然我已经能够区分列表中的最后一个值(即7)。
要分隔列表中的最后一个和倒数第二个值(即5和7),我将它们定义为变量:
finalElem2 = nlist[-1]
penultElem = nlist[-2]
然后我使用for
和and
语句排除这两个值。到目前为止,我的代码是:
def average1(nlist):
length = len(nlist)
finalElem2 = nlist[-1]
penultElem = nlist[-2]
sum_1 = 0
print("The values contained in the list are as follows: ",end="")
for i in nlist:
if i != finalElem2 and penultElem:
print(i, end=", ")
if i == finalElem2:
print("and ",i,".",sep="")
print("The length of the list is",length,"values.")
for i in nlist:
sum_1 = sum_1 + int(i)
average2 = sum_1 / length
print("The average is",average2)
就像我之前提到的那样,调用average1(list1)会返回"The values contained in the list are as follows: 1, 3, 5, and 7."
我原以为以下两行意味着列表值5(即penultElem,即倒数第二个列表值)最后没有用逗号打印,但是没有&似乎是这样的。
if i != finalElem2 and penultElem:
print(i, end=", ")
这就是我认为我添加的内容,如果penultElem没有被打印出来......那将会打印出来' 5'之后没有逗号。
if i == penultElem:
print(i)
我错过了一些非常明显的东西吗?如果我是道歉,我对此非常陌生。谢谢你的帮助!
答案 0 :(得分:1)
有一种更简单的方法。你可以unpack'你的清单到位了。这将起作用: -
"The values contained in the list are as follows: {}, {}, {} and {}.".format(*list1)
如果您的列表中包含不同数量的元素,那就不太好了。我只是想让你看到这个方法。
击>
经过进一步调查,我认为这是一个很好的方法: -
list1 = [1, 3, 5, 7]
strlist1 = [str(item) for item in list1] # Make it a list of str
"The values contained in the list are as follows: {} and {}.".format(', '.join(strlist1[:-1]), strlist1[-1])
提供所需的输出。
'列表中包含的值如下:1,3,5和7.'
只要您至少有两个元素,这将有效。
答案 1 :(得分:0)
我会这样推荐:
def average1(nlist):
length = len(nlist)
finalElem2 = nlist[-1]
penultElem = nlist[-2]
sum_1 = 0
print("The values contained in the list are as follows: ",end="")
for i in nlist:
if i not in (finalElem2, penultElem):
print(i, end=", ")
elif i == penultElem:
print(i, end="")
else:
print(" and ",i,".",sep="")
print("The length of the list is",length,"values.")
for i in nlist:
sum_1 = sum_1 + int(i)
average2 = sum_1 / length
print("The average is",average2)
average1(nlist)
这里的主要改进是使用单个if/elif/else
块来更有效地过滤您想要的结果。如果您根据列表中的数字来考虑哪些字符:某些字符以,
结尾,and
结尾,另一端以.
结尾所以这是一个很好的理由一个if/else
块来处理这些场景。
您还可以使用pop()
两次从列表末尾返回元素:
def average1(nlist):
length = len(nlist)
finalElem2 = nlist.pop()
penultElem = nlist.pop()
sum_1 = 0
print("The values contained in the list are as follows: ",end="")
for i in nlist:
print(i, end=", ")
print(penultElem, end=" and ")
print(finalElem2, end=".")
print()
print("The length of the list is",length,"values.")
for i in nlist:
sum_1 = sum_1 + int(i)
average2 = sum_1 / length
print("The average is",average2)
average1(nlist)
答案 2 :(得分:0)
这是一种使用join
来处理逗号部分的简单方法,然后只是添加'和'部分。诀窍是首先进行字符串转换,以便您可以使用join
。
def average1(nlist):
length = len(nlist)
sum_1 = 0
stringlist = [str(i) for i in nlist]
valuestring = ', '.join(stringlist[:-1])
if length > 1:
valuestring += ' and '
valuestring += stringlist[-1]
print("The values contained in the list are as follows:", valuestring + '.')
print("The length of the list is", length, "values.")
for i in nlist:
sum_1 = sum_1 + int(i)
average2 = sum_1 / length
print("The average is",average2)
答案 3 :(得分:-2)
如果i == penultElem,此声明不适用,因为这会导致尾随逗号:
if i != finalElem2 and penultElem:
print(i, end=", ")
将该部分更新为:
if i != finalElem2 and i != penultElem:
print(i, end=", ")
if i == penultElem:
print(i, end="")
if i == finalElem2:
print(" and ",i,".",sep="")
这里是完整更新的代码:
def average1(nlist):
length = len(nlist)
finalElem2 = nlist[-1]
penultElem = nlist[-2]
sum_1 = 0
print("The values contained in the list are as follows: ",end="")
for i in nlist:
if i != finalElem2 and i != penultElem:
print(i, end=", ")
if i == penultElem:
print(i, end="")
if i == finalElem2:
print(" and ",i,".",sep="")
print("The length of the list is",length,"values.")
for i in nlist:
sum_1 = sum_1 + int(i)
average2 = sum_1 / length
print("The average is",average2)
list1 = [1, 3, 5, 7]
average1(list1)
打印:
The values contained in the list are as follows: 1, 3, 5 and 7.
The length of the list is 4 values.
The average is 4.0