How can i check if a string has some of the same characters in it in Python? - string

In my program, when a user inputs a word, it needs to be checked for letters that are the same.
For example, in string = "hello", hello has 2 'l's. How can i check for this in a python program?

Use a Counter object to count characters, returning those that have counts over 1.
from collections import Counter
def get_duplicates(string):
c = Counter(string)
return [(k, v) for k, v in c.items() if v > 1]
In [482]: get_duplicates('hello')
Out[482]: [('l', 2)]
In [483]: get_duplicates('helloooo')
Out[483]: [('l', 2), ('o', 4)]

You can accomplish this with
d = defaultdict(int)
def get_dupl(some_string):
# iterate over characters is some_string
for item in some_string:
d[item] += 1
# select all characters with count > 1
return dict(filter(lambda x: x[1]>1, d.items()))
print(get_dupl('hellooooo'))
which yields
{'l': 2, 'o': 5}

Related

swap the keys and values in a dictionary by storing the user input in dic

First the input should be dic length consider 3. then the input to a dic is keys and values separated by spaces i,e
"A 1
B 2
C 1"
now dic={A:1, B:2, C:1}
At first the keys and values and should be swapped, and if there are same keys and there values should be merged in a list and assigned to the same key as shown below.(these program should work for any length of dictionary)
the output should be dicout={1:['A','C'], 2:B}.
Thank you.
Define:
from collections import defaultdict
def make_dict(s):
d = defaultdict(list)
xs = s.split(" ")
for k, v in zip(xs[1::2], xs[::2]):
d[k].append(v)
for k, v in d.items():
if len(v) == 1:
d[k] = v[0]
return dict(d)
Example usage:
>>> make_dict("A 1 B 2 C 1")
{'1': ['A', 'C'], '2': 'B'}

Add counts to dict values

