我正在尝试解决一个问我以下问题的练习:
创建一个由空格分隔的一系列单词的字符串
使用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)
我期望的输出是:
托特纳姆热刺利物浦切尔西加的夫埃弗顿阿森纳尤文图斯塞维利亚巴塞罗那拉齐奥
有人能帮我弄清楚它有什么问题吗?谢谢!
您正在混淆英格兰字符串以及您使用拆分从此字符串生成的列表。你的输出应该排序所以它将是阿森纳卡迪夫切尔西埃弗顿莱斯特托特纳姆热刺尤文图斯塞维利亚巴塞罗那拉齐奥
所以我会像下面这样做
def clubs(t):
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)
你能尝试以下方法吗?
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')
,所以Lazio
将插入到最后。希望能帮助到你!!!
更新的代码:
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个args是索引和第二个要插入的对象。英格兰是一个字符串变量。由于您在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()
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']```