Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表理解将每两个元素连接在一个列表中_Python - Fatal编程技术网

Python 列表理解将每两个元素连接在一个列表中

Python 列表理解将每两个元素连接在一个列表中,python,Python,如何通过将元素1连接到元素2等方式将lst1转换为lst2 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'] 到 我试过了,但没有达到预期: for i in lst: lstNew = [] tempList = i.split() lenList = len(tempList

如何通过将元素1连接到元素2等方式将lst1转换为lst2

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']

我试过了,但没有达到预期:

   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
lst中的i的
:
lstNew=[]
圣殿骑士=i.split()
lenList=len(圣殿骑士)
#印刷圣堂武士
索引=0
而(索引<列表):
打印模板列表[索引]+模板列表[索引+1]
索引=索引+2

您需要正确定义列表。您的列表只有一项,即字符串。这是长度为2的字符串列表:(我想这是您想要的)

然后您可以这样做来创建
lst2

lst2 = []
for i in range(0, len(lst1), 2):
    lst2.append(lst1[i] + lst1[i+1)
这是迭代
lst1
并将每2个元素放在一起,然后将其追加
lst2

是否可以:

>>> 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']
>>>


假设您有一个由空格分隔的两个字母字符组成的字符串,您可以执行以下操作:

>>> from funcy import ichunks
>>> s = "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(chunk) for chunk in ichunks(2, s.split())]
['ff55', '0090', '0092', '00ad', '00c6', '00b7', '008d', '0098', '0087', '008a', '0098', '008f', '00ca', '0178', '0354', '05bf']
>>>
这利用了我最喜欢的图书馆

更新:

如评论中所建议;下面是一个稍微好一点的版本:

>>> from funcy import ichunks
>>> s = "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"
>>> [c[:2] + c[3:5] for c in ichunks(6, s)]
['ff55', '0090', '0092', '00ad', '00c6', '00b7', '008d', '0098', '0087', '008a', '0098', '008f', '00ca', '0178', '0354', '05bf']
>>>

给出你的列表的格式

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']
让我们替换所有空格并将其转换为字符串

list1=''.join([i.replace(" ","") for i in lst1])
现在我们可以增加每4个字符以得到结果

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']

或者
[a+b为a,b为zip(…)]
如果你喜欢的话:-)ya:)那看起来更整洁你错过了结尾
]
lst
这会创建两个
lst
映射(''.join,ichunks(2,s.split())或不拆分
[c[1:3]+c[4:6]为ichunks(6,lst1)中的c创建两个副本
@James Mills当FF和55在一个长列表中同时出现时,如何仅将FF和55连接在一起“FF55”
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']
list1=''.join([i.replace(" ","") for i in lst1])
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']