Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Remove an '\n\t\t\t'-element from list

I got the following list called "phonenumbers". I struggle to remove the elements which contain ' ' and ' '. I tried "try and except"-methode and remove(' ') but couldnt make it work. Any tips?

['(02271) 6 79', ' 70', ' ', '(02271) 6 79', ' ', ' 70', ' ', ' ', '(02181) 27 0', ' ', '3-0', ' ', ' ', '(02181) 27 0', ' ', '3-0', ' ', ' ', '(02161) 24 19', ' ', ' 40', ' ', ' ', '(02161) 24 19', ' ', ' 40', ' ', ' ', '(02131) 66 67', ' ', ' 10', ' ', ' ', '(02131) 66 67', ' ', ' 10', ' ', ' ', '(02103) 39 00', ' ', ' 93', ' ', ' ', '(02103) 39 00', ' ', ' 93', ' ', ' ', '(02173) 2 04 7', ' ', '3-0', ' ', ' ', '(02173) 2 04 7', ' ', '3-0', ' ', ' ', '(02235) 9 23 04', ' ', ' 30', ' ', ' ', '(02235) 9 23 04', ' ', ' 30', ' ', ' ', ' ', '(0221) 3 46 79 40', ' ', ' ', ' ', '(0221) 3 46 79 40', ' ', ' ', '(02232) 4 23', ' ', ' 05', ' ', ' ', '(02232) 4 23', ' ', ' 05', ' ', ' ', '(0157) 86 85 74', ' ', ' 43', ' ', ' ', '(0157) 86 85 74', ' ', ' 43', ' ', ' ', '(02181) 2 78 11', ' ', ' 47', ' ', ' ', '(02181) 2 78 11', ' ', ' 47', ' ', ' ', '(02181) 47 49 0', ' ', '0-0', ' ', ' ', '(02181) 47 49 0', ' ', '0-0', ' ', ' ', '(02202) 1 88', ' ', ' 60', ' ', ' ', '(02202) 1 88', ' ', ' 60', ' ', ' ', '(0211) 23 80', ' ', ' 70', ' ', ' ', '(0211) 23 80', ' ', ' 70', ' ', ' ', '(02235) 9 23 0', ' ', '4-0', ' ', ' ', '(02235) 9 23 0', ' ', '4-0', ' ']

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You could go for a simple expression like

^s+$

In Python:

import re

lst = ['(02271) 6 79', ' 70', '
			', '(02271) 6 79', '
				', ' 70', '
			', '
			', '(02181) 27 0', '
				', '3-0', '
			', '
			', '(02181) 27 0', '
				', '3-0', '
			', '
			', '(02161) 24 19', '
				', ' 40', '
			', '
			', '(02161) 24 19', '
				', ' 40', '
			', '
			', '(02131) 66 67', '
				', ' 10', '
			', '
			', '(02131) 66 67', '
				', ' 10', '
			', '
			', '(02103) 39 00', '
				', ' 93', '
			', '
			', '(02103) 39 00', '
				', ' 93', '
			', '
			', '(02173) 2 04 7', '
				', '3-0', '
			', '
			', '(02173) 2 04 7', '
				', '3-0', '
			', '
			', '(02235) 9 23 04', '
				', ' 30', '
			', '
			', '(02235) 9 23 04', '
				', ' 30', '
			', '
			', '
				', '(0221) 3 46 79 40', '
			', '
			', '
				', '(0221) 3 46 79 40', '
			', '
			', '(02232) 4 23', '
				', ' 05', '
			', '
			', '(02232) 4 23', '
				', ' 05', '
			', '
			', '(0157) 86 85 74', '
				', ' 43', '
			', '
			', '(0157) 86 85 74', '
				', ' 43', '
			', '
			', '(02181) 2 78 11', '
				', ' 47', '
			', '
			', '(02181) 2 78 11', '
				', ' 47', '
			', '
			', '(02181) 47 49 0', '
				', '0-0', '
			', '
			', '(02181) 47 49 0', '
				', '0-0', '
			', '
			', '(02202) 1 88', '
				', ' 60', '
			', '
			', '(02202) 1 88', '
				', ' 60', '
			', '
			', '(0211) 23 80', '
				', ' 70', '
			', '
			', '(0211) 23 80', '
				', ' 70', '
			', '
			', '(02235) 9 23 0', '
				', '4-0', '
			', '
			', '(02235) 9 23 0', '
				', '4-0', '
			']

rx = re.compile(r'^s+$')

lst = [item.strip() for item in lst if not rx.match(item)]
print(lst)

This yields and strips all numbers that are not only whitespaces from start to end:

['(02271) 6 79', '70', '(02271) 6 79', '70', '(02181) 27 0', '3-0', '(02181) 27 0', '3-0', '(02161) 24 19', '40', '(02161) 24 19', '40', '(02131) 66 67', '10', '(02131) 66 67', '10', '(02103) 39 00', '93', '(02103) 39 00', '93', '(02173) 2 04 7', '3-0', '(02173) 2 04 7', '3-0', '(02235) 9 23 04', '30', '(02235) 9 23 04', '30', '(0221) 3 46 79 40', '(0221) 3 46 79 40', '(02232) 4 23', '05', '(02232) 4 23', '05', '(0157) 86 85 74', '43', '(0157) 86 85 74', '43', '(02181) 2 78 11', '47', '(02181) 2 78 11', '47', '(02181) 47 49 0', '0-0', '(02181) 47 49 0', '0-0', '(02202) 1 88', '60', '(02202) 1 88', '60', '(0211) 23 80', '70', '(0211) 23 80', '70', '(02235) 9 23 0', '4-0', '(02235) 9 23 0', '4-0']


As @dawg points out, a regular expression is not really needed, actually:
lst = [number for item in lst for number in [item.strip()] if number]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...