how do one remove whitespace from a list? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how do one remove whitespace from a list? (/thread-6587.html) |
how do one remove whitespace from a list? - BoaCoder3 - Nov-29-2017 I'm trying to make a programme that calculates a mathematical ratio problem. in order to do so i convert the 4 numbers the user enters into list elements, then i perform the operations on them. However, i have a problem with whitespace, how do i remove those in python? Here's my programme: # !/usr/bin/python # -*- coding: utf8 -*- import math def data_prosesering(): ratio = list(raw_input('ratio syfers: ')) r1 = float(ratio[0]) * float(ratio[1]) r2 = float(ratio[2]) * float(ratio[3]) a = r1/r2 print math.sqrt(a) data_prosesering()First searched and tried to do something like this: d = [x.strip() for x in ratio] Then however i got stuck so i went searching for solutions again and finally got the idea of trying this: pre_ratio = raw_input('ratio syfers: ') pre_ratio.split() ratio = list(pre_ratio) print ratio #testing if this worksThis however didn't work either. Does anyone know how to solve this problem? Python version: 2.7 operating system: windows 8 RE: how do one remove whitespace from a list? - Larz60+ - Nov-29-2017 You can use a regular expression import re pre_ratio = re.sub(r'\s', '', pre_ratio) RE: how do one remove whitespace from a list? - hshivaraj - Nov-30-2017 list(raw_input().replace(' ', '')) hee ll o ['h', 'e', 'e', 'l', 'l', 'o'] RE: how do one remove whitespace from a list? - BoaCoder3 - Dec-09-2017 Ah yes of course! thanks guys, you solved a lot of struggle for me now :) |