Home > Net >  how to separate alternating digits and characters in string into dict or list?
how to separate alternating digits and characters in string into dict or list?

Time:04-01

'L134e2t1C1o1d1e1'

the original string is "LeetCode"

but I need to separate strings from digits, digits can be not only single-digit but also 3-4 digits numbers like 345.

My code needs to separate into dict of key values; keys are characters and numbers is the digit right after the character. Also create 2 lists of separate digits, letters only.

expected output:

letters = ['L', 'e', 't', 'C', 'o', 'd', 'e']
digits = [134,2,1,1,1,1,1]

This code is not properly processing this.

def f(s):

    d = dict()
    letters = list()
    # letters = list(filter(lambda x: not x.isdigit(), s))

    i = 0
    while i < len(s):
        print('----------------------')
        if not s[i].isdigit():

            letters.append(s[i])

        else:
            j = i
            temp = ''
            while j < len(s) and s[j].isdigit():
                j  = 1

            substr = s[i:j]
            print(substr)
            
        i  = 1

    print('----END -')
    print(letters)

CodePudding user response:

With the following modification your function separates letters from digits in s:

def f(s):
    letters = list()
    digits = list()
    i = 0
    while i < len(s):
        if not s[i].isdigit():
            letters.append(s[i])
            i  = 1
        else:
            j = i
            temp = ''
            while j < len(s) and s[j].isdigit():
                j  = 1
            substr = s[i:j]
            i = j
            digits.append(substr)
    print(letters)
    print(digits)
f('L134e2t1C1o1d1e1')

As said in my comments you didn't update i after the inner loop terminates which made i go back to a previous and already processed index.

CodePudding user response:

If I would be limited to not use regex I would do it following way

text = 'L134e2t1C1o1d1e1'
letters = [i for i in text if i.isalpha()]
digits = ''.join(i if i.isdigit() else ' ' for i in text).split()
print(letters)
print(digits)

output

['L', 'e', 't', 'C', 'o', 'd', 'e']
['134', '2', '1', '1', '1', '1', '1']

Explanation: for letters I use simple list comprehension with condition, .isalpha() is str method which check if string (in this consisting of one character) is alphabetic. For digits (which should be rather called numbers) I replace non-digits using single space, turn that into string using ''.join then use .split() (it does split on one or more whitespaces). Note that digits is now list of strs rather than ints, if that is desired add following line:

digits = list(map(int,digits))

CodePudding user response:

You edited your question and I got a bit confused, so here is a really exhaustive code giving you a list of letters, list of the numbers, the dict with the number associated with the number, and finally the sentence with corresponding number of characters ...

def f(s):
    letters = [c for c in s if c.isalpha()]
    numbers = [c for c in s if c.isdigit()]

    mydict = {}
    currentKey = ""
    for c in s:
        print(c)
        if c.isalpha():
            mydict[c] = [] if c not in mydict.keys() else mydict[c]
            currentKey = c
        elif c.isdigit():
            mydict[currentKey].append(c)

    sentence = ""
    for i in range(len(letters)):
        count = int(numbers[i])
        while count > 0:
            sentence  = letters[i]
            count -= 1

    print(letters)
    print(numbers)
    print(mydict)
    print(sentence)

CodePudding user response:

Your string only had two e's, so I've added one more to complete the example. This is one way you could do it:

import re

t = 'L1e34e2t1C1o1d1e1'
print(re.sub('[^a-zA-Z]', '', t))

Result:

LeetCode

I know you cannot use regex, but to complete this answer, I'll just add a solution:

def f(s):
    d = re.findall('[0-9] ', s)
    l = re.findall('[a-zA-Z]', s)
    print(d)
    print(l)

f(t)

Result:

['134', '2', '1', '1', '1', '1', '1']
['L', 'e', 't', 'C', 'o', 'd', 'e']

CodePudding user response:

letters = []
digits = []
dig = ""
for letter in 'L134e2t1C1o1d1e1':
    if letter.isalpha():
        # do not add empty string to list
        if dig:
            # append dig to list of digits
            digits.append(dig)
            dig = ""
        letters.append(letter)
        # if it is a actual letter continue
        continue
    # add digits to `dig`
    dig = dig   letter

Try this. The idea is to skip all actual letters and add the digits to dig.

  • Related