Skip to content Skip to sidebar Skip to footer

Assigning A String With A Combination Of Two And Separate Them In A List

This is a simple example of what I am trying to do. Say, I have a random list which is the following: aa = 1 b = 2 c = 3 ao = 4 uw = 5 d = 6 ih = 7 I give the input to the progra

Solution 1:

How about ...

str_converter = {'aa': 1, 'b':2, 'c':3, 'ao': 4, 'uw':5, 'd':6, 'ih':7}
pre_converter = {'aw': ['ao', 'uw'], 'ay': ['ao', 'ih']}

input = ['b', 'd', 'aa', 'aw', 'ay', 'c']

work_list = []

for in_string ininput:
    converted_str = pre_converter.get(in_string)
    if converted_str isNone:
        work_list.append(in_string)
    else:
        work_list.extend(converted_str)

for work_string in work_list:
    print str_converter.get(work_string)

Solution 2:

Step 1. Assign the phoneme values. You've done some of that. You need to finish all 37. It shouldn't be too hard.

aa = 1b = 2c = 3ao = 4uw = 5d = 6ih = 7

Step 2. Assign dipthong variables which are lists. This isn't too hard, either.

aw= [ ao, uw ]
ay= [ ao, ih ]

Step 3. Create a nested list. The dipthongs will become sub-lists.

nested = [ b, d, aa, aw, ay, c ]

Step 4. Flatten the nested list.

defflatten( some_list ):
    for x in some_list:
        ifisinstance(x,collections.Sequence):
            for v in flatten(x):
                yield v
        else:
            yield x

result= list( flatten( nested ) )

Solution 3:

I understand that the output is the associated number if the string is in the initial list, and if it is not, you are looking for decomposition of this string into 2 concatenated string.

First of all, you have to think of wether there is a unique decomposition or not (you might have ab+bc=ac=ad+dc for exemple).

then to solve your issue, if you are looking only for first degree decomposition (no more than 2 concatenated string), you could pre compute the concatenation in a new list.

When you have an input, you first look in the first list. If it is not in it, look at the new list of concatenated strings.

Post a Comment for "Assigning A String With A Combination Of Two And Separate Them In A List"