0

I'm trying to create a program where I compare two strings' characters. I'm trying to make it where the second string has to have the same characters as the first.

So if the first string is "Hello" and the second is "Helloo" it will turn out as false, but if the second string is "ello" it will be true.

I have both strings in their own dictionary that counts the amount of each character. I'm trying to do this :

if Dictionary1 >= Dictionary2 :
        print('True')

I don't know if I am thinking of this in an incorrect fashion, but I can't get this to work. I'm still relatively new to Python so if possible, please keep the explanations simple. Thanks !

Bill
  • 3
  • 3
  • Please post the relevant code here, at the moment it's not enough to give a concrete answer. Are you saying you have a dictionary like `Dictionary1 = {'helloo': 6}`? You should provide a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve). – roganjosh Feb 12 '17 at 16:28
  • Sorry, I have it as String1 = `input('Enter Text') String 2 = input('Text').` Then each is set to a dictionary and counted for characters. – Bill Feb 12 '17 at 16:29
  • But that doesn't relate to dictionaries. Please edit sufficient information into the original question so we can see what you're actually doing. – roganjosh Feb 12 '17 at 16:30
  • Is [this answer](http://stackoverflow.com/a/3484456/424173) what you're looking for? – hoyland Feb 12 '17 at 16:35

2 Answers2

1

Using all and collections.Counter which is a high performance alternative for your counting dict:

> from collections import Counter

> string_1 = 'hello'
> string_2 = 'ello'
> c1, c2  = map(Counter, (string_1, string_2))
# c1, c2  = Counter(string_1), Counter(string_2)
> all(c1[c] >= c2[c] for c in c2)
True

You must make sure that for every letter in string_2, this letter's count in string_1 is greater or equal to its count in string_2. You cannot just compare two dictionaries via >=

user2390182
  • 72,016
  • 6
  • 67
  • 89
-1
>>> str1 = "Hello"
>>> str2 = "ello"
>>> print (str2 in str1)
True
>>> print (str1 in str2)
False
Thomas Lehoux
  • 1,158
  • 9
  • 13