This question already has answers here:
Removing duplicate characters from a string
(15 answers)
Closed 2 years ago.
I have a list l = ['AAB', 'CAA', 'ADA'] . I want to get the following list without duplicated characters new_l = ['AB','CA','AD']. I am trying to iterate on a nested loop but I'm not sure this is the best way to accomplish this. here is my try:
new_l = []
for i in range(0,len(l)-1):
for j in range(0,len(l)-1):
if l[i][j] != l[i+1][j+1]:
new_l = ..............
Can someone help me on how to get a set by iterating over every element of this list of strings ?
You can easily do it, since a string is also a list.
strl = ['AAB', 'CAA', 'ADA']
new_strl = []
for s in strl:
new_strl.append("".join(set(s)))
print(new_strl)
Set can mess order of characters. Better use OrderedDict:
from collections import OrderedDict
strl = ['AAB', 'CAA', 'ADA']
result = ["".join(OrderedDict.fromkeys(s)) for s in strl]
l = ['AAB', 'CAA', 'ADA']
new_l = [''.join(sorted(set(x))) for x in l]
#op
['AB', 'AC', 'AD']
Related
This question already has answers here:
How to remove duplicates only if consecutive in a string? [duplicate]
(9 answers)
Closed 4 years ago.
I convert string a to a list and I want the loop to create tabb = ['a', 'b', 'c', 'a']
a = aaabbbbcccaaa
taba = list(a)
tabb = []
for i in taba:
for j in range(len(tabb)):
if not i[j] == i[j-1]:
tabb.append(i[j])
print (tabb)
But apparently my solution gives tabb = []
Do You have any better and simple ideas to make it work?
groupby from itertools is your ally:
from itertools import groupby
a = 'aaabbbbcccaaa'
res = [x for x, _ in groupby(a)]
print(res) # -> ['a', 'b', 'c', 'a']
The solution without any libraries (the one you were trying to arrive at) would be:
res = [a[0]]
for i, c in enumerate(a[1:]):
if c != a[i]:
res.append(c)
which has the same outcome of course.
This question already has answers here:
Split by comma and strip whitespace in Python
(10 answers)
Closed 4 years ago.
Input list example = ['listen, silent', 'dog, fog', 'colour, simple']
how do I return a nested list from the example in pairs, to look like this:
[[word1,word2], [word3,word4]...etc]
please, thank you
I have tried list comprehension,
my_list1 = [i[1] for i in my_list]
my_list2 = [i[0] for i in my_list]
but it took out only the first letter instead of word... hoping for it to look like;
[listen, silent],[dog, fog]...etc
You can split each word in the list using , as a separator:
l = ['listen, silent', 'dog, fog', 'colour, simple']
l = [elem.split(', ') for elem in l]
print(l)
Output:
[['listen', 'silent'], ['dog', 'fog'], ['colour', 'simple']]
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 6 years ago.
For example, suppose we have a string:
'abcdefg'
And we need to get a list like this:
['ab', 'bc', 'cd', 'de', 'ef', 'fg']
we should not use any kind of library
Here is my solution:
def str_split(s):
s = iter(s)
ch1=''
ch2=''
chars_list=[]
while True:
try:
ch1 = ch2 or next(s)
ch2 = next(s)
chars_list.append(ch1 + ch2)
except:
break
return chars_list
I wonder is there a better solution? Maybe it is possible to use list comprehension like here?
You can simply use zip() and a list comprehension:
chars_list = [ch1 + ch2 for ch1, ch2 in zip(s, s[1:])]
More generally, if you need a solution for any n:
n = 3
chars_list = [s[i:i+n] for i in range(0, len(s) - n + 1, n - 1)]
# ['abc', 'cde', 'efg']
You could try this (hacky) solution:
def str_split(s):
return [s[start:end] for start, end in enumerate(range(2, len(s)+1))]
Delgan's zipping solution seems more elegant though :)
This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 6 months ago.
I try to make flat list. Now I have list:
L=['aa',['bb','cc']]
and I try:
L=['aa',['bb','cc']]
new=[]
for i in L:
print i
new+=i
print new
and I got:
'aa'
['bb','cc']
['a','a','bb','cc']
Why in print i=0 = 'aa' and in new+=i i=0 is only 'a'?
How i could get list ['aa','bb','cc']?
In general, meaning when you don't know the depth of the original list, this should work:
L=['aa',['bb','cc', ['dd', 'ee']], 'ff']
new = []
for l_item in L:
stack = [ l_item ]
while stack:
s_item = stack.pop(0)
if isinstance(s_item, list):
stack += [ x for x in s_item ]
else:
new.append(s_item)
print new
This gives:
['aa', 'bb', 'cc', 'dd', 'ee', 'ff']
Well, don't forget that strings are iterable in Python.
>>> new = []
>>> new += 'aa'
>>> print new
['a', 'a']
To be sure of adding what you want, you can proceed this way:
>>> L = ['aa',['bb','cc']]
>>> new = []
>>> for e in L:
... new.extend(e if type(e) == list else (e,))
>>> print new
['aa', 'bb', 'cc']
Seriously,
P.S. You can look at this post ... for more information.
This happens because you iterate over 'aa', basically treating it like it was ['a', 'a'].
If you want to avoid iterating over strings, you can look at the type:
for i in L:
if isinstance(i, list):
new += i
else:
new.append(i)
See this question for more details and how to do it recursively:
Flatten (an irregular) list of lists
This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
How to convert list to string [duplicate]
(3 answers)
Closed 9 years ago.
I am trying, in the following code, to encrypt a message. The problem is that my result comes up in a list format instead of a string. How do I make it into a string?
You need to flatten the nested lists in your result and then turn it into a string. Here's one way to do it:
>>> import itertools
>>> result = [['I', 'R', 'A', ' ', 'O'], [' ', 'E', 'D', 'Y', 'U']]
>>> ''.join(itertools.chain(*result))
'IRA O EDYU'
finalArray is clearly a list:
finalArray = []
To convert it to a string, use join:
print ''.join(finalArray)
But first, you probably do not want these nested lists. You should use extend, not append:
def stringEncrypter(A):
length = len(A)
finalArray = []
if length%2 == 0:
firstArray=[]*(length/2)
secondArray=[]*(length/2)
else:
firstArray=[]*((length+1)/2)
secondArray=[]*((length-1)/2)
for x in range(0, length-1):
if x%2 == 0:
firstArray.append(A[x:x+1])
secondArray.append(A[x+1:x+2])
finalArray.extend(firstArray)
finalArray.extend(secondArray)
print ''.join(finalArray)