第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何用python打印()最后兩個(gè)詞用空格形成句子

如何用python打?。ǎ┳詈髢蓚€(gè)詞用空格形成句子

搖曳的薔薇 2022-11-18 16:41:46
如何用python最后兩個(gè)單詞打印()形成帶空格的句子?比如“Hello world, there are 200 pcs”,我需要打?。骸?00 pcs”。太感謝了。sentence = "Hello world, there is 200 pcs"what_I_need = sentence.split()what_I_need[-3]# That prints "is"print(what_I_need)# But I need to print "is 200 pcs"
查看完整描述

3 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

將拆分后的句子切片[-2:]將返回所需的輸出。嘗試:


sentence = "Hello world, there is 200 pcs"

what_I_need = sentence.split()

print(what_I_need[-2:]) # output: ['200', 'pcs']

# or as a string:

print(" ".join(what_I_need[-2:])) # output: 200 pcs


查看完整回答
反對(duì) 回復(fù) 2022-11-18
?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊

def get_last_n_words(n:int, sentence:string):

   last_n_Words = ' '.join(sentence.split()[-n:])

   return last_n_words


sentence = "Hello world, there is 200 pcs"

lastThreeWords = get_last_n_words(3, sentence)

# lasthreeWords: "is 200 pcs"


查看完整回答
反對(duì) 回復(fù) 2022-11-18
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是因?yàn)槟谒饕?-3 中打印了列表,您應(yīng)該從末尾到索引 -3 獲取所有元素,這樣您就可以像之前提到的那樣使用 : 運(yùn)算符



查看完整回答
反對(duì) 回復(fù) 2022-11-18
  • 3 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)