我正在尝试解决一个要求以下内容的练习:
创建一个字符串,该字符串是由空格分隔的一连串单词
使用split将字符串转换成单词列表
从列表中删除三个单词,但使用另一种Python操作将每个单词删除
对列表进行排序
使用三种不同的Python操作将新单词添加到列表中
使用join将单词列表变回单个字符串
打印字符串
这就是我所做的:
def clubs(t):
england = 'Tottenham Burnley Newcastle Leicester Liverpool Chelsea Cardiff Everton Arsenal'
t = england.split()
t = england.pop(1)
del england[3]
england.remove('Newcastle')
england.sort()
england.append('Juventus')
spain = 'Sevilla, Barcelona'
england.extend(spain)
england.insert('Lazio')
delimiter = ' '
t = delimiter.join(england)
print(england)
我期望的输出是:
热刺利物浦切尔西加的夫埃弗顿阿森纳尤文图斯塞维利亚 巴塞罗那拉齐奥
有人可以帮助我找出问题所在吗? 谢谢!
答案 0 :(得分:1)
您可以尝试以下方法吗?
/api/v2/
输出:
def clubs():
england = 'Tottenham Burnley Newcastle Leicester Liverpool Chelsea Cardiff Everton Arsenal'
england = england.split()
t = england.pop(1)
del england[2]
england.remove('Newcastle')
# england.sort()
england.append('Juventus')
spain = 'Sevilla, Barcelona'
england.extend(spain.split(','))
england.insert(len(england), 'Lazio')
delimiter = ' '
t = delimiter.join(england)
print(england)
print(t)
clubs()
说明:
['Tottenham', 'Liverpool', 'Chelsea', 'Cardiff', 'Everton', 'Arsenal', 'Juventus', 'Sevilla', ' Barcelona', 'Lazio']
Tottenham Liverpool Chelsea Cardiff Everton Arsenal Juventus Sevilla Barcelona Lazio
而不是del england[2]
del england[3]
将按字母顺序排序。sort()
,否则它将使用每个字母扩展。england.extend(spain.split(','))
,以便将england.insert(len(england), 'Lazio')
插入到最后。希望有帮助!
答案 1 :(得分:0)
更新的代码:
def clubs(t):
england = 'Tottenham Burnley Newcastle Leicester Liverpool Chelsea Cardiff Everton Arsenal'
england = england.split()
england.pop(1)
del england[3]
england.remove('Newcastle')
england.sort()
england.append('Juventus')
spain = 'Sevilla, Barcelona'
spain = spain.split(",")
england.extend(spain)
england.insert(0, 'Lazio')
delimiter = ' '
england = delimiter.join(england)
# t = delimiter.join(england)
print(england)
clubs("")
以下是您错过的事情:
insert
需要2个参数,第一个是索引,第二个要插入的对象。答案 2 :(得分:0)
england是一个字符串变量。由于您将字符串插入t的列表中,因此t是您要使用的
def clubs():
england = 'Tottenham Burnley Newcastle Leicester Liverpool Chelsea Cardiff Everton Arsenal'
t = england.split()
t.pop(1)
del t[3]
t.remove('Newcastle')
t.sort()
t.append('Juventus')
spain = 'Sevilla Barcelona'
t.extend(spain.split())
t.insert(len(t),'Lazio')
delimiter = ' '
new_string = delimiter.join(t)
print(new_string)
clubs()
答案 3 :(得分:0)
def clubs(t):
england = 'Tottenham Burnley Newcastle Leicester Liverpool Chelsea Cardiff Everton Arsenal'
england = england.split()#now england is list earlier it was string and t was list
t=england.pop(1)
del england[3]
england.remove('Newcastle')
england.sort()
england.append('Juventus')
spain = ['Sevilla', 'Barcelona']
england.extend(spain)#if extend is used with string then it adds each element of string as an element to list so spain should had been a list
england.insert(2,'Lazio')#insert function takes exactly 2 arguments (pos,element) only element was given earlier
delimiter = ' '
t = delimiter.join(england)
print(england)
输出
['Arsenal', 'Cardiff', 'Lazio', 'Chelsea', 'Everton', 'Leicester', 'Tottenham', 'Juventus', 'Sevilla', 'Barcelona']```