Delete Extra Space from Values In List - python

I have a list of numbers in Python all with a extra space in the front. How do I remove the extra space (so that only the numbers are left)? A short example is below (note the extra space):
List = [' 5432', ' 23421', ' 43242', .......]

For your case, with a list, you can use str.strip()
l = [x.strip() for x in List]
This will strip both trailing and leading spaces. If you only need to remove leading spaces, go with Alex' solution.

Use str.lstrip here, as the white-space is only at the front:
List = [s.lstrip() for s in List]
# ['5432', '23421', '43242', ...]
Or in this case, seeing as you know how many spaces there are you can just do:
List = [s[1:] for s in List]

map(str.strip, List)
or
map(lambda l: l.strip(), List)

Related

Removing trailing characters

I thought that this code below could remove the trailing character of every element in a. I can't understand the output
a = ['wonderland#', 'alice,', 'in!$', 'book:']
for index, word in enumerate(a):
for ch in '#!$,:':
a[index] = word.strip(ch)
print(a)
>>>['wonderland#', 'alice,', 'in!$', 'book']
Recommended:
Try rstrip this way with a list comprehension:
a = ['wonderland#', 'alice,', 'in!$', 'book:']
print([i.rstrip('#!$,:') for i in a])
Or try regex:
import re
print([re.sub('[#!$,:]+$', '', i) for i in a])
Both codes output:
['wonderland', 'alice', 'in', 'book']
Not recommended:
The reason your code doesn't work is because you may well matched the character(s) at the end but the next iteration you would be still trying to strip from the original string, instead of the string that you just stripped in the previous iterations. Also you use strip so it might be that only one character would be striped, so you also need to use sorted, so to fix your code:
a = ['wonderland#', 'alice,', 'in!$', 'book:']
for index, word in enumerate(a):
for ch in sorted('#!$,:', key=word.find)[::-1]:
a[index] = a[index].strip(ch)
print(a)
print(a)
Output:
['wonderland', 'alice', 'in', 'book']
The problem is that you're iterating over the list and assigning it to word, then every call to strip starts over with the unmodified word. Try this instead:
for index in range(len(a)):
for ch in '#!$,:':
a[index] = a[index].strip(ch)
And it turns out that doesn't work either, because it's dependent on the order of the characters that you're removing. Take advantage of strips ability to remove multiple characters at once.
for index in range(len(a)):
a[index] = a[index].strip('#!$,:')

how remove every thing from a list except words?

I have a list like this:
my_list=["'-\\n'",
"'81\\n'",
"'-\\n'",
"'0913\\n'",
"'Assistant nursing\\n'",
"'0533\\n'",
"'0895 Astronomy\\n'",
"'0533\\n'",
"'Astrophysics\\n'",
"'0532\\n'"]
Is there any way to delete every thing from this list except words?
out put:
my_list=['Assistant nursing',
'Astronomy',
'Astrophysics',]
I know for example if i wanna remove integers in string form i can do this:
no_integers = [x for x in my_list if not (x.isdigit()
or x[0] == '-' and x[1:].isdigit())]
but it dosn't work well enough
The non-regex solution:
You can start by striping off the characters '-\\n, then take only the characters that are alphabets using str.isalpha or a white space, then filter out the sub-strings that are empty ''. You may need to strip off the white space characters in the end, whic
>>> list(filter(lambda x: x!='', (''.join(j for j in i.strip('\'-\\\\n') if j.isalpha() or j==' ').strip() for i in my_list)))
['Assistant nursing', 'Astronomy', 'Astrophysics']
If you want to use regex, you can use the pattern: '([A-Za-z].*?)\\\\n' with re.findall, then filter out the elements that are empty list, finally you can flatten the list
>>> import re
>>> list(filter(lambda x: x, [re.findall('([A-Za-z].*?)\\\\n', i) for i in my_list]))
[['Assistant nursing'], ['Astronomy'], ['Astrophysics']]
with regular expresssions
import re
my_list = # above
# remove \n, -, digits, ' symbols
my_new_list = [re.sub(r"[\d\\n\-']", '', s) for s in my_list]
# remove empty strings
my_new_list = [s for s in my_new_list if s != '']
print(my_new_list)
Output
['Assistat ursig', ' Astroomy', 'Astrophysics']

How do I remove Spaces from nested list created with for loop?

