简体   繁体   English

什么是Pythonic附加到bytearray列表的方法?

[英]What's the Pythonic way to append to a bytearray a list?

I'm trying to append the contents of a list (which only contains hex numbers) to a bytearray. 我正在尝试将列表的内容(仅包含十六进制数字)附加到bytearray。 Right now I'm doing this and it works: 现在我正在做这个并且它有效:

payload = serial_packets.get()
final_payload = bytearray(b"StrC")
final_payload.append(len(payload))
for b in payload:
   final_payload.append(b)

However, I believe it's not very Pythonic. 但是,我相信它不是非常Pythonic。 Is there a better way to do this? 有一个更好的方法吗?

tldr; tldr; How can I append payload to final_payload in a more Pythonic way? 如何以更加Pythonic的方式将有效负载附加到final_payload?

You can extend , you don't need to iterate over payload: 您可以扩展 ,您不需要迭代有效负载:

final_payload.extend(payload)

Not sure you want final_payload.append(len(payload)) either. 不确定你是否想要final_payload.append(len(payload))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM