How would I convert lst1 to lst2 by joining element 1 to element 2 and so on?
lst1=[' ff 55 00 90 00 92 00 ad 00 c6 00 b7 00 8d 00 98 00 87 00 8a 00 98 00 8f 00 ca 01 78 03 54 05 bf']
to
lst2=[ff55, 0090, 0092, 00ad, 00c6, 00b7, 008d, 0098, 0087, 008a, 0098, 008f, 00ca, 0178, 0354,05bf]
I tried but it was not as expected:
for i in lst:
lstNew = []
tempList = i.split()
lenList = len(tempList)
#print tempList
index = 0
while (index < lenList):
print tempList[index] + tempList[index+1]
index = index + 2
To join specific list elements (e.g., with indices 0 , 2 , and 4 ) and return the joined string that's the concatenation of all those, use the expression ''. join([lst[i] for i in [0, 2, 4]]) . The list comprehension statement creates a list consisting of elements lst[0] , lst[2] , and lst[4] .
Python String join() The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.
Use enumerate() to access every other element in a list {use-for-loop-enumerate} Use the syntax for index, element in enumerate(iterable) to iterate through the list iterable and access each index with its corresponding element .
Is this ok:
>>> lst = ['ff', '55', '00', '90', '00', '92', '00', 'ad',
'00', 'c6', '00', 'b7', '00', '8d', '00', '98',
'00', '87', '00', '8a', '00', '98', '00', '8f',
'00', 'ca', '01', '78', '03', '54', '05', 'bf']
>>> [ ''.join(x) for x in zip(lst[0::2], lst[1::2]) ]
['ff55', '0090', '0092', '00ad', '00c6', '00b7', '008d',
'0098', '0087', '008a', '0098', '008f', '00ca', '0178',
'0354', '05bf']
>>>
Or
>>> [ x+y for x,y in zip(lst[0::2], lst[1::2]) ]
['ff55', '0090', '0092', '00ad', '00c6', '00b7',
'008d', '0098', '0087', '008a', '0098', '008f',
'00ca', '0178', '0354', '05bf']
>>>
Given Your list in the Format
lst1=[' ff 55 00 90 00 92 00 ad 00 c6 00 b7 00 8d 00 98 00 87 00 8a 00 98 00 8f 00 ca 01 78 03 54 05 bf']
let us replace all the spaces and convert it into string
list1=''.join([i.replace(" ","") for i in lst1])
now we can increment each 4 character to get Result
list1= [list1[i:i+4]for i in range(0,len(list1),4)]
print list
#output=['ff55', '0090', '0092', '00ad', '00c6', '00b7', '008d', '0098', '0087', '008a', '0098', '008f', '00ca', '0178', '0354', '05bf']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With