Python: how to set value to a list element -
this question has answer here:
i have list, named mac, saves 6-bytes mac address. want set last byte 0, use:
mac[5] = 0 but gives me error:
typeerror: 'str' object not support item assignment how fix error?
because mac str , strings immutable in python.
mac = list(mac) mac[5] = '0' mac = ''.join(mac) #to mac in str or use bytearray, function mutable string.
>>> x = bytearray('abcdef') >>> x bytearray(b'abcdef') >>> x[5] = '0' >>> x bytearray(b'abcde0') >>> str(x) 'abcde0'
Comments
Post a Comment