Greetings,
I just want to know how to split a string using values of dictionary:

dic = {'': '000', ' ': '011', 'e': '1111', 'g': '10', 'h': '010', 'o': '00', 'p': '1101', 's': '1110', 'r': '1100'}

word = '1000011100001110001101010111111001110000'

for items in dic.values():
word.split(items)

.... and when i do this i get this:

....BUT IM ACTUALLY SUPPOSE TO GET THIS OUTPUT:

IF ANYBODY CAN HELP IT SHALL BE GREATLY APPRECIATED THANKS!

Recommended Answers

All 7 Replies

Member Avatar for Mouche

What do you mean by splitting a string using values of a dictionary? You have a for loop that will print out 9 different arrays -- one for each dictionary value used as a delimiter. It doesn't make sense that you're expecting one array value.

is the array you get if you only use the value 1100 as the delimiter

I'm not sure what you mean when you say is the expected output. That just looks like a list of the dictionary values.

To get those, you can just write

print dic.values()

Note: By delimiter, I mean the string that splits the word into different sections. For example, if ":" was the delimiter in "Hi:my:name:is:lamouche", then you would get as a resulting array. The delimiter is removed when you use split.

What I mean is that is the items d.values() (which is ) to split the word into this :

What do you mean by splitting a string using values of a dictionary? You have a for loop that will print out 9 different arrays -- one for each dictionary value used as a delimiter. It doesn't make sense that you're expecting one array value.

is the array you get if you only use the value 1100 as the delimiter

I'm not sure what you mean when you say is the expected output. That just looks like a list of the dictionary values.

To get those, you can just write

print dic.values()

Note: By delimiter, I mean the string that splits the word into different sections. For example, if ":" was the delimiter in "Hi:my:name:is:lamouche", then you would get as a resulting array. The delimiter is removed when you use split.

The delimeter's absolute value cannot be prefaced by bracketed values.

I know that u cannot use braketed values that why a for loop is used to get items from list thus, no bracketed values

The delimeter's absolute value cannot be prefaced by bracketed values.

k sorry everyone
this is wat i mean...

i want to get this:
using :
word = '1000011100001110001101010111111001110000'
lst = [ '011','1111','10','010','00','1101','1110','1100','000']
basically the items in lst i want to search the item in word then split it at that point

I know that u cannot use braketed values that why a for loop is used to get items from list thus, no bracketed values

Greetings,
I just want to know how to split a string using values of dictionary:

dic = {'': '000', ' ': '011', 'e': '1111', 'g': '10', 'h': '010', 'o': '00', 'p': '1101', 's': '1110', 'r': '1100'}

word = '1000011100001110001101010111111001110000'

for items in dic.values():
word.split(items)

.... and when i do this i get this:

....BUT IM ACTUALLY SUPPOSE TO GET THIS OUTPUT:

IF ANYBODY CAN HELP IT SHALL BE GREATLY APPRECIATED THANKS!

i want to get this:
using :
word = '1000011100001110001101010111111001110000'

But you could also get '10' & '000' for the first two, as '000' is also one of the dictionary values. You will have to weight the values and check in that order. So, to check with the length of the value as the weight, you would check for a two byte match; if not found, add a third byte and check for a three byte match; and if not found, check for a four byte match. How ever you do it you will have to use some kind of algorithm to decide a double match like the one in the example above. Then you would probably call functions in the appropriate order until a match is found, using slicing to reduce the original string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.