How to remove whitespace in a list - python

I can't remove my whitespace in my list.
invoer = "5-9-7-1-7-8-3-2-4-8-7-9"
cijferlijst = []
for cijfer in invoer:
cijferlijst.append(cijfer.strip('-'))
I tried the following but it doesn't work. I already made a list from my string and seperated everything but the "-" is now a "".
filter(lambda x: x.strip(), cijferlijst)
filter(str.strip, cijferlijst)
filter(None, cijferlijst)
abc = [x.replace(' ', '') for x in cijferlijst]

Try that:
>>> ''.join(invoer.split('-'))
'597178324879'

If you want the numbers in string without -, use .replace() as:
>>> string_list = "5-9-7-1-7-8-3-2-4-8-7-9"
>>> string_list.replace('-', '')
'597178324879'
If you want the numbers as list of numbers, use .split():
>>> string_list.split('-')
['5', '9', '7', '1', '7', '8', '3', '2', '4', '8', '7', '9']

This looks a lot like the following question:
Python: Removing spaces from list objects
The answer being to use strip instead of replace. Have you tried
abc = x.strip(' ') for x in x

Related

Convert a number to an array of digits using a one line for loop in Python

I am trying t convert a number as follows:
input -> 123456
output -> ['1','2','3','4','5','6']
The following loop does work:
number = 123456
defg = []
abc = str(number)
for i in range(len(abc)):
defg.append(abc[i])
However, when i try this in the form of a one line for loop, the output is ['None','None','None','None']
My one line loop is as follows:
number = 123456
defg = []
abc = str(number)
defg= [[].append(abc[i]) for i in range(len(abc))]
Any idea what I am doing wrong?
The answers above are concise, but I personally prefer vanilla list comprehension over map, as powerful as it is. I add this answer simply for the sake of adding diversity to the mix, showing that the same can be achieved without using map.
str_list = [abc[i] for i in range(len(abc))]
We can strive for a more Pythonic solution by avoiding direct indexing and using a simpler loop.
better_str_list = [char for char in abc]
This list comprehension is basically just a translation of the for loop you already have.
Try this:
x = 527876324
print(list(str(x))
Output
['5', '2', '7', '8', '7', '6', '3', '2', '4']
here the solution
list(map(lambda x: str(x), str(number)))
Out[13]: ['1', '2', '3', '4', '5', '6']
or you can do this
list(map(str, str(number)))
Out[13]: ['1', '2', '3', '4', '5', '6']
or this
list(str(number))
['1', '2', '3', '4', '5', '6']
duplicate you can see more here
Splitting integer in Python?
Solution
As your expection the result is
number = 123456
defg = []
abc = str(number)
[defg.append(abc[i]) for i in range(len(abc))]
print(defg)
When you run the loop the values are append to defg but it return none. you assign the None value to defg so it will show the None output
Recomanded way
You can use list metnod to convert the number to list
number = 865393410
result = list(str(number))
print(result)
If you want int in list try this
number = 865393410
result = []
for i in list(str(number)):
result.append(int(i))
print(result)
With single line loop
number = 865393410
result = []
[result.append(int(i)) for i in list(str(number))]
print(result)
The last one is recommended for you

How to properly group items in a string?

I currently have a group of strings that look like this:
[58729 58708]
[58729]
[58708]
[58729]
I need to turn them into a list, but when I use list(), I get:
['[', '5', '8', '7', '2', '9', ']']
['[', '5', '8', '7', '0', '8', ']']
['[', '5', '8', '7', '2', '9', ']']
['[', '5', '8', '7', '2', '9', ' ', '5', '8', '7', '0', '8', ']']
How do I group them so that they don't get separated out into individual characters? So, something like this:
['58729', '58708']
['58729']
['58708']
['58729']
Let's say your input string is assigned to a variable foo.
foo = '[58729 58708]'
First, you want to use list slicing to get rid of the brackets at the start and end of the string:
foo = foo[1:-1]
Now, you can just use the string method split() to turn the string into a list. Here, the input of split() is the character at which the list shall be split. In your case, that would be a single space character:
foo.split(' ')
This returns
['58729', '58708'].
You can use regex to extract the values between the square brackets, then split the values into a list.
The code:
import re
s = '[58729 58708]'
result = re.search('\[(.*)\]', s).group(1).split()
The result:
>>> %Run string2list.py
['58729', '58708']
>>> %Run string2list.py
<class 'list'>
Imo the royal path would be to combine a regex with a small parser:
from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor
import re
data = """
[58729 58708]
[58729]
[58708]
[58729]
"""
# outer expression
rx = re.compile(r'\[[^\[\]]+\]')
# nodevisitor class
class StringVisitor(NodeVisitor):
grammar = Grammar(
r"""
list = lpar content+ rpar
content = item ws?
item = ~"[^\[\]\s]+"
ws = ~"\s+"
lpar = "["
rpar = "]"
"""
)
def generic_visit(self, node, visited_children):
return visited_children or node
def visit_content(self, node, visited_children):
item, _ = visited_children
return item.text
def visit_list(self, node, visited_children):
_, content, _ = visited_children
return [item for item in content]
sv = StringVisitor()
for lst in rx.finditer(data):
real_list = sv.parse(lst.group(0))
print(real_list)
Which would yield
['58729', '58708']
['58729']
['58708']
['58729']
Example with "ast" module usage
import ast
data_str = '[58729 58708]'
data_str = data_str.replace(' ',',') # make it '[58729, 58708]'
x = ast.literal_eval(data_str)
print(x)
Out[1]:
[58729, 58708]
print(x[0])
Out[2]:
58729
print(type(x))
Out[3]:
<class 'list'>
# and after all if you want exactly list of string:
[str(s) for s in x]
Out[4]:
['58729', '58708']

Convert mixed list to string [duplicate]

This question already has an answer here:
How to join mixed list (array) (with integers in it) in Python?
(1 answer)
Closed 3 years ago.
I have the list below, which contains strings and integers. How do I make a string of them? Tried to use
''.join(l)
but it does't work, because there are integers on the list(specifically, L[1] is an integer, and the others are all strings.). Can you help?
L=['1', 9, ':', '0', '5', ':', '4', '5']
#expected "19:05:45"
You can convert each number in the list into a string with a for loop or list comprehension. Like so:
l = ['1', 9, ':', '0', '5', ':', '4', '5']
''.join([str(x) for x in l])
'19:05:45'
Generators are perfect in this case:
>>> l = ['1', 9, ':', '0', '5', ':', '4', '5']
>>> ''.join(str(x) for x in l)
'19:05:45'
This looks the same as a list comprehension but does not require the creation of another list instance.
Just map all the elements to string before joining.
>>> ''.join(map(str,['1', 9, ':', '0', '5', ':', '4', '5']))
'19:05:45'
You can do it in a for loop
list_str = ''
for i in l:
list_str += str(i)
Or a with a list comprehension
list_str = ''.join([str(i) for i in l])

Python regular expression retrieving numbers between two different delimiters

I have the following string
"h=56,7,1,d=88,9,1,h=58,8,1,d=45,h=100,d=,"
I would like to use regular expressions to extract the groups:
group1 56,7,1
group2 88,9,1
group3 58,8,1
group4 45
group5 100
group6 null
My ultimate goal is to have tuples such as (group1, group2), (group3, group4), (group5, group6). I am not sure if this all can be accomplished with regular expressions.
I have the following regular expression with gives me partial results
(?<=h=|d=)(.*?)(?=h=|d=)
The matches have an extra comma at the end like 56,7,1, which I would like to remove and d=, is not returning a null.
You likely do not need to use regex. A list comprehension and .split() can likely do what you need like:
Code:
def split_it(a_string):
if not a_string.endswith(','):
a_string += ','
return [x.split(',')[:-1] for x in a_string.split('=') if len(x)][1:]
Test Code:
tests = (
"h=56,7,1,d=88,9,1,h=58,8,1,d=45,h=100,d=,",
"h=56,7,1,d=88,9,1,d=,h=58,8,1,d=45,h=100",
)
for test in tests:
print(split_it(test))
Results:
[['56', '7', '1'], ['88', '9', '1'], ['58', '8', '1'], ['45'], ['100'], ['']]
[['56', '7', '1'], ['88', '9', '1'], [''], ['58', '8', '1'], ['45'], ['100']]
You could match rather than split using the expression
[dh]=([\d,]*),
and grab the first group, see a demo on regex101.com.
That is
[dh]= # d or h, followed by =
([\d,]*) # capture d and s 0+ times
, # require a comma afterwards
In Python:
import re
rx = re.compile(r'[dh]=([\d,]*),')
string = "h=56,7,1,d=88,9,1,h=58,8,1,d=45,h=100,d=,"
numbers = [m.group(1) for m in rx.finditer(string)]
print(numbers)
Which yields
['56,7,1', '88,9,1', '58,8,1', '45', '100', '']
You can use ([a-z]=)([0-9,]+)(,)?
Online demo
just you need add index to group
You could use $ in positive lookahead to match against the end of the string:
import re
input_str = "h=56,7,1,d=88,9,1,h=58,8,1,d=45,h=100,d=,"
groups = []
for x in re.findall('(?<=h=|d=)(.*?)(?=d=|h=|$)', input_str):
m = x.strip(',')
if m:
groups.append(m.split(','))
else:
groups.append(None)
print(groups)
Output:
[['56', '7', '1'], ['88', '9', '1'], ['58', '8', '1'], ['45'], ['100'], None]
Here, I have assumed that parameters will only have numerical values. If it is so, then you can try this.
(?<=h=|d=)([0-9,]*)
Hope it helps.

How to remove whitespace from the string of list

I would like to remove whitespace of the string of the list as following
original = ['16', '0000D1AE18', '1', '1', '1', 'S O S .jpg', '0']
after remove the whitespace
['16', '0000D1AE18', '1', '1', '1', 'SOS.jpg', '0']
Use str.translate() on each element in a list comprehension:
[v.translate(None, ' ') for v in original]
Here None means don't replace characters with other characters, and ' ' means remove spaces altogether. This produces a new list to replace the original.
The above only removes just the spaces. To remove all whitespace (newlines, tabs, feeds, etc.) simply expand what characters should be removed
[v.translate(None, ' \t\r\n\f\x0a') for v in original]
str.translate() is the fastest option for removing characters from text.
Demo:
>>> original = ['16', '0000D1AE18', '1', '1', '1', 'S O S .jpg', '0']
>>> [v.translate(None, ' \t\r\n\f\x0a') for v in original]
['16', '0000D1AE18', '1', '1', '1', 'SOS.jpg', '0']
If you want to remove any whitespace (i.e.Space, Tab, CR and Newline), use this:
import re
without_spaces = [re.sub(r'\s+', '', item) for item in original]
If you need to replace only regular spaces, use the already suggested solution
without_spaces = [item.replace(' ', '') for item in original]
You can use
k=[]
for i in original :
j = i.replace(' ','')
k.append(j)

Categories