Inquiry about removing duplicates - python-3.x

Alright so I'm required to eliminate spaces and duplicate values in a list (of only numbers). Here's my code:
def eliminateDuplicates(lst):
i=0
while i<len(lst):
while lst.count(lst[i])!=1:
lst.remove(lst[i])
i=i+1
print(lst)
def main():
a=input("Enter numbers: ")
lst=list(a)
while ' ' in lst:
lst.remove(' ')
eliminateDuplicates(lst)
main()
while this method is effective and works, when the input is say
Enter numbers: 1 2 3 4 5 3 2 1 1 22
The output results in
['4', '5', '3', '1', '2']
I need my program to recognize 22 and 2 as different items so it doesn't delete the last 2 and the 2 in 22. Any suggestions?
EDIT: Sorry to the two posters that have already given me answers. I am not allowed to use the set function, and order does not matter.

This doesn't do what you think it does:
b="".join(a) # doesn't do anything useful since `a` is already a string
lst=list(b) # this is converting the string to a list of characters
Try this instead:
lst = a.split() # automatically cleans up the whitespace for you
print(list(set(lst)))
Turning a list into a set and back again is a handy way to remove duplicates. It's also quite efficient compared to the way you are doing it by scanning the list over and over
If you really want to keep the eliminateDuplicates function then it can just be
def eliminate_duplicates(lst):
return list(set(lst))
def main():
a=input("Enter numbers: ")
lst = a.split() # split automatically cleans up the whitespace
print(eliminate_duplicates(lst))
if __name__ == "__main__":
main()
Edit: since you're not allowed to use set, Collections is another fairly efficient method to remove duplicates
from collections import Counter
def eliminate_duplicates(lst):
return list(Counter(lst))
This is not quite so efficient, but still much better than two nested loops
from itertools import groupby
def eliminate_duplicates(lst):
[k for k,g in groupby(sorted(lst))]

Does order matter? If not cast it to a set and then cast it back to a list.
lst = [1,2,3,3,6,4,5,6, 3, 22]
lst2 = list(set(lst))
Also, you should probably use lst = a.split(' ') rather than join
def main():
a=input("Enter numbers: ") # Input the numbers
clean_a = a.strip(); #Cleans trailing white space.
lst=list(set(clean_a.split(' '))) #Split into tokens, and remove duplicates

Related

Find an index in a list of lists using an index inside one of the lists in pyton

I'm trying to determine if there is a way to access an index essentially by making a list of lists, where each inner list has a tuple that provides essentially grid coordinates, i.e:
example = [
['a', (0,0)], ['b',(0,1)], ['c', (0,2)],
['d', (1,0)], ['e',(1,1)], ['d', (1,2)],
.....
]
and so on.
So, If I have coordinates (0,1), I want to be able to return example[1][0], or at the very least example[1] since these coordinates correlate with example[1].
I tried using index(), but this doesn't go deep enough. I also looked into itertools, but I cannot find a tool that finds it and doesn't return a boolean.
Using a number pad as an example:
from itertools import chain
def pinpad_test():
pad=[
['1',(0,0)],['2',(0,1)],['3',(0,2)],
['4',(1,0)],['5',(1,1)],['6',(1,2)],
['7',(2,0)],['8',(2,1)],['9',(2,2)],
['0',(3,1)]
]
tester = '1234'
print(tester)
for dig in tester:
print(dig)
if dig in chain(*pad):
print(f'Digit {dig} in pad')
else:
print('Failed')
print('end of tester')
new_test = pad.index((0,1)in chain(*pad))
print(new_test)
if __name__ == '__main__':
pinpad_test()
I get an value error at the initiation of new_test.
You can just yield from simple generator expression:
coords = (0, 1)
idx = next((sub_l[0] for sub_l in pad if sub_l[1] == coords), None)
print(idx)
2
You can create a function that will give you want
def on_coordinates(coordinates:tuple, list_coordinates:list):
return next(x for x in list_coordinatesif x[1] == coordinates)

Counting: How do I add a zero if a word does not occur in a list?

I would like to find keywords from a list, but return a zero if the word does not exist (in this case: part). In this example, collabor occurs 4 times and part 0 times.
My current output is
[['collabor', 4]]
But what I would like to have is
[['collabor', 4], ['part', 0]]
str1 = ["collabor", "part"]
x10 = []
for y in wordlist:
for string in str1:
if y.find(string) != -1:
x10.append(y)
from collections import Counter
x11 = Counter(x10)
your_list = [list(i) for i in x11.items()]
rowssorted = sorted(your_list, key=lambda x: x[0])
print(rowssorted)
Although you have not clearly written your problem and requirements,I think I understood the task.
I assume that you have a set of words that may or may not occur in a given list and you want to print the count of those words based on the occurrence in the given list.
Code:
constants=["part","collabor"]
wordlist = ["collabor", "collabor"]
d={}
for const in constants:
d[const]=0
for word in wordlist:
if word in d:
d[word]+=1
else:
d[word]=0
from collections import Counter
x11 = Counter(d)
your_list = [list(i) for i in x11.items()]
rowssorted = sorted(your_list, key=lambda x: x[0])
print(rowssorted)
output:
[['collabor', 2], ['part', 0]]
This approach gives the required output.
In python, to get the count of occurrence dictionary is popular.
Hope it helps!