I have created a nested list using for loop. I used the following code:
'''
a=[]
for i in range(1,5+1):
a=a+[[1]*i]
print(a)
'''
And the output is:
How do I remove these spaces before every 1 from the list? I need my list to be modified!
The spaces are part of the default __repr__ (visual representation) of the list object.
To remove the spaces, you'll have to do your own output processing.
Convert the list to a string. Remove the spaces from that. Print:
print( str(a).replace(' ', '') )
Output:
'[[1],[1,1],[1,1,1],[1,1,1,1],[1,1,1,1,1]]'
"But how can I make that change in my list?"
You can't. As I said, those spaces come from __repr__, which is called implicitly when you print the list. There are no spaces in your list, only in the print representation. You cannot remove the spaces from your in-memory list, any more than you could remove the commas or the brackets.
The output that you are getting is the default output of python. In order to change it, you can do the following, as mentioned by Prune:
a = []
for i in range (1, 6):
a += str([1]*i).replace(' ', '')
print(a)
#or
a = []
for i in range(1, 6):
a += [[1]*i]
a = str(a).replace(' ', '')
print(a)
Have you tried:
a=[] for i in range(1,5+1): a=a+[[1]*i] print(a, end='')?

how to remove "\n" from a list of strings

I have a list that is read from a text file that outputs:
['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
I want to remove the \n from each element, but using .split() does not work on lists only strings (which is annoying as this is a list of strings).
How do I remove the \n from each element so I can get the following output:
['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
old_list = [x.strip() for x in old_list]
old_list refers to the list you want to remove the \n from.
Or if you want something more readable:
for x in range(len(old_list)):
old_list[x] = old_list[x].strip()
Does the same thing, without list comprehension.
strip() method takes out all the whitespaces, including \n.
But if you are not ok with the idea of removing whitespaces from start and end, you can do:
old_list = [x.replace("\n", "") for x in old_list]
or
for x in range(len(old_list)):
old_list[x] = old_list[x].replace("\n", "")
do a strip but keep in mind that the result is not modifying the original list, so you will need to reasign it if required:
a = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
a = [path.strip() for path in a]
print a
Give this code a try:
lst = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
for n, element in enumerate(lst):
element = element.replace('\n', '')
lst[n] = element
print(lst)
Use:
[i.strip() for i in lines]
in case you don't mind to lost the spaces and tabs at the beginning and at the end of the lines.
You can read the whole file and split lines using str.splitlines:
temp = file.read().splitlines()
if you still have problems go to this question where I got the answer from
How to read a file without newlines?
answered Sep 8 '12 at 11:57 Bakuriu
There are many ways to achieve your result.
Method 1: using split() method
l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.split('\n')[0] for i in l]
print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
Method 2: using strip() method that removes leading and trailing whitespace
l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.strip() for i in l]
print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
Method 3: using rstrip() method that removes trailing whitespace
l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.rstrip() for i in l]
print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
Method 4: using the method replace
l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.replace('\n', '') for i in l]
print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
Here is another way to do it with lambda:
cleannewline = lambda somelist : map(lambda element: element.strip(), somelist)
Then you can just call it as:
cleannewline(yourlist)

Spliting string into two by comma using python

I have following data in a list and it is a hex number,
['aaaaa955554e']
I would like to split this into ['aaaaa9,55554e'] with a comma.
I know how to split this when there are some delimiters between but how should i do for this case?
Thanks
This will do what I think you are looking for:
yourlist = ['aaaaa955554e']
new_list = [','.join([x[i:i+6] for i in range(0, len(x), 6)]) for x in yourlist]
It will put a comma at every sixth character in each item in your list. (I am assuming you will have more than just one item in the list, and that the items are of unknown length. Not that it matters.)
i assume you wanna split into every 6th character
using regex
import re
lst = ['aaaaa955554e']
newlst = re.findall('\w{6}', lst[0])
# ['aaaaa9', '55554e']
Using list comprehension, this works for multiple items in lst
lst = ['aaaaa955554e']
newlst = [item[i:i+6] for i in range(0,len(a[0]),6) for item in lst]
# ['aaaaa9', '55554e']
This could be done using a regular expression substitution as follows:
import re
print re.sub(r'([a-zA-Z]+\d)(.*?)', r'\1,\2', 'aaaaa955554e', count=1)
Giving you:
aaaaa9,55554e
This splits after seeing the first digit.

Categories

Resources