add a single spaces in list containing characters - python

I am new to the programming. I have a list. List contains multiple spaces. All the multiple spaces should be replaced with single space.
lis = ['h','e','l','l','','','','','o','','','','w']
output = ['h','e','l','l','','o','','w']
Could anyone tell how to do?

Just simple list comprehension will suffice:
lis = ['h','e','l','l','','','','','o','','','','w']
output = [v for i, v in enumerate(lis) if v != '' or i == 0 or (v == '' and lis[i-1] != '')]
print(output)
This will print:
['h', 'e', 'l', 'l', '', 'o', '', 'w']

You can use a list comprehension with enumerate
and select only those '' which
follow non-empty characters themselves
[c for i, c in enumerate(lis) if c or (not c and lis[i - 1])]

You can use itertools. The idea here is to group according whether strings are empty, noting bool('') == False.
from itertools import chain, groupby
L = (list(j) if i else [''] for i, j in groupby(lis, key=bool))
res = list(chain.from_iterable(L))
print(res)
['h', 'e', 'l', 'l', '', 'o', '', 'w']

You can simply use zip() within a list comprehension as following:
In [21]: lis = ['', '', 'h','e','l','l','','','','','o','','','','w', '', '']
In [22]: lis[:1] + [j for i, j in zip(lis, lis[1:]) if i or j]
Out[22]: ['', 'h', 'e', 'l', 'l', '', 'o', '', 'w', '']
In this case we're looping over each pair an keeping the second item if one of the items in our pair is valid (not empty). You just then need to add the first item to the result because it's omitted.

Why not just a for loop?
new_list = []
for char in lis:
if char == '' and new_list[-1] == '': continue
new_list.append(char)
Outputs
['h', 'e', 'l', 'l', '', 'o', '', 'w']

Related

Python: merge adjacent number in list

is it possible to merge the numbers in a list of chars?
I have a list with some characters:
my_list = ['a', 'f', '£', '3', '2', 'L', 'k', '3']
I'm want to concatenate the adjacent numbers as follow:
my_list = ['a', 'f', '£', '32', 'L', 'k', '3']
I have this, and it works fine, but i don't really like how it came out.
def number_concat(my_list):
new_list = []
number = ""
for ch in my_list:
if not ch.isnumeric():
if number != "":
new_list.append(number)
number =""
new_list.append(ch)
else:
number = ''.join([number,ch])
if number != "":
new_list.append(number)
return new_list
What's the best way to do this?
You can use itertools.groupby:
from itertools import groupby
my_list = ['a', 'f', '£', '3', '2', 'L', 'k', '3']
out = []
for _, g in groupby(enumerate(my_list, 2), lambda k: True if k[1].isdigit() else k[0]):
out.append(''.join(val for _, val in g))
print(out)
Prints:
['a', 'f', '£', '32', 'L', 'k', '3']
you can use a variable to track the index position in the list and then just compare two elements and if they are both digits concat them by popping the index and adding it to the previous one. we leave index pointing to the same value since we popd all other elements iwll have shifted so we need to check this index again and check the next char which will now be in that index. If the char is not a digit then move the index to the next char.
# coding: latin-1
my_list = ['a', 'f', '£', '3', '2', 'L', 'k', '3']
index = 1
while index < len(my_list):
if my_list[index].isdigit() and my_list[index - 1].isdigit():
my_list[index - 1] += my_list.pop(index)
else:
index += 1
print(my_list)
OUTPUT
['a', 'f', '£', '32', 'L', 'k', '3']
Regex:
>>> re.findall('\d+|.', ''.join(my_list))
['a', 'f', '£', '32', 'L', 'k', '3']
itertools:
>>> [x for d, g in groupby(my_list, str.isdigit) for x in ([''.join(g)] if d else g)]
['a', 'f', '£', '32', 'L', 'k', '3']
Another:
>>> [''.join(g) for _, g in groupby(my_list, lambda c: c.isdigit() or float('nan'))]
['a', 'f', '£', '32', 'L', 'k', '3']
You are just trying to reduce your numbers together.
One way to accomplish this is to loop through the list, and check if it's a number using str.isnumeric().
my_list = ['a', 'f', '£', '3', '2', 'L', 'k', '3']
new_list = ['']
for c in my_list:
if c.isnumeric() and new_list[-1].isnumeric(): # Check if current and previous character is a number
new_list[-1] += c # Mash characters together.
else:
new_list.append(c)
else:
new_list[:] = new_list[1:] # Remove '' placeholder to avoid new_list[-1] IndexError
print(new_list) # ['a', 'f', '£', '32', 'L', 'k', '3']
This has also been tested with first character is numeric.
sure! this will combine all consecutive digits:
i = 0
while i < len(my_list):
if my_list[i].isdigit():
j = 1
while i+j < len(my_list) and my_list[i+j].isdigit():
my_list[i] += my_list.pop(i+j)
j += 1
i += 1
you can also do this recursively, which is maybe more elegant (in that it will be easier to build up correctly as the task becomes more complicated) but also possibly more confusing:
def group_digits(list, accumulator=None):
if list == []:
return accumulator or []
if not accumulator:
return group_digits(list[1:], list[:1])
x = list.pop(0)
if accumulator[-1].isdigit() and x.isdigit():
accumulator[-1] += x
else:
accumulator.append(x)
return group_digits(list, accumulator)
A quick and dirty way under the assumption that the non-numeric characters are not white-space:
''.join(c if c.isdigit() else ' '+ c + ' ' for c in my_list).split()
The idea is to pad with spaces the characters that you don't want merged, smush the resulting characters together so that the non-padded ones become adjacent, and then split the result on white-space, the net result leaving the padded characters unchanged and the non-padded characters joined.
I have written a beginner-friendly solution using an index and two lists:
my_list = ['a', 'f', '£', '3', '2', 'L', 'k', '3']
result = []
index = 0
for item in my_list:
if item.isdigit():
# If current item is a number
if my_list[index-1].isdigit() and len(result) > 1:
# If previous item is a number too and it is not the 1st item
# of the list, sum the two and put them in the previous slot in result
result[index-1] = my_list[index-1] + my_list[index]
else:
result.append(item)
else:
result.append(item)
index += 1
print(my_list)
print(result)
Output
['a', 'f', '£', '3', '2', 'L', 'k', '3']
['a', 'f', '£', '32', 'L', 'k', '3']

Python string split by multiple delimiters

Given a string: s = FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE
The delimiting characters are P, Q, Dand E
I want to be able to split the string on these characters.
Based on: Is it possible to split a string on multiple delimiters in order?
I have the following
def splits(s,seps):
l,_,r = s.partition(seps[0])
if len(seps) == 1:
return [l,r]
return [l] + splits(r,seps[1:])
seps = ['P', 'D', 'Q', 'E']
sequences = splits(s, seps)
This gives me:
['FFFFRRFFFFFFF',
'PRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLF',
'RRFRRFFFFFFFFR',
'',
'E']
As we can see the second entry has many P.
I want is the occurrence of characters between the last set of P, not the first occurrence (i.e., RFFFFFFFLF).
Also, the order of occurrence of the delimiting characters is not fixed.
Looking for solutions/hints on how to achieve this?
Update: Desired output, all set of strings between these delimiters (similar to the one shown) but adhering to the condition of the last occurrence as above
Update2: Expected output
['FFFFRRFFFFFFF',
'RFFFFFFFLF', # << this is where the output differs
'RRFRRFFFFFFFFR',
'',
''] # << the last E is 2 consecutive E with no other letters, hence should be empty
Sounds like you want to split at sequence from first character appearance until the last.
([PDQE])(?:.*\1)?
([PDQE]) captures one of the characters in class
(?:.*\1)? optionally match any amount of characters until last occurence of captured.
Have a try with split pattern at regex101 and a PHP Demo at 3v4l.org (should be similar in Python).
import re
s = "FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE"
def get_sequences(s):
seen_delimiters = {c: ('', None) for c in 'PDQE'}
order = 0
for g in re.finditer(r'(.*?)([PDQE]|\Z)', s):
if g[2]:
if seen_delimiters[g[2][0]][1] == None:
seen_delimiters[g[2][0]] = (g[1], order)
order += 1
return seen_delimiters
for k, (seq, order) in get_sequences(s).items():
print('{}: order: {} seq: {}'.format(k, order, seq))
Prints:
P: order: 0 seq: FFFFRRFFFFFFF
D: order: 1 seq: RFFFFFFFLF
Q: order: 2 seq: RRFRRFFFFFFFFR
E: order: 3 seq:
Update (for print sequences and delimiters enclosing):
import re
s = "FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE"
for g in re.finditer(r'(.*?)([PDQE]+|\Z)', s):
print(g[1], g[2])
Prints:
FFFFRRFFFFFFF PP
RRRRRRLLRLLRLLL PP
F PP
L PP
L PP
LF PP
FF P
FLR P
FFRRLLR P
F P
RFFFFFFFLF D
RRFRRFFFFFFFFR QEE
Use re.split with a character class [PQDE]:
import re
s = 'FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE'
sequences = re.split(r'[PQDE]', s)
print(sequences)
Output:
['FFFFRRFFFFFFF', '', 'RRRRRRLLRLLRLLL', '', 'F', '', 'L', '', 'L', '', 'LF', '', 'FF', 'FLR', 'FFRRLLR', 'F', 'RFFFFFFFLF', 'RRFRRFFFFFFFFR', '', '', '']
If you want to split on 1 or more delimiter:
import re
s = 'FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE'
sequences = re.split(r'[PQDE]+', s)
print(sequences)
Output:
['FFFFRRFFFFFFF', 'RRRRRRLLRLLRLLL', 'F', 'L', 'L', 'LF', 'FF', 'FLR', 'FFRRLLR', 'F', 'RFFFFFFFLF', 'RRFRRFFFFFFFFR', '']
If you want to capture the delimiters:
import re
s = 'FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE'
sequences = re.split(r'([PQDE])', s)
print(sequences)
Output:
['FFFFRRFFFFFFF', 'P', '', 'P', 'RRRRRRLLRLLRLLL', 'P', '', 'P', 'F', 'P', '', 'P', 'L', 'P', '', 'P', 'L', 'P', '', 'P', 'LF', 'P', '', 'P', 'FF', 'P', 'FLR', 'P', 'FFRRLLR', 'P', 'F', 'P', 'RFFFFFFFLF', 'D', 'RRFRRFFFFFFFFR', 'Q', '', 'E', '', 'E', '']
This solution is iterating the delimiters one by one, so you can control the order you want to apply each one of them:
s = 'FFFFRRFFFFFFFPPRRRRRRLLRLLRLLLPPFPPLPPLPPLFPPFFPFLRPFFRRLLRPFPRFFFFFFFLFDRRFRRFFFFFFFFRQEE'
spliters='PDQE'
for sp in spliters:
if type(s) is str:
s = s.split(sp)
else: #type is list
s=[x.split(sp) for x in s]
s = [item for sublist in s for item in sublist if item != ''] #flatten the list
output:
['FFFFRRFFFFFFF',
'RRRRRRLLRLLRLLL',
'F',
'L',
'L',
'LF',
'FF',
'FLR',
'FFRRLLR',
'F',
'RFFFFFFFLF',
'RRFRRFFFFFFFFR']

Why does this line remover function behave inconsistently? [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 7 years ago.
in this code I am trying to create a function anti_vowel that will remove all vowels (aeiouAEIOU) from a string. I think it should work ok, but when I run it, the sample text "Hey look Words!" is returned as "Hy lk Words!". It "forgets" to remove the last 'o'. How can this be?
text = "Hey look Words!"
def anti_vowel(text):
textlist = list(text)
for char in textlist:
if char.lower() in 'aeiou':
textlist.remove(char)
return "".join(textlist)
print anti_vowel(text)
You're modifying the list you're iterating over, which is bound to result in some unintuitive behavior. Instead, make a copy of the list so you don't remove elements from what you're iterating through.
for char in textlist[:]: #shallow copy of the list
# etc
To clarify the behavior you're seeing, check this out. Put print char, textlist at the beginning of your (original) loop. You'd expect, perhaps, that this would print out your string vertically, alongside the list, but what you'll actually get is this:
H ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
e ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # !
l ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
k ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # Problem!!
['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
W ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
d ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
s ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
! ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
Hy lk Words!
So what's going on? The nice for x in y loop in Python is really just syntactic sugar: it still accesses list elements by index. So when you remove elements from the list while iterating over it, you start skipping values (as you can see above). As a result, you never see the second o in "look"; you skip over it because the index has advanced "past" it when you deleted the previous element. Then, when you get to the o in "Words", you go to remove the first occurrence of 'o', which is the one you skipped before.
As others have mentioned, list comprehensions are probably an even better (cleaner, clearer) way to do this. Make use of the fact that Python strings are iterable:
def remove_vowels(text): # function names should start with verbs! :)
return ''.join(ch for ch in text if ch.lower() not in 'aeiou')
Other answers tell you why for skips items as you alter the list. This answer tells you how you should remove characters in a string without an explicit loop, instead.
Use str.translate():
vowels = 'aeiou'
vowels += vowels.upper()
text.translate(None, vowels)
This deletes all characters listed in the second argument.
Demo:
>>> text = "Hey look Words!"
>>> vowels = 'aeiou'
>>> vowels += vowels.upper()
>>> text.translate(None, vowels)
'Hy lk Wrds!'
>>> text = 'The Quick Brown Fox Jumps Over The Lazy Fox'
>>> text.translate(None, vowels)
'Th Qck Brwn Fx Jmps vr Th Lzy Fx'
In Python 3, the str.translate() method (Python 2: unicode.translate()) differs in that it doesn't take a deletechars parameter; the first argument is a dictionary mapping Unicode ordinals (integer values) to new values instead. Use None for any character that needs to be deleted:
# Python 3 code
vowels = 'aeiou'
vowels += vowels.upper()
vowels_table = dict.fromkeys(map(ord, vowels))
text.translate(vowels_table)
You can also use the str.maketrans() static method to produce that mapping:
vowels = 'aeiou'
vowels += vowels.upper()
text.translate(text.maketrans('', '', vowels))
Quoting from the docs:
Note: There is a subtlety when the sequence is being modified by the
loop (this can only occur for mutable sequences, i.e. lists). An
internal counter is used to keep track of which item is used next, and
this is incremented on each iteration. When this counter has reached
the length of the sequence the loop terminates. This means that if the
suite deletes the current (or a previous) item from the sequence, the
next item will be skipped (since it gets the index of the current item
which has already been treated). Likewise, if the suite inserts an
item in the sequence before the current item, the current item will be
treated again the next time through the loop. This can lead to nasty
bugs that can be avoided by making a temporary copy using a slice of
the whole sequence, e.g.,
for x in a[:]:
if x < 0: a.remove(x)
Iterate over a shallow copy of the list using [:]. You're modifying a list while iterating over it, this will result in some letters being missed.
The for loop keeps track of index, so when you remove an item at index i, the next item at i+1th position shifts to the current index(i) and hence in the next iteration you'll actually pick the i+2th item.
Lets take an easy example:
>>> text = "whoops"
>>> textlist = list(text)
>>> textlist
['w', 'h', 'o', 'o', 'p', 's']
for char in textlist:
if char.lower() in 'aeiou':
textlist.remove(char)
Iteration 1 : Index = 0.
char = 'W' as it is at index 0. As it doesn't satisfies that condition you'll do noting.
Iteration 2 : Index = 1.
char = 'h' as it is at index 1. Nothing more to do here.
Iteration 3 : Index = 2.
char = 'o' as it is at index 2. As this item satisfies the condition so it'll be removed from the list and all the items to it's right will shift one place to the left to fill the gap.
now textlist becomes :
0 1 2 3 4
`['w', 'h', 'o', 'p', 's']`
As you can see the other 'o' moved to index 2, i.e the current index so it'll be skipped in the next iteration. So, this is the reason some items are bring skipped in your iteration. Whenever you remove an item the next item is skipped from the iteration.
Iteration 4 : Index = 3.
char = 'p' as it is at index 3.
....
Fix:
Iterate over a shallow copy of the list to fix this issue:
for char in textlist[:]: #note the [:]
if char.lower() in 'aeiou':
textlist.remove(char)
Other alternatives:
List comprehension:
A one-liner using str.join and a list comprehension:
vowels = 'aeiou'
text = "Hey look Words!"
return "".join([char for char in text if char.lower() not in vowels])
regex:
>>> import re
>>> text = "Hey look Words!"
>>> re.sub('[aeiou]', '', text, flags=re.I)
'Hy lk Wrds!'
You're modifying the data you're iterating over. Don't do that.
''.join(x for x in textlist in x not in VOWELS)
text = "Hey look Words!"
print filter(lambda x: x not in "AaEeIiOoUu", text)
Output
Hy lk Wrds!
You're iterating over a list and deleting elements from it at the same time.
First, I need to make sure you clearly understand the role of char in for char in textlist: .... Take the situation where we have reached the letter 'l'. The situation is not like this:
['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
char
There is no link between char and the position of the letter 'l' in the list. If you modify char, the list will not be modified. The situation is more like this:
['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
char = 'l'
Notice that I've kept the ^ symbol. This is the hidden pointer that the code managing the for char in textlist: ... loop uses to keep track of its position in the loop. Every time you enter the body of the loop, the pointer is advanced, and the letter referenced by the pointer is copied into char.
Your problem occurs when you have two vowels in succession. I'll show you what happens from the point where you reach 'l'. Notice that I've also changed the word "look" to "leap", to make it clearer what's going on:
advance pointer to next character ('l') and copy to char
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'l'
char ('l') is not a vowel, so do nothing
advance pointer to next character ('e') and copy to char
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'e'
char ('e') is a vowel, so delete the first occurrence of char ('e')
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', <- 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
advance pointer to next character ('p') and copy to char
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'p'
When you removed the 'e' all the characters after the 'e' moved one place to the left, so it was as if remove had advanced the pointer. The result is that you skipped past the 'a'.
In general, you should avoid modifying lists while iterating over them. It's better to construct a new list from scratch, and Python's list comprehensions are the perfect tool for doing this. E.g.
print ''.join([char for char in "Hey look Words" if char.lower() not in "aeiou"])
But if you haven't learnt about comprehensions yet, the best way is probably:
text = "Hey look Words!"
def anti_vowel(text):
textlist = list(text)
new_textlist = []
for char in textlist:
if char.lower() not in 'aeiou':
new_textlist.append(char)
return "".join(new_textlist)
print anti_vowel(text)
List Comprehensions:
vowels = 'aeiou'
text = 'Hey look Words!'
result = [char for char in text if char not in vowels]
print ''.join(result)
Others have already explained the issue with your code. For your task, a generator expression is easier and less error prone.
>>> text = "Hey look Words!"
>>> ''.join(c for c in text if c.lower() not in 'aeiou')
'Hy lk Wrds!'
or
>>> ''.join(c for c in text if c not in 'AaEeIiOoUu')
'Hy lk Wrds!'
however, str.translate is the best way to go.
You shouldn't delete items from list you iterating through:
But you can make new list from the old one with list comprehension syntax. List comprehension is very useful in this situation. You can read about list comprehension here
So you solution will look like this:
text = "Hey look Words!"
def anti_vowel(text):
return "".join([char for char in list(text) if char.lower() not in 'aeiou'])
print anti_vowel(text)
It's pretty, isn't it :P
Try to not use the list() function on a string. It will make things a lot more complicated.
Unlike Java, in Python, strings are considered as arrays. Then, try to use an index for loop and del keyword.
for x in range(len(string)):
if string[x].lower() in "aeiou":
del string[x]

Unexpected behavior of python loop when remove function is used [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 7 years ago.
in this code I am trying to create a function anti_vowel that will remove all vowels (aeiouAEIOU) from a string. I think it should work ok, but when I run it, the sample text "Hey look Words!" is returned as "Hy lk Words!". It "forgets" to remove the last 'o'. How can this be?
text = "Hey look Words!"
def anti_vowel(text):
textlist = list(text)
for char in textlist:
if char.lower() in 'aeiou':
textlist.remove(char)
return "".join(textlist)
print anti_vowel(text)
You're modifying the list you're iterating over, which is bound to result in some unintuitive behavior. Instead, make a copy of the list so you don't remove elements from what you're iterating through.
for char in textlist[:]: #shallow copy of the list
# etc
To clarify the behavior you're seeing, check this out. Put print char, textlist at the beginning of your (original) loop. You'd expect, perhaps, that this would print out your string vertically, alongside the list, but what you'll actually get is this:
H ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
e ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # !
l ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
k ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # Problem!!
['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
W ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
d ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
s ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
! ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
Hy lk Words!
So what's going on? The nice for x in y loop in Python is really just syntactic sugar: it still accesses list elements by index. So when you remove elements from the list while iterating over it, you start skipping values (as you can see above). As a result, you never see the second o in "look"; you skip over it because the index has advanced "past" it when you deleted the previous element. Then, when you get to the o in "Words", you go to remove the first occurrence of 'o', which is the one you skipped before.
As others have mentioned, list comprehensions are probably an even better (cleaner, clearer) way to do this. Make use of the fact that Python strings are iterable:
def remove_vowels(text): # function names should start with verbs! :)
return ''.join(ch for ch in text if ch.lower() not in 'aeiou')
Other answers tell you why for skips items as you alter the list. This answer tells you how you should remove characters in a string without an explicit loop, instead.
Use str.translate():
vowels = 'aeiou'
vowels += vowels.upper()
text.translate(None, vowels)
This deletes all characters listed in the second argument.
Demo:
>>> text = "Hey look Words!"
>>> vowels = 'aeiou'
>>> vowels += vowels.upper()
>>> text.translate(None, vowels)
'Hy lk Wrds!'
>>> text = 'The Quick Brown Fox Jumps Over The Lazy Fox'
>>> text.translate(None, vowels)
'Th Qck Brwn Fx Jmps vr Th Lzy Fx'
In Python 3, the str.translate() method (Python 2: unicode.translate()) differs in that it doesn't take a deletechars parameter; the first argument is a dictionary mapping Unicode ordinals (integer values) to new values instead. Use None for any character that needs to be deleted:
# Python 3 code
vowels = 'aeiou'
vowels += vowels.upper()
vowels_table = dict.fromkeys(map(ord, vowels))
text.translate(vowels_table)
You can also use the str.maketrans() static method to produce that mapping:
vowels = 'aeiou'
vowels += vowels.upper()
text.translate(text.maketrans('', '', vowels))
Quoting from the docs:
Note: There is a subtlety when the sequence is being modified by the
loop (this can only occur for mutable sequences, i.e. lists). An
internal counter is used to keep track of which item is used next, and
this is incremented on each iteration. When this counter has reached
the length of the sequence the loop terminates. This means that if the
suite deletes the current (or a previous) item from the sequence, the
next item will be skipped (since it gets the index of the current item
which has already been treated). Likewise, if the suite inserts an
item in the sequence before the current item, the current item will be
treated again the next time through the loop. This can lead to nasty
bugs that can be avoided by making a temporary copy using a slice of
the whole sequence, e.g.,
for x in a[:]:
if x < 0: a.remove(x)
Iterate over a shallow copy of the list using [:]. You're modifying a list while iterating over it, this will result in some letters being missed.
The for loop keeps track of index, so when you remove an item at index i, the next item at i+1th position shifts to the current index(i) and hence in the next iteration you'll actually pick the i+2th item.
Lets take an easy example:
>>> text = "whoops"
>>> textlist = list(text)
>>> textlist
['w', 'h', 'o', 'o', 'p', 's']
for char in textlist:
if char.lower() in 'aeiou':
textlist.remove(char)
Iteration 1 : Index = 0.
char = 'W' as it is at index 0. As it doesn't satisfies that condition you'll do noting.
Iteration 2 : Index = 1.
char = 'h' as it is at index 1. Nothing more to do here.
Iteration 3 : Index = 2.
char = 'o' as it is at index 2. As this item satisfies the condition so it'll be removed from the list and all the items to it's right will shift one place to the left to fill the gap.
now textlist becomes :
0 1 2 3 4
`['w', 'h', 'o', 'p', 's']`
As you can see the other 'o' moved to index 2, i.e the current index so it'll be skipped in the next iteration. So, this is the reason some items are bring skipped in your iteration. Whenever you remove an item the next item is skipped from the iteration.
Iteration 4 : Index = 3.
char = 'p' as it is at index 3.
....
Fix:
Iterate over a shallow copy of the list to fix this issue:
for char in textlist[:]: #note the [:]
if char.lower() in 'aeiou':
textlist.remove(char)
Other alternatives:
List comprehension:
A one-liner using str.join and a list comprehension:
vowels = 'aeiou'
text = "Hey look Words!"
return "".join([char for char in text if char.lower() not in vowels])
regex:
>>> import re
>>> text = "Hey look Words!"
>>> re.sub('[aeiou]', '', text, flags=re.I)
'Hy lk Wrds!'
You're modifying the data you're iterating over. Don't do that.
''.join(x for x in textlist in x not in VOWELS)
text = "Hey look Words!"
print filter(lambda x: x not in "AaEeIiOoUu", text)
Output
Hy lk Wrds!
You're iterating over a list and deleting elements from it at the same time.
First, I need to make sure you clearly understand the role of char in for char in textlist: .... Take the situation where we have reached the letter 'l'. The situation is not like this:
['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
char
There is no link between char and the position of the letter 'l' in the list. If you modify char, the list will not be modified. The situation is more like this:
['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
char = 'l'
Notice that I've kept the ^ symbol. This is the hidden pointer that the code managing the for char in textlist: ... loop uses to keep track of its position in the loop. Every time you enter the body of the loop, the pointer is advanced, and the letter referenced by the pointer is copied into char.
Your problem occurs when you have two vowels in succession. I'll show you what happens from the point where you reach 'l'. Notice that I've also changed the word "look" to "leap", to make it clearer what's going on:
advance pointer to next character ('l') and copy to char
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'l'
char ('l') is not a vowel, so do nothing
advance pointer to next character ('e') and copy to char
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'e'
char ('e') is a vowel, so delete the first occurrence of char ('e')
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', <- 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
advance pointer to next character ('p') and copy to char
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'p'
When you removed the 'e' all the characters after the 'e' moved one place to the left, so it was as if remove had advanced the pointer. The result is that you skipped past the 'a'.
In general, you should avoid modifying lists while iterating over them. It's better to construct a new list from scratch, and Python's list comprehensions are the perfect tool for doing this. E.g.
print ''.join([char for char in "Hey look Words" if char.lower() not in "aeiou"])
But if you haven't learnt about comprehensions yet, the best way is probably:
text = "Hey look Words!"
def anti_vowel(text):
textlist = list(text)
new_textlist = []
for char in textlist:
if char.lower() not in 'aeiou':
new_textlist.append(char)
return "".join(new_textlist)
print anti_vowel(text)
List Comprehensions:
vowels = 'aeiou'
text = 'Hey look Words!'
result = [char for char in text if char not in vowels]
print ''.join(result)
Others have already explained the issue with your code. For your task, a generator expression is easier and less error prone.
>>> text = "Hey look Words!"
>>> ''.join(c for c in text if c.lower() not in 'aeiou')
'Hy lk Wrds!'
or
>>> ''.join(c for c in text if c not in 'AaEeIiOoUu')
'Hy lk Wrds!'
however, str.translate is the best way to go.
You shouldn't delete items from list you iterating through:
But you can make new list from the old one with list comprehension syntax. List comprehension is very useful in this situation. You can read about list comprehension here
So you solution will look like this:
text = "Hey look Words!"
def anti_vowel(text):
return "".join([char for char in list(text) if char.lower() not in 'aeiou'])
print anti_vowel(text)
It's pretty, isn't it :P
Try to not use the list() function on a string. It will make things a lot more complicated.
Unlike Java, in Python, strings are considered as arrays. Then, try to use an index for loop and del keyword.
for x in range(len(string)):
if string[x].lower() in "aeiou":
del string[x]

List of strings in python issue [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 7 years ago.
in this code I am trying to create a function anti_vowel that will remove all vowels (aeiouAEIOU) from a string. I think it should work ok, but when I run it, the sample text "Hey look Words!" is returned as "Hy lk Words!". It "forgets" to remove the last 'o'. How can this be?
text = "Hey look Words!"
def anti_vowel(text):
textlist = list(text)
for char in textlist:
if char.lower() in 'aeiou':
textlist.remove(char)
return "".join(textlist)
print anti_vowel(text)
You're modifying the list you're iterating over, which is bound to result in some unintuitive behavior. Instead, make a copy of the list so you don't remove elements from what you're iterating through.
for char in textlist[:]: #shallow copy of the list
# etc
To clarify the behavior you're seeing, check this out. Put print char, textlist at the beginning of your (original) loop. You'd expect, perhaps, that this would print out your string vertically, alongside the list, but what you'll actually get is this:
H ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
e ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # !
l ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
k ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # Problem!!
['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
W ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
d ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
s ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
! ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
Hy lk Words!
So what's going on? The nice for x in y loop in Python is really just syntactic sugar: it still accesses list elements by index. So when you remove elements from the list while iterating over it, you start skipping values (as you can see above). As a result, you never see the second o in "look"; you skip over it because the index has advanced "past" it when you deleted the previous element. Then, when you get to the o in "Words", you go to remove the first occurrence of 'o', which is the one you skipped before.
As others have mentioned, list comprehensions are probably an even better (cleaner, clearer) way to do this. Make use of the fact that Python strings are iterable:
def remove_vowels(text): # function names should start with verbs! :)
return ''.join(ch for ch in text if ch.lower() not in 'aeiou')
Other answers tell you why for skips items as you alter the list. This answer tells you how you should remove characters in a string without an explicit loop, instead.
Use str.translate():
vowels = 'aeiou'
vowels += vowels.upper()
text.translate(None, vowels)
This deletes all characters listed in the second argument.
Demo:
>>> text = "Hey look Words!"
>>> vowels = 'aeiou'
>>> vowels += vowels.upper()
>>> text.translate(None, vowels)
'Hy lk Wrds!'
>>> text = 'The Quick Brown Fox Jumps Over The Lazy Fox'
>>> text.translate(None, vowels)
'Th Qck Brwn Fx Jmps vr Th Lzy Fx'
In Python 3, the str.translate() method (Python 2: unicode.translate()) differs in that it doesn't take a deletechars parameter; the first argument is a dictionary mapping Unicode ordinals (integer values) to new values instead. Use None for any character that needs to be deleted:
# Python 3 code
vowels = 'aeiou'
vowels += vowels.upper()
vowels_table = dict.fromkeys(map(ord, vowels))
text.translate(vowels_table)
You can also use the str.maketrans() static method to produce that mapping:
vowels = 'aeiou'
vowels += vowels.upper()
text.translate(text.maketrans('', '', vowels))
Quoting from the docs:
Note: There is a subtlety when the sequence is being modified by the
loop (this can only occur for mutable sequences, i.e. lists). An
internal counter is used to keep track of which item is used next, and
this is incremented on each iteration. When this counter has reached
the length of the sequence the loop terminates. This means that if the
suite deletes the current (or a previous) item from the sequence, the
next item will be skipped (since it gets the index of the current item
which has already been treated). Likewise, if the suite inserts an
item in the sequence before the current item, the current item will be
treated again the next time through the loop. This can lead to nasty
bugs that can be avoided by making a temporary copy using a slice of
the whole sequence, e.g.,
for x in a[:]:
if x < 0: a.remove(x)
Iterate over a shallow copy of the list using [:]. You're modifying a list while iterating over it, this will result in some letters being missed.
The for loop keeps track of index, so when you remove an item at index i, the next item at i+1th position shifts to the current index(i) and hence in the next iteration you'll actually pick the i+2th item.
Lets take an easy example:
>>> text = "whoops"
>>> textlist = list(text)
>>> textlist
['w', 'h', 'o', 'o', 'p', 's']
for char in textlist:
if char.lower() in 'aeiou':
textlist.remove(char)
Iteration 1 : Index = 0.
char = 'W' as it is at index 0. As it doesn't satisfies that condition you'll do noting.
Iteration 2 : Index = 1.
char = 'h' as it is at index 1. Nothing more to do here.
Iteration 3 : Index = 2.
char = 'o' as it is at index 2. As this item satisfies the condition so it'll be removed from the list and all the items to it's right will shift one place to the left to fill the gap.
now textlist becomes :
0 1 2 3 4
`['w', 'h', 'o', 'p', 's']`
As you can see the other 'o' moved to index 2, i.e the current index so it'll be skipped in the next iteration. So, this is the reason some items are bring skipped in your iteration. Whenever you remove an item the next item is skipped from the iteration.
Iteration 4 : Index = 3.
char = 'p' as it is at index 3.
....
Fix:
Iterate over a shallow copy of the list to fix this issue:
for char in textlist[:]: #note the [:]
if char.lower() in 'aeiou':
textlist.remove(char)
Other alternatives:
List comprehension:
A one-liner using str.join and a list comprehension:
vowels = 'aeiou'
text = "Hey look Words!"
return "".join([char for char in text if char.lower() not in vowels])
regex:
>>> import re
>>> text = "Hey look Words!"
>>> re.sub('[aeiou]', '', text, flags=re.I)
'Hy lk Wrds!'
You're modifying the data you're iterating over. Don't do that.
''.join(x for x in textlist in x not in VOWELS)
text = "Hey look Words!"
print filter(lambda x: x not in "AaEeIiOoUu", text)
Output
Hy lk Wrds!
You're iterating over a list and deleting elements from it at the same time.
First, I need to make sure you clearly understand the role of char in for char in textlist: .... Take the situation where we have reached the letter 'l'. The situation is not like this:
['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
char
There is no link between char and the position of the letter 'l' in the list. If you modify char, the list will not be modified. The situation is more like this:
['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
char = 'l'
Notice that I've kept the ^ symbol. This is the hidden pointer that the code managing the for char in textlist: ... loop uses to keep track of its position in the loop. Every time you enter the body of the loop, the pointer is advanced, and the letter referenced by the pointer is copied into char.
Your problem occurs when you have two vowels in succession. I'll show you what happens from the point where you reach 'l'. Notice that I've also changed the word "look" to "leap", to make it clearer what's going on:
advance pointer to next character ('l') and copy to char
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'l'
char ('l') is not a vowel, so do nothing
advance pointer to next character ('e') and copy to char
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'e'
char ('e') is a vowel, so delete the first occurrence of char ('e')
['H', 'e', 'y', ' ', 'l', 'e', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', <- 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
^
advance pointer to next character ('p') and copy to char
['H', 'e', 'y', ' ', 'l', 'a', 'p', ' ', 'W', 'o', 'r', 'd', 's', '!']
-> ^
char = 'p'
When you removed the 'e' all the characters after the 'e' moved one place to the left, so it was as if remove had advanced the pointer. The result is that you skipped past the 'a'.
In general, you should avoid modifying lists while iterating over them. It's better to construct a new list from scratch, and Python's list comprehensions are the perfect tool for doing this. E.g.
print ''.join([char for char in "Hey look Words" if char.lower() not in "aeiou"])
But if you haven't learnt about comprehensions yet, the best way is probably:
text = "Hey look Words!"
def anti_vowel(text):
textlist = list(text)
new_textlist = []
for char in textlist:
if char.lower() not in 'aeiou':
new_textlist.append(char)
return "".join(new_textlist)
print anti_vowel(text)
List Comprehensions:
vowels = 'aeiou'
text = 'Hey look Words!'
result = [char for char in text if char not in vowels]
print ''.join(result)
Others have already explained the issue with your code. For your task, a generator expression is easier and less error prone.
>>> text = "Hey look Words!"
>>> ''.join(c for c in text if c.lower() not in 'aeiou')
'Hy lk Wrds!'
or
>>> ''.join(c for c in text if c not in 'AaEeIiOoUu')
'Hy lk Wrds!'
however, str.translate is the best way to go.
You shouldn't delete items from list you iterating through:
But you can make new list from the old one with list comprehension syntax. List comprehension is very useful in this situation. You can read about list comprehension here
So you solution will look like this:
text = "Hey look Words!"
def anti_vowel(text):
return "".join([char for char in list(text) if char.lower() not in 'aeiou'])
print anti_vowel(text)
It's pretty, isn't it :P
Try to not use the list() function on a string. It will make things a lot more complicated.
Unlike Java, in Python, strings are considered as arrays. Then, try to use an index for loop and del keyword.
for x in range(len(string)):
if string[x].lower() in "aeiou":
del string[x]

Categories

Resources