python小技巧收集(持续更新)

python小技巧收集

  • 字符串转数字及数字转字符串

字符串转数字及数字转字符串

常见于从数据库中取出来的字符串存储的数组,需要转为list

class _UtilPort(object):
    def _char2IntList(self, char):
        if len(char) <= 0:
            return []
        return list(set(map(int, char.split(','))))

    def _intList2char(self, int_list):
        if len(int_list) <= 0:
            return ''
        return ','.join(str(i) for i in int_list)

a = "1,2,3"
b = "3,3,4,5"
c = []
c = c+_UtilPort()._char2IntList(a)+_UtilPort()._char2IntList(b)
print(c) # [1,2,3,3,4,5]

你可能感兴趣的:(python)