亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python分割,排除字符串的一部分

Python分割,排除字符串的一部分

MMMHUHU 2024-01-16 10:23:07
這是我得到的字符串 Enter 3 random strings, separated by commas: The,Dave,Make我希望該列表僅包含以下內容:["The", "Dave", "Make"]我嘗試過使用 split,但它導致結果錯誤strings = input("Enter 3 random strings, separated by commas:") strings = strings.split()
查看完整描述

4 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

使用string.split()方法strip()

In [2680]: s = "Enter 3 random strings, separated by commas: The,Dave,Make"

In [2686]: s.split(':')[-1].strip().split(',')

Out[2686]: ['The', 'Dave', 'Make']


查看完整回答
反對 回復 2024-01-16
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

在冒號處拆分字符串,刪除多余的空格,然后在逗號處拆分。

string = 'Enter 3 random strings, separated by commas: The,Dave,Make'
result = string.split(':')[1].strip().split(',')


查看完整回答
反對 回復 2024-01-16
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

如果字符串是從控制臺獲取的,那么它的第一部分應該是輸入提示:


strings = input("Enter 3 random strings, separated by commas:")

strings = strings.strip().split(',')

代碼可以縮短為一行:


strings = input("Enter 3 random strings, separated by commas:").strip().split(',')


查看完整回答
反對 回復 2024-01-16
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

嘗試這個:


string = 'Enter 3 random strings, separated by commas: The,Dave,Make'


lst = string.split(':')[-1].strip().split(',')

輸出:


>>> lst

['The', 'Dave', 'Make']


查看完整回答
反對 回復 2024-01-16
  • 4 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號