How to cast string back into a list

前端 未结 3 1327
后悔当初
后悔当初 2021-01-12 13:43

I have a list:

ab = [1, 2, a, b, c]

I did:

strab = str(ab).

So strab is now a string.

<
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 13:55

    In the context of setting a numpy array element with a sequence, you can use the built-in join to bypass setting it to a string:

    str_list_obj = '-'.join(list_obj)
    

    and afterwards when needed split the string sequence again with the same connector (provided it does not appear in the list's strings):

    og_list_obj = str_list_obj.split("-")

提交回复
热议问题