Python的zip
有趣的一点是,它会乐意将相同的迭代器多次作为参数,让您轻松配对输入。例如:
# For efficiency, if you're on Python 2, include this line so zip is a generator that produces pairs on demand, rather than eagerly slurping the whole file
from future_builtins import zip
with open('myinput') as f:
# Creates a generator that produces only the final space separated value for each line (could be anything; not checking for hex)
final_hex = (line.rsplit(None, 1)[-1] for line in f)
# By using the same generator twice, we get the 1st, 3rd, 5th, etc. from one
# and the 2nd, 4th, 6th, etc. from the other.
for hexa, hexb in zip(final_hex, final_hex):
print("something something", hexa, hexb) # Python 3 print function
print "something something", hexa, hexb # Python 2 print statement
注意:如果输入的数据是不是偶数行,这将下降最终未成输入。如果你想要不成对的值,你可以使用itertools.zip_longest
(Python 2上的izip_longest
)。
添加'确切'简单的输入和输出。避免“东西”像东西 –
提供你已经尝试过的代码和错误 – garg10may
这有点不清楚(所以答案会不同)。对于示例输入,输出行将是什么?我回答假设你真的意味着_last_十六进制值(所以输出将是'000002ac 000008fb'),但标题可能意味着转换为10进制,或者在':'之后使用所有十六进制,而不仅仅是最后一个十六进制的术语等,你可以澄清? – ShadowRanger