duplicate list items when using .replace() function - python

lst = ['123,456', '"hello"', '345,678', '"bye"']
def main():
new_lst = []
for item in lst:
#print item
new_lst.append(item.replace(',','***'))
new_lst.append(item.replace('\"', ''))
return new_lst
print main()
This is quite puzzling to me. I don't know what I'm doing wrong here. I know it's a very stupid mistake but it's not clicking for me. I don't know why I get an output of:
['123***456', '123,456', '"hello"', 'hello', '345***678', '345,678', '"bye"', 'bye']
What I was hoping was for:
['123***456', 'hello', '345***678', 'bye']
Any help is greatly appreciated!

You are appending the same string twice, with two different replacements. You should chain the replaces like this
new_lst.append(item.replace(',','***').replace('\"', ''))
Even better, you can use a list comprehension here, like this
return [item.replace(',','***').replace('\"', '') for item in lst]

Related

Filtering a list of strings

The problem has the following parameters:
Write a function called only_a that takes in a list of strings and returns a list of strings that contain the letter 'a'.
This is code I am trying to use:
def only_a(starting_list):
i = 'a'
final_list = ()
for char in starting_list:
if i in starting_list:
final_list = final_list + starting_list.append[0]
return final_list
t_string_list = ['I like apples', 'I like tea', 'I don\'t', 'Never']
print(only_a(t_string_list))
I keep getting () as a result.
You should a bit change your solution to have it works:
def only_a(starting_list):
i = 'a'
final_list = []
for char in starting_list:
if i in char:
final_list.append(char)
return final_list
t_string_list = ['I like apples', 'I like tea', 'I don\'t', 'Never']
print(only_a(t_string_list))
There is a confusion of types here.
() is a tuple, [] is a list, and
final_list = final_list + starting_list.append[0] is treating final_list as a string since you are concatenating(adding) another presumably string. But
starting_list.append[0] errors are that append is not an attribute of starting_list and/or that the append list function is not subscriptable(can't be indexed). anotherlist.append(alist[0]) appends the value in alist at [0] to anotherlist.
[0] only gets you a value at index 0 of a string or tuple or list
this is why you are getting an empty tuple but I am surprised it gets that far.
for char in starting_list: implies that you are thinking of the list as a string instead of a list of strings.
So a suggestion to explore the different types in Python.

How to remove characters in a string AFTER a given character?

I have a list of tuples that I want to remove the url extensions from. Here's what it looks like
['google.com', 'google.ru', 'google.ca']
Basically, I want to remove everything after the "." in each one so that I'm returned with something like this
['google', 'google', 'google']
My instructions specifically tell me to use the split() function, but I'm confused with that as well. If it's also possible, I need to remove duplicates, so my final result would be:
['google']
Thanks for the help, sorry if my specifications are odd.
This def removes url extensions:
def removeurlextensions(L):
L2 = []
for x in range(len(L)):
L2.append(L[x].split('.')[0])
return L2
To print your list:
L = ['google.com', 'google.ru', 'google.ca']
print(removeurlextensions(L))
#prints ['google', 'google', 'google']
To remove duplicates you can use list(set()):
L = ['google.com', 'google.ru', 'google.ca']
print(list(set(removeurlextensions(L))))
#prints ['google']
You can simply use split.
ls = ['google.com', 'google.ru', 'google.ca']
print([i.split('.', 1)[0] for i in ls])
# result = ['google', 'google', 'google']
And to remove the duplicate, you might want to use set.
mod = [i.split('.', 1)[0] for i in ls]
print(list(set(mod)))
# result = ['google']
This will only work if all items are strings:
for i in range(len(my_list)):
my_list[i] = my_list[I].split('.')[0]
already_in_list = []
for item in my_list:
if item in already_in_list:
my_list.pop(item)
else:
already_in_list.append(item)
print(my_list)
I did do this from memory so if there is a bug please let me know.

can i insert of my list with List Comprehensions

I want to make an insert on the List Comprehensions.
I can do it?
t = ['test','tes']
x = ['1','2','3','4','5']
t.insert([0,i]for i in x)
If the result you're looking for is something like this
['test', 'tes', [0, 'test'], [0, 'tes']]
The code is below:
t = ['test','tes']
x = ['1','2','3','4','5']
t.extend([[0,i]for i in t])
print t
You can do it in a list comprehension but it ends up doing extra work. Just do it in a for loop:
for thing in x:
t.insert(0, thing)
t = ['test','tes']
x = ['1','2','3','4','5']
out = t + x
your out put will be ['tes','test','1','2','3','4','5','6']
or something alone those lines. python allows you to concatenate strings
The ListItemLabel accepts only strings. I want to give it more than one element in a list, I use in ListItemLabel front str, but it does not do anything.
I do not know if it's the way you think
Python:
result_true = ListProperty([])
extra = self.viewedit.adapter.data
for edit in extra:
if edit['active'] == True:
app.result_true.append(edit['text'])
#app.result_true = (edit['text'])
print app.result_true
Kivy:
ListItemLabel:
text:str(ctx.edit)
Return:
['\xce\xbb\xce\xb9\xce\xb1\xcf\x83\xcf\x84\xce\xae
\xce\xbd\xcf\x84\xce\xbf\xce\xbc\xce\xac\xcf\x84\xce\xb1',
'\xcf\x83\xce\xbf\xcf\x85\xcf\x83\xce\xac\xce\xbc\xce\xb9',
'\xcf\x84\xcf\x85\xcf\x81\xce\xaf']

Search of Element inside a multi-dimensional List Not Working. Any Clue

I am trying to search for an element inside a multi-dimensional list, but its not working. Anything wrong in my search ?? Below is the simple code and I tried searching using 'in' and 'any' but didnt work..
list1 = []
list1.append([['hello'], ['blue'], ['bla']])
print list1
list1.append([['hello'], ['blue'], ['bla']])
print list1
#if 'hello' in list1:
if any('hello' in x for x in list1):
print "Found Hello"
else:
print "Not Found Hello"
Any guess whats the error I am doing ?? Kindly share in your inputs/comments.
Thanks In Adv !
Vimo
If list1 should be a list of lists, then the problem is that you are appending a list to list1, in this case, use extend instead.
If you want to search a item in a nested list (any number of levels), you can use this function
def in_nested_list(item, x):
if not isinstance(x, list):
return x == item
else:
return any(in_nested_list(item, ix) for ix in x)
list1 = []
list1.append([['hello'], ['blue'], ['bla']])
print list1
list1.append([['hello'], ['blue'], ['bla']])
print list1
#if 'hello' in list1:
if in_nested_list('hello', list1):
print "Found Hello"
else:
print "Not Found Hello"
If you want to find 'hello' you'll have to search down one further level:
if any('hello' in x for middle_list in list1 for x in middle_list):
print "Found Hello"
You need to go one layer deeper, like:
for l in list1:
for item in l:
if 'hello' in item:
#do something
Or use list.extend()

Remove duplicates in a list while keeping its order (Python)

This is actually an extension of this question. The answers of that question did not keep the "order" of the list after removing duplicates. How to remove these duplicates in a list (python)
biglist =
[
{'title':'U2 Band','link':'u2.com'},
{'title':'Live Concert by U2','link':'u2.com'},
{'title':'ABC Station','link':'abc.com'}
]
In this case, the 2nd element should be removed because a previous "u2.com" element already exists. However, the order should be kept.
use set(), then re-sort using the index of the original list.
>>> mylist = ['c','a','a','b','a','b','c']
>>> sorted(set(mylist), key=lambda x: mylist.index(x))
['c', 'a', 'b']
My answer to your other question, which you completely ignored!, shows you're wrong in claiming that
The answers of that question did not
keep the "order"
my answer did keep order, and it clearly said it did. Here it is again, with added emphasis to see if you can just keep ignoring it...:
Probably the fastest approach, for a really big list, if you want to preserve the exact order of the items that remain, is the following...:
biglist = [
{'title':'U2 Band','link':'u2.com'},
{'title':'ABC Station','link':'abc.com'},
{'title':'Live Concert by U2','link':'u2.com'}
]
known_links = set()
newlist = []
for d in biglist:
link = d['link']
if link in known_links: continue
newlist.append(d)
known_links.add(link)
biglist[:] = newlist
Generators are great.
def unique( seq ):
seen = set()
for item in seq:
if item not in seen:
seen.add( item )
yield item
biglist[:] = unique( biglist )
This page discusses different methods and their speeds:
http://www.peterbe.com/plog/uniqifiers-benchmark
The recommended* method:
def f5(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
f5(biglist,lambda x: x['link'])
*by that page
This is an elegant and compact way, with list comprehension (but not as efficient as with dictionary):
mylist = ['aaa','aba','aaa','aea','baa','aaa','aac','aaa',]
[ v for (i,v) in enumerate(mylist) if v not in mylist[0:i] ]
And in the context of the answer:
[ v for (i,v) in enumerate(biglist) if v['link'] not in map(lambda d: d['link'], biglist[0:i]) ]
dups = {}
newlist = []
for x in biglist:
if x['link'] not in dups:
newlist.append(x)
dups[x['link']] = None
print newlist
produces
[{'link': 'u2.com', 'title': 'U2 Band'}, {'link': 'abc.com', 'title': 'ABC Station'}]
Note that here I used a dictionary. This makes the test not in dups much more efficient than using a list.
Try this :
list = ['aaa','aba','aaa','aea','baa','aaa','aac','aaa',]
uniq = []
for i in list:
if i not in uniq:
uniq.append(i)
print list
print uniq
output will be :
['aaa', 'aba', 'aaa', 'aea', 'baa', 'aaa', 'aac', 'aaa']
['aaa', 'aba', 'aea', 'baa', 'aac']
A super easy way to do this is:
def uniq(a):
if len(a) == 0:
return []
else:
return [a[0]] + uniq([x for x in a if x != a[0]])
This is not the most efficient way, because:
it searches through the whole list for every element in the list, so it's O(n^2)
it's recursive so uses a stack depth equal to the length of the list
However, for simple uses (no more than a few hundred items, not performance critical) it is sufficient.
I think using a set should be pretty efficent.
seen_links = set()
for index in len(biglist):
link = biglist[index]['link']
if link in seen_links:
del(biglist[index])
seen_links.add(link)
I think this should come in at O(nlog(n))

Categories

Resources