如何从单个数字整数列表创建一个双位整数列表?

时间:2016-03-08 20:46:55

标签: python list

l=[1,4,5,6,3,2,4,0]

我想要成为

newlist=[14,56,32,40]

我试过了

for i in l[::2]:
   newlist.append(i)

该怎么做

3 个答案:

答案 0 :(得分:4)

您可以在列表解析中使用zip()函数:

>>> lst = [1,4,5,6,3,2,4,0]
>>> [i*10+j for i,j in zip(lst[0::2],lst[1::2])]
[14, 56, 32, 40]

作为覆盖具有奇数项目的列表的更一般方法,您可以使用itertools.izip_longest(在python 3.X itertools.zip_longest中): 将0作为fillvalue参数传递:

>>> lst=[1,4,5,6,3,2,4]
>>> 
>>> from itertools import izip_longest
>>> [i*10+j for i,j in izip_longest(lst[0::2],lst[1::2], fillvalue=0)]
[14, 56, 32, 40]

答案 1 :(得分:1)

另一种解决方案,只是为了好玩

lst = [1,4,5,6,3,2,4,0]
it = iter(lst)
for i in it:
  num = int(str(i) + str(next(it)))
  print num

答案 2 :(得分:0)

Private Function LoadHOQuery()

progComb_Filter = "Like '' & ""*"" Or (Production_Readiness.[All Engine Models]) Is Null"
partNoCmb_Filter = "Like '' & ""*"" Or (Production_Readiness.Material) Is Null"
commodCmb_Filter = "Like '' & ""*"" Or (Production_Readiness.Commodities) Is Null"
custCmb_Filter = "Like '' & ""*"" Or (Production_Readiness.[Buyer Name]) Is Null"
subCodeCmb_Filter = "Like '' & ""*"" Or (Production_Readiness.[Vendor]) Is Null"
subCmb_Filter = "Like '' & ""*"" Or (Production_Readiness.[Vendor Name]) Is Null"
startDateTxt_Filter = "5/16/1892"
endDateTxt_Filter = Date
Dim sqlCmd As String

Dim isFormLoades As Boolean
If (openCondition = "Buyer") Then
    isFormLoades = IsLoaded("Handoff_Frm")
    If (isFormLoades) Then
        sqlCmd = "SELECT DISTINCT Production_Readiness.* " & _
                 "FROM Production_Readiness " & _
                 "WHERE (((Production_Readiness.Material) " & partNoCmb_Filter & ") AND ((Production_Readiness.[All Engine Models]) " & progComb_Filter & ") AND " & _
                        "((Production_Readiness.Commodities) " & commodCmb_Filter & " ) AND ((Production_Readiness.[Buyer Name]) " & custCmb_Filter & " ) AND " & _
                        "((Production_Readiness.Vendor) " & subCodeCmb_Filter & ") AND ((Production_Readiness.[Vendor Name]) " & subCmb_Filter & ") AND " & _
                        "((Production_Readiness.[MinOfStat-Rel Del Date     Next coming delivery])>=#" & startDateTxt_Filter & "#) AND ((Production_Readiness.[MinOfStat-Rel Del Date     Next coming delivery])<=#" & endDateTxt_Filter & "#) And" & _
                        "(((Production_Readiness.[Buyer Badge])=FctUserID()) And ((Production_Readiness.[Status]) <>  'Processed') ) ); "

        'MsgBox sqlCmd


        Me.ManageHanOff.Form.RecordSource = sqlCmd
        Me.ManageHanOff.Form.Requery
     End If
 ElseIf (openCondition = "Manager") Then

 End If

输出:

lst = [1,4,5,6,3,2,4,0,1]
length = len(lst)
newList = [i*10+j for i,j in zip(lst[::2],lst[1::2])] 
if length % 2 == 1:
    newList.append(lst[-1]*10)

print newList