return unique python lists of chars ignoring order

Problem:
Consider a python list of lists that contains a sequence of chars:
[['A', 'B'],['A','B','C'],['B','A'],['C','A','B'],['D'],['D'],['Ao','B']]
The goal is to return the unique lists, regardless of order:
[['A','B'],['A','B','C'],['D'],['Ao','B']]
Attempt:
I'm able to achieve my goal using many if/else statements with try/exceptions. What would be the most pythonic (faster) way to approach this problem? Thanks!
def check_duplicates(x,list_):
for li in list_:
if compare(x,li):
return True
def compare(s, t):
t = list(t) # make a mutable copy
try:
for elem in s:
t.remove(elem)
except ValueError:
return False
return not t
vars_list = [['A', 'B'],['A','B','C'],['B','A'],['C','A','B'],['D'],['D'],['Ao','B']]
second_list = []
for i in vars_list:
if check_duplicates(i,second_list):
continue
else:
second_list.append(i)
print(i)
Assuming that the elements of the nested lists are hashable, you can isolate the unique collections by constructing a set of frozensets from the nested list:
unique_sets = {frozenset(l) for l in vars_list}
# {frozenset({'D'}),
# frozenset({'A', 'B'}),
# frozenset({'A', 'B', 'C'}),
# frozenset({'Ao', 'B'})}
If you need a list-of-lists as the output, you can obtain one trivially with [list(s) for s in unique_sets].

i want to find a common character from n number string inside a single multidimensional list using python

def bibek():
test_list=[[]]
x=int(input("Enter the length of String elements using enter -: "))
for i in range(0,x):
a=str(input())
a=list(a)
test_list.append(a)
del(test_list[0]):
def filt(b):
d=['b','i','b']
if b in d:
return True
else:
return False
for t in test_list:
x=filter(filt,t)
for i in x:
print(i)
bibek()
suppose test_list=[['b','i','b'],['s','i','b'],['r','i','b']]
output should be ib since ib is common among all
an option is to use set and its methods:
test_list=[['b','i','b'],['s','i','b'],['r','i','b']]
common = set(test_list[0])
for item in test_list[1:]:
common.intersection_update(item)
print(common) # {'i', 'b'}
UPDATE: now that you have clarified your question i would to this:
from difflib import SequenceMatcher
test_list=[['b','i','b','b'],['s','i','b','b'],['r','i','b','b']]
# convert the list to simple strings
strgs = [''.join(item) for item in test_list]
common = strgs[0]
for item in strgs[1:]:
sm = SequenceMatcher(isjunk=None, a=item, b=common)
match = sm.find_longest_match(0, len(item), 0, len(common))
common = common[match.b:match.b+match.size]
print(common) # 'ibb'
the trick here is to use difflib.SequenceMatcher in order to get the longest string.
one more update after clarification of your question this time using collections.Counter:
from collections import Counter
strgs='app','bapp','sardipp', 'ppa'
common = Counter(strgs[0])
print(common)
for item in strgs[1:]:
c = Counter(item)
for key, number in common.items():
common[key] = min(number, c.get(key, 0))
print(common) # Counter({'p': 2, 'a': 1})
print(sorted(common.elements())) # ['a', 'p', 'p']

Formatting lists to display leading zero - Python 3.x

I'm trying to create a matrix with 4 rows and 10 columns and display the leading 0 for all the single digit numbers that will randomly get generated later. This is what I would like it to look like: My teacher gave me this snippet as a way to format the numbers:
print('{:02}'.format(variable))
But when I use this in my function, it gives me the error: unsupported format string passed to list.__format__
I reworked my code and was able to get the leading zero, but now the 4x10 matrix is just 40 ints side by side. Anyone able to give me some help and an explanation?
My code:
def printMatrix(matrix):
for r in range(ROWS):
for c in range(COLS):
print('{:02}'.format(matrix[r][c]), end=' ')
def main():
matrix = [0]*ROWS
for i in range(ROWS):
matrix[i] = [0]*COLS
printMatrix(matrix)
You're really close, looks like you may just need another print() after the for-loop to put a newline after each row. Try this:
def printMatrix(matrix):
for r in range(ROWS):
for c in range(COLS):
print('{:02}'.format(matrix[r][c]), end=' ')
print()
Demo
you need a 0 in front .. i.e. {0:02}
print('{0:02}'.format(variable))
This 0 refer to the index of the parameters passed in e.g. this should work too:
print('{2:02}'.format("x", "y", variable))
Your code:
def printMatrix(matrix):
for r in range(ROWS):
for c in range(COLS):
print('{0:02}'.format(matrix[r][c]), end=' ')
def main():
matrix = [0]*ROWS
for i in range(ROWS):
matrix[i] = [0]*COLS
printMatrix(matrix)

Resources