Python Forum
string to list and back again as same type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string to list and back again as same type
#1
i need to do some processing on a string that requires temporarily putting its contents into a list. then it needs to be made back into a string. but there are a bunch of string types that could be involved. i want to handle them all. what is the best way to convert a list to a string and have it be the same type as the original string (still have a reference to it if that helps).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(Sep-03-2018, 02:25 PM)Skaperen Wrote: but there are a bunch of string types that could be involved. i want to handle them all
Can you describe these string types?
Reply
#3
str, unicode, bytes, bytearray
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
The 'to_list()' part is easy
def to_list(item):
    return item.split()
For the 'from_list()' part, you can use this for non empty lists (works for python >= 2.7)
_joinchar = dict([(type(k), k) for k in (' ', b' ', u' ', bytearray(b' '))])

def from_list(items):
    return _joinchar[type(items[0])].join(items)
For the empty list, you need the target type...
Reply
#5
i was playing around with ...
    original_string[0:0].join(temp_list)
... or ...
 temp_list[0][0:0].join(temp_list)
... under the assumption that the items in the list were strings of the same type. for some things, like slicing in-place, this should be enough. for others, i might need to make the items in the list be some common form, such as only string, or as only int. it looks like in this case i might need to have blocks of code for various types.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
One thing to consider is that with [0:0], you'll join with the empty string instead of a space character. That's why I introduced the _joinchar dictionary.

There is still a missing feature in my code above: it doesn't handle the case of user defined subclasses of the builtin string types. A generic function should solve this issue, that is to say code from_list() with functools.singledispatch().
Reply
#7
if i have split a string of length 17 into a list of length 17, one character per item, then i would want an empty string as the base for a .join(). the [0:0] was to get that in the same type.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Don't know what you are doing.
class ListString:
    def __init__(self, string):
        self.type = type(string)
        self.string = list(string)

    def __getitem__(self, index):
        return self.string[index]

    def __setitem__(self, index, value):
        self.string[index] = value

    def __len__(self):
        return len(self.string)

    def __bool__(self):
        return len(self) > 0

    def __str__(self):
        return str(self.string)

    def get_string(self):
        if self.type == str:
            return ''.join(self.string)
        return self.type(self.string)

a = ListString('string')
print(a)
print(a.get_string())

a = ListString(b'bytes')
print(a)
print(a.get_string())

a = ListString(bytearray(b'bytearray'))
print(a)
print(a.get_string())
99 percent of computer problems exists between chair and keyboard.
Reply
#9
i was going to do some slicing of a string to modify it, which cannot be done directly since strings are immutable. i did not want to change it to a bytearray since, some day, maybe soon, there might be some unicode in there. lists seem to be the logical choice. convert it to a list of single characters, then do the slicing i need with other lists that were converted from strings, then convert back to a string.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(Sep-06-2018, 02:43 AM)Skaperen Wrote: i did not want to change it to a bytearray since, some day, maybe soon, there might be some unicode in there.
You can use array.array(). The following code works in python 3 and 2
>>> from array import array
>>> s = array('u', u'Hello World')
>>> s[6:] = array('u', u'Skaperen')
>>> s
array('u', 'Hello Skaperen')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,175 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  reading a table which is of type string saisankalpj 2 963 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  search a list or tuple for a specific type ot class Skaperen 8 1,945 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,175 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,884 May-07-2022, 08:07 AM
Last Post: menator01
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,013 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,415 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,394 Jan-30-2021, 07:11 AM
Last Post: alloydog
Question dict value, how to change type from int to list? swissjoker 3 2,758 Dec-09-2020, 09:50 AM
Last Post: perfringo
  Make list of dates between today back to n days Mekala 3 2,398 Oct-03-2020, 01:01 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020