[英]How to perform two functions in a single for loop in python
I wanna split a sentence and also replace quotes from it.我想拆分一个句子并替换其中的引号。 I did:我做了:
sentences = read_data.split('\n')
sentences_no_quotes = [sentence.replace('"', '') for sentence in sentences]
splited_sentences = [sentence.split(',') for sentence in sentences_no_quotes]
How can I do it in a single line?我怎样才能在一行中做到这一点? any suggestions!有什么建议! Thanks for the help谢谢您的帮助
Just do it, it's straightforward.去做吧,这很简单。
splited_sentences = [sentence.split(',') for sentence in
[sentence.replace('"', '') for sentence in
read_data.split('\n')]]
Probably you'd better use generators instead of lists but that's beyond your question.可能你最好使用生成器而不是列表,但这超出了你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.