第4章末尾的“自动化镗孔”一书中的原始问题
逗号代码[用Python 3.x编写]
假设你有一个像这样的列表值:
spam = ['apples', 'bananas', 'tofu', 'cats']
编写一个以列表值作为参数并返回a的函数 包含逗号和空格的所有项目的字符串,
'and'
在最后一项之前插入。例如,传递前一个 该函数的spam
列表将返回'apples, bananas, tofu, and cats'
。但是您的函数应该能够使用任何列表值 传递给它。
我的问题是:
我希望修改用Python 3.x编写的结果代码,以便在命令提示符下输入一个列表,例如:1, 2, 3, 4, 5
并将程序放出1, 2, 3, 4 and 5
我已完成此部分的操作将从我的(很长)代码中看到。当我有一个数字和名称或单词列表并且想要这个结果时,问题出现了,我在命令提示符下键入:1, 2, 3, 4, jeremy, anna
并想要以下输出:1, 2, 3, 4, jeremy and anna
正如您将从我的代码中看到的那样,我遗漏了字母表的其余部分,只是为了找到解决方案而保持简短。我试图解决这个问题的方法是使用正则表达式函数,如搜索等,并在输出中搜索逗号,逻辑是获取这些逗号的位置,然后用'and'
字符串替换最后一个逗号。但我找不到任何可以实现的目标。
P.S。我的代码(下面)使用了Stack Overflow上的某个人给出的原始逗号代码问题的答案,请参阅here
#! python3.
#import re
my_input = list(input('Please enter a list of words and or numbers separated by commas:\n'))
def commacode(listname):
#if the last entry is equal to a number between 0 and 9 put an "and" before it then print the modified input to screen
if listname[len(listname) - 1] == '0':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '1':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '2':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '3':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '4':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '5':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '6':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '7':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '8':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == '9':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1]
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
elif listname[len(listname) - 1] == 'a':
listname[len(listname) - 1] = 'and ' + listname[len(listname) -1] #This is where I need the help in this elif statement....
index = 0
new_string = listname[index]
while index < len(listname)-1:
new_string = new_string + listname[index + 1]
index = index + 1
if index == len(listname)-1:
print('\n')
print(new_string)
else:
print('\n')
print('Please enter lower case letters')
exit
commacode(my_input)
答案 0 :(得分:0)
为什么这么久? 这是我的:
spam = ['apple', 'bananas', 'tofu', 'cats']
def func(a):
a[-1] = 'and '+ a[-1]
print(a)
func(spam)
顺便说一句,我正在读这本书,但我还没有达到其他的东西,我是否回答了你的问题?
答案 1 :(得分:0)
感谢您的努力。它并没有完全回答我的问题。来自另一个论坛的代码确实回答了我的问题:
#Celine Hagbard from reddit.com slightly modified. It works!
s = input('Please enter a list of words and or numbers separated by commas:\n')
print(" and ".join(s.rsplit(",",1)))
是的,比我能管理的线少得多。
答案 2 :(得分:0)
spam = ['apples', 'bananas', 'tofu', 'cats']
# function
def func(list):
string = ''
for item in list:
string = string + str(item)
# adding 'and' as last second item in the string
if list.index(item) == (len(list)-2):
string = string + ', and '
# No comma after last item in the string
elif list.index(item) == (len(list)-1):
string = string
else:
string = string + ', '
return string
print(func(spam))
苹果,香蕉,豆腐和猫
答案 3 :(得分:0)
spam = [1, 2, 3, 4, 'jeremy', 'anna']
# Function
def func(list):
string = ''
for item in list:
string = string + str(item)
# adding 'and' as last second item in the string
if list.index(item) == (len(list)-2):
string = string + ', and '
# No comma after last item in the string
elif list.index(item) == (len(list)-1):
string = string
else:
string = string + ', '
return string
# function calling
print(func(spam))
1、2、3、4,杰里米和安娜