遍历Python中的列表 [英] Iterating through a list in Python

查看:101
本文介绍了遍历Python中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历列表,并获取列表的每个部分,对其进行编码,然后将结果全部合并起来.例如,我有一个字符串,该字符串生成一个列表,每个元素的长度为16个字符.

I am trying to iterate through a list and take each part of the list, encode it and join the result up when it is all done. As an example, I have a string which produces a list with each element being 16 characters in length.

message = (u'sixteen-letters.sixteen-letters.sixteen-letters.sixteen-letters.')
result = split16(message, 16)
msg = ';'.join(encode(result.pop(0)) for i in result)

encode函数采用16字节的字符串并返回结果.但是,通过编写方式,它只能编码列表中一半的元素.

The encode function takes a 16 byte string and returns the result. However with the way it is written, it only encodes half of the elements in the list.

如果我尝试理解:

result = [encode(split16(message, 16) for message in list_of_messages)]
result = ''.join(result)

它导致立即发送整个列表.我需要做的是将每个元素分别发送到encode函数,获取结果,然后将它们连接在一起.

It results in the whole list being sent at once. What I need to do is send each element to the encode function separately, get the result then join them together.

有没有简单的方法可以实现这一目标?

Is there an easy way of achieving this?

推荐答案

您是否正在尝试执行类似的操作?

Are you trying to do something like this?

';'.join(encode(i) for i in message.split('.'))

当然可能只是

';'.join(encode(i) for i in result)

如果您的split16函数足够复杂.

if your split16 function complicated enough.

这篇关于遍历Python中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