arr = [1,1,3,2,2,4]
d={i:[] for i in range(max(arr)+1)}
for i in arr:
if i in d.keys():
d[i] += 1
Am trying to get a result of {0:0, 1:2, 2:2, 3:1, 4:1} but get getting
TypeError: 'int' object is not iterable
Can any kind soul point to me what's the error? Thanks !
You might wanna do this:
arr = [1,1,3,2,2,4]
d = {i:0 for i in range(max(arr)+1)}
for i in arr:
if i in d.keys():
d[i] += 1
Or try more efficient way (you won't find keys which are not in array):
from collections import Counter
arr = [1,1,3,2,2,4]
d = Counter(arr)
You just need to change the part of the list comprehension to create 0 for each number until the max number instead of an empty list
arr = [1,1,3,2,2,4]
d={i:0 for i in range(max(arr) + 1)}
for i in arr:
if i in d.keys():
d[i] += 1
print(d)
Or to use dictionary comprehension with the range of max number in arr list and count of the list.
{v: arr.count(v) for v in range(max(arr) + 1)}
Output
{0: 0, 1: 2, 2: 2, 3: 1, 4: 1}

How to find the second most repetitive character​ ​in string using python

Here in the program how can you find the second repetitive character in the string. for ex:abcdaabdefaggcbd"​
Output : d (because 'd' occurred 3 times where 'a' occurred 4 times)​
how can I get the output, please help me.
Given below is my code:
s="abcdaabdefaggcbd"
d={}
for i in s:
d[i] = d.get(i,0)+1
print(d,"ddddd")
max2 = 0
for k,v in d.items():
if(v>max2 and v<max(d.values())):
max2=v
if max2 in d.values():
print k,"kkk"
The magnificent Python Counter and its most_common() method are very handy here.
import collections
my_string = "abcdaabdefaggcbd"
result = collections.Counter(my_string).most_common()
print(result[1])
Output
('b', 3)
In case you need to capture all the second values (if you have more than one entry) you can use the following:
import collections
my_string = "abcdaabdefaggcbd"
result = collections.Counter(my_string).most_common()
second_value = result[1][1]
seconds = []
for item in result:
if item[1] == second_value:
seconds.append(item)
print(seconds)
Output
[('b', 3), ('d', 3)]
I also wanted to add an example of solving the problem using a methodology more similar to the one that you showed in your question:
my_string="abcdaabdefaggcbd"
result={}
for character in my_string:
if character in result:
result[character] = result.get(character) + 1
else:
result[character] = 1
sorted_data = sorted([(value,key) for (key,value) in result.items()])
second_value = sorted_data[-2][0]
result = []
for item in sorted_data:
if item[0] == second_value:
result.append(item)
print(result)
Output
[(3, 'b'), (3, 'd')]
Ps
Please forgive me if I took the freedom to change variable names but I think that in this way my answer will be more readable for a broader audience.
Sort the dict's items on their values (descending) and get the second item:
>>> from collections import Counter
>>> c = Counter("abcdaabdefaggcbd")
>>> vals = sorted(c.items(), key=lambda item:item[1], reverse=True)
>>> vals
[('a', 4), ('b', 3), ('d', 3), ('c', 2), ('g', 2), ('e', 1), ('f', 1)]
>>> print(vals[1])
('b', 3)
>>>
EDIT:
or just use Counter.most_common():
>>> from collections import Counter
>>> c = Counter("abcdaabdefaggcbd")
>>> print(c.most_common()[1])
Both b and d are second most repetitive. I would think that both should be displayed. This is how I would do it:
Code:
s="abcdaabdefaggcbd"
d={}
for i in s:
ctr=s.count(i)
d[i]=ctr
fir = max(d.values())
sec = 0
for j in d.values():
if(j>sec and j<fir):
sec = j
for k,v in d.items():
if v == sec:
print(k,v)
Output:
b 3
d 3
in order to find the second most repetitive character​ ​in string you can very well use collections.Counter()
Here's an example:
import collections
s='abcdaabdefaggcbd'
count=collections.Counter(s)
print(count.most_common(2)[1])
Output: ('b', 3)
You can do a lot with Counter(). Here's a link for a further read:
More about Counter()
I hope this answers your question. Cheers!

Find elements in list which occure only one time in list

I have written a function which return a list of elements which occur only once in a given list and it is working as expected but this is not what I want is there any built-in function or other method which do same functionalities without iterating:
def unique_items(ls):
return [item for item in ls if ls.count(item)==1]
print(unique_items([1,1,1,2,3,2,4,5,4]))
>>> from collections import Counter
>>> a = [1,2,3,4,5,1,2]
>>> c = Counter(a)
>>> c
Counter({1: 2, 2: 2, 3: 1, 4: 1, 5: 1})
>>> [k for k, v in c.items() if v == 1]
[3, 4, 5]
If you don't want to use an explicit loop, you can use filter:
def unique_items(ls):
return list(filter(lambda s : ls.count(s) == 1, ls))
print(unique_items([1,1,1,2,3,2,4,5,4]))
Output:
[3, 5]
Comparing some options mentioned here:
def func_1(ls):
return [
item
for item in ls
if ls.count(item) == 1]
def func_2(ls):
c = collections.Counter(ls)
return [
k
for k, v in c.items()
if v == 1]
Comparisson code:
import timeit
a_1 = [1,1,1,2,3,2,4,5,4]
a_2 = [1,1,1,2,3,2,4,5,4] * 100
for f in [func_1, func_2]:
print(f.__name__)
print(
'short list',
timeit.timeit(
'f(a)',
'from __main__ import {} as f, a_1 as a'.format(f.__name__),
number=100))
print(
'long list ',
timeit.timeit(
'f(a)',
'from __main__ import {} as f, a_2 as a'.format(f.__name__),
number=100))
Results:
func_1
short list 0.00014933500006009126
long list 0.5417057829999976
func_2
short list 0.0005539439998756279
long list 0.0029211379996922915
So far, func_2 is faster for large inputs and func_1 is slightly faster for very short inputs.

return dictionary of file names as keys and word lists with words unique to file as values

I am trying to write a function to extract only words unique to each key and list them in a dictionary output like {"key1": "unique words", "key2": "unique words", ... }. I start out with a dictionary. To test with I created a simple dictionary:
d = {1:["one", "two", "three"], 2:["two", "four",
"five"], 3:["one","four", "six"]}
My output should be:
{1:"three",
2:"five",
3:"six"}
I am thinking maybe split in to separate lists
def return_unique(dct):
Klist = list(dct.keys())
Vlist = list(dct.values())
aList = []
for i in range(len(Vlist)):
for j in Vlist[i]:
if
What I'm stuck on is how do I tell Python to do this: if Vlist[i][j] is not in the rest of Vlist then aList.append(Vlist[i][j]).
Thank you.
You can try something like this:
def return_unique(data):
all_values = []
for i in data.values(): # Get all values
all_values = all_values + i
unique_values = set([x for x in all_values if all_values.count(x) == 1]) # Values which are not duplicated
for key, value in data.items(): # For Python 3.x ( For Python 2.x -> data.iteritems())
for item in value: # Comparing values of two lists
for item1 in unique_values:
if item == item1:
data[key] = item
return data
d = {1:["one", "two", "three"], 2:["two", "four", "five"], 3:["one","four", "six"]}
print (return_unique(d))
result >> {1: 'three', 2: 'five', 3: 'six'}
Since a key may have more than one unique word associated with it, it makes sense for the values in the new dictionary to be a container type object to hold the unique words.
The set difference operator returns the difference between 2 sets:
>>> a = set([1, 2, 3])
>>> b = set([2, 4, 6])
>>> a - b
{1, 3}
We can use this to get the values unique to each key. Packaging these into a simple function yields:
def unique_words_dict(data):
res = {}
values = []
for k in data:
for g in data:
if g != k:
values += data[g]
res[k] = set(data[k]) - set(values)
values = []
return res
>>> d = {1:["one", "two", "three"],
2:["two", "four", "five"],
3:["one","four", "six"]}
>>> unique_words_dict(d)
{1: {'three'}, 2: {'five'}, 3: {'six'}}
If you only had to do this once, then you might be interested in the less efficeint but more consice dictionary comprehension:
>>> from functools import reduce
>>> {k: set(d[k]) - set(reduce(lambda a, b: a+b, [d[g] for g in d if g!=k], [])) for k in d}
{1: {'three'}, 2: {'five'}, 3: {'six'}}

Resources