Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

Seperate strings from array that have whitespace from those that dont have whitespaces in python

I have an array that has strings in it and I want to make 2 new arrays, first array is the one that will have items from first list that have whitespaces and second array that has those who don't:

array = ["something", "something with space", "something2", "something with space 2"]

First:

noWhitespaceCharacters = ["something", "something2"]

Second:

hasWhitespaceCharacters = ["something with space", "something with space 2"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just check if there is whitespace or not by in keyword

array = ["something", "something with space", "something2", "something with space 2"]
nows=[i for i in array if " " in i]
ws = [i for i in array if " " not in i]

nows means no white space, ws means white space


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...