How to remove duplicates from a given sample string? [duplicate] - python

This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 2 years ago.
s = "aadarsh , aravind aadarsh,"
st=s.split()
lst=[]
for i in st:
if i not in lst:
lst.append(i)
print(' '.join(lst))
this is my program but am not able to get my desired output
My sample string is
s = "aadarsh , aravind aadarsh,"
and my output should be -> aadarsh , aravind
and all the duplicates should be removed including commas as well how to do it.

The issue seems to be with the fact that split should sometimes work with a comma, and sometimes with a space. Use re.split instead:
s = "aadarsh , aravind aadarsh,"
st=re.split("[\s,]+", s)
lst=[]
for i in st:
if i and i not in lst:
lst.append(i)
print(' '.join(lst))
==> aadarsh aravind
An even simpler solution would be to use a set:
s = "aadarsh , aravind aadarsh,"
# (use comprehension to drop empty strings)
lst = [x for x in set(re.split("[\s,]+", s)) if x]
print(' '.join(lst))

Related

How to capitalize every other character in a string [duplicate]

This question already has answers here:
Capitalise every other letter in a string in Python? [closed]
(5 answers)
Closed 3 years ago.
I want the program to return ' mahir ' as 'MaHiR', I have got MHR but how do I get 'a' and 'h' at their usual place ?
I have already tried slicing but that does not work
s = 'mahir'
a = list (s)
c = a[0:5:2]
for i in range (len(c)):
print (c[i].capitalize(),end = " ")
Python's strings are immutable, calling c[i].capitalize() will not change c[i], and therefore will not change s, to modify a string you must create a new one out of it, you can use str.join with a generator expression instead:
s = 'mahir'
s = ''.join(c.upper() if i % 2 == 0 else c for i, c in enumerate(s))
print(s)
Output:
MaHiR
If you want to do it using slicing, you could convert your string to a list since lists are mutable (but the string approach above is better):
s = 'mahir'
l = list(s)
l[::2] = map(str.upper, l[::2])
s = ''.join(l)
print(s)
Output:
MaHiR

How do you replace a '|' with ' ' or null string? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I removed all the null values and numbers from my data. I only have list of lists containing text strings and '|'. I want to loop over my RDD object and replace the '|' with '' or even remove it.
I tried using the map function and then I linked it to an external function
def fun(item):
newlist=list()
for i in item:
if '|' == i or '|' in i:
j=''
newlist.append(j)
else:
newlist.append(i)
return newlist
final=orginial.map(x : fun(x))
input: [['Hello','|'],..]
expected output: [['Hello',''],..]
actual output: [['Hello','|'],..]
you can use replace in python.
a = "ABCD|EFG"
a = a.replace("|", "")
i change the code you can use this:
def fun(item):
newlist=list()
for i in item:
newlist.append(i.replace("|",""))
return newlist
if you want to get rid of the empty strings you could also try this
output = []
for single_list in list_of_lists:
new_in_list = [i for i in single_list if not i is "|"]
output.append(new_in_list)
i add more example :
a = ["hello|||", "he||oagain", "|this is |", "how many ||||||||| ?"]
output = []
for i in a:
output.append(i.replace("|", ""))
print(output)
at the end output is :
['hello', 'heoagain', 'this is ', 'how many ?']

retrieve in list occurrences from text python [duplicate]

This question already has answers here:
Modifying list while iterating [duplicate]
(7 answers)
Closed 5 years ago.
Text = input('please enter your text')
l = [str(x) for x in Text.split()]
count = 0
for item in l:
for i in range(1,len(item)):
if item[i-1] == item[i]:
count +=1
if count <1:
l.remove(item)
count = 0
print (l)
the goal is : if we have a text : 'baaaaah mooh lpo mpoo' to get a list with elements they have 2 successive same characters, in this case ['baaaaah', 'mooh', 'mpoo' ]
the program is working with the mentioned example
if i use this one it's not working : 'hgj kio mpoo'
thank you
(complex)One liner:
>>> def successive_items(s):
return [x for x in s.split() if any(x[i]==x[i-1] for i in range(1,len(x)))]
>>> successive_items('baaaaah mooh lpo mpoo')
['baaaaah', 'mooh', 'mpoo']
>>> successive_items('hgj kio mpoo')
['mpoo']
In case of your code, you should not modify the list you are iterating over. Say, for example, lets have an array:
a = [1,2,3,4,5]
Now, if you iterate and remove elements (inside the loop), you would expect a to be empty. But let's check out:
>>> a = [1,2,3,4,5]
>>> for item in a:
a.remove(item)
>>> a
[2, 4]
See? It is not empty. This is better explained here.
Your program is not working for the second case because of list modification. Here is how:
Initially your list had ['hgj','kio','mpoo'].
After reading the first element you removed hgj. So the list now becomes ['kio','mpoo'].
The loop iterates the 2nd element next, and it gets mpoo (in the modified list);
kio was never read.
This might help:
sentence = input("please enter your text")
words = sentence.split()
answers = []
for each_word in words:
for idx in range(1, len(each_word)):
if each_word[idx] == each_word[idx-1]:
answers.append(each_word)
break
print(answers)
In your code, you are iterating over a list and at the same time you are modifying that very same list(by deleting elements from it), for more explanation see this answer

how can I convert string to list in python [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 6 years ago.
i am trying to parse a xml file, it works very well. I have string output, which i would like to make as list, but i doesnot work.
I get for tuple or list, that every line is a list...Somebody any idea?
def handleToc(self,elements):
for element in elements:
self.name = element.getElementsByTagName("name")[0]
self.familyname = element.getElementsByTagName("family")[0]
#self.position = element.getElementsByTagName("position")[0].firstChild.nodeValue
position = element.getElementsByTagName("position")[0].firstChild.nodeValue
liste=position.encode('utf-8')
nameslist = [y for y in (x.strip() for x in liste.splitlines()) if y]
#print names_list[1:-1]
#print ''.join(repr(x).lstrip('u')[1:-1] for x in position)
#converted_degrees = {int(value) for value in position}
liste1=tuple(liste)
print liste
print list1
and the output is:
66.5499972
70.5500028
73.7
76.3
79.4499972
83.4500028
86.6
89.2
replace
listel= tuple(liste)
with
liste1 = liste.split(' ')
split(' ') will split the string into a list of items, and access it with index
say listel[0] for first item. liste1[1] for second item and so on.

Sorting a List in Python by file name [duplicate]

This question already has answers here:
Is there a built in function for string natural sort?
(23 answers)
Closed 7 years ago.
I want to sort a List with Strings by name:
I want the following:
a = ["Datei", "Datei-1", "Datei-2", "Datei-3", "Datei-4", "Datei-5", "Datei-6", "Datei-7", "Datei-8", "Datei-9", "Datei-10", "Datei-11", "Datei-12", "Datei-13", "Datei-14", "Datei-15", "Datei-16"]
I have got the following:
a = ["Datei", "Datei-1", "Datei-10", "Datei-11", "Datei-12", "Datei-13", "Datei-14", "Datei-15", "Datei-16" , "and so on"]
I have tried:
sorted(a)
In [1896]: a = ["Datei", "Datei-1","Datei-2", "Datei-10", "Datei-11", "Datei-12", "Datei-13", "Datei-14", "Datei-15", "Datei-16" , ]
In [1897]: sorted(a, key=lambda v:int(v.split('-')[-1]) if '-' in v else 0)
Out[1897]:
['Datei',
'Datei-1',
'Datei-2',
'Datei-10',
'Datei-11',
'Datei-12',
'Datei-13',
'Datei-14',
'Datei-15',
'Datei-16']
We can sort by splitting the string, and sorting by the numeric value. However your first element is missing a value, so we could put that first, as element 0:
def sort_func(entry):
try:
return int(x.split('-')[1])
except IndexError:
return 0
new_a = sorted(a, key=sort_func)
returns
['Datei', 'Datei-1', 'Datei-2', ..., 'Datei-9', 'Datei-10', 'Datei-11', ...]

Categories

Resources