Home > front end >  Getting the desired string
Getting the desired string

Time:09-24

def split(word):
    return [char for char in word]
a = "8hypotheticall024y6wxz"
alp = "ABCDEFGHIKLMNOPQRSTVXYZ"
alph = alp.lower()
b= split(alph)
c = set(b)-set(a)
c = sorted(c)
c = str(c)


res = [int(i) for i in a if i.isdigit()]
num_list = [0,1,2,3,4,5,6,7,8,9]
l = set(num_list)-set(res)
l = sorted(l)
l = str(l)
print(l,c)

The output I get is

[1, 3, 5, 7, 9] ['b', 'd', 'f', 'g', 'k', 'm', 'n', 'q', 'r', 's', 'v']

The output I want is

"13579bdfgjkmnqrsuv"

How do I get it? Please provide me with the code to get rid of these square brackets, commas and quotation marks.

CodePudding user response:

Add this is in the last -

l.extend(c)
l = [str(i) for i in l]

print(''.join(l)) # 13579bdfgkmnqrsv

Additionally, you could simplify your code (No need of function and many reassignments) -

a = "8hypotheticall024y6wxz"
alp = "ABCDEFGHIKLMNOPQRSTVXYZ"
b = alp.lower()
c = sorted(set(b)-set(a))


res = [int(i) for i in a if i.isdigit()]
num_list = [0,1,2,3,4,5,6,7,8,9]
l = sorted(set(num_list)-set(res))

l.extend(c)
l = [str(i) for i in l]

print(''.join(l))

CodePudding user response:

I guess you can make another string like this

str = ""
for i in [your output list]:
    str  = i

CodePudding user response:

For the list c, which is a list of strings, you can convert it with ''.join(c) instead of str(c).

For the list l, you can do the same thing but you have to convert every element to a string. One concise way to do this is with, map(str, l). To accomplish the conversion in one line, you could to ''.join(map(str,l))

  • Related