(Python)格式化列表中的元素

时间:2018-09-01 15:04:37

标签: python

因此,我试图使列表中的元素长7个字节,但我自己尝试这样做并没有运气。

redChannelData = [46, 49, 50, 51, 53, 53, 54, 56, 59, 59, 60, 61, 62, 62, 64, 64, 65, 65]
redChannelList = []
for value in redChannelData:
    redChannelListBinary = bin(value)[2:]
    redChannelList.append(redChannelListBinary)
redChannelListBinaryList = [[int(b) for b in binary] for binary in redChannelList]
print(redChannelListBinaryList)
输出:

[[1, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1], [1, 1, 0, 0, 1, 0], [1, 1, 0, 0, 1, 1], [1, 1, 0, 1, 0, 1], [1, 1, 0, 1, 0, 1], [1, 1, 0, 1, 1, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 1, 1], [1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 1]]

2 个答案:

答案 0 :(得分:3)

我使用format函数并指定我对最多7位感兴趣。

; *** Variables ***
$FrameInPercent = 10

do

   $hWindow = WinWaitActive("[CLASS:SunAwtDialog;TITLE:Expression editor]", "") ; *** Wait here until the Expression editor window is opened ***
   WinActivate($hWindow)

   ; *** Calculate distance from screen edge for Expression editor window ***
   $DeskSize = _GetWorkingArea()
   $WindowWidth = $DeskSize[2]-($DeskSize[2]*(0.01*2*$FrameInPercent))
   $WindowHeight = $DeskSize[3]-($DeskSize[3]*(0.01*2*$FrameInPercent))
   $windowStartLeft = ($DeskSize[2]*(0.01*$FrameInPercent))
   $windowStartTop = ($DeskSize[3]*(0.01*$FrameInPercent))

   WinMove($hWindow, "",$windowStartLeft, $windowStartTop, $WindowWidth, $WindowHeight)

   ;WinMove($hWindow, "",71, 116, 1815, 863)

   Do
      Sleep(1000) ; calmly idle around as long as the Expression editor window is    open
   until NOT(WinExists($hWindow))

until False ; *** Loop forever ***

;===============================================================================
;
; Credits for this function goes to: https://www.autoitscript.com/forum/topic/53788-getting-windows-desktop-size/
; Function Name:    _GetWorkingArea()
; Description:    Returns the coordinates of desktop working area rectangle
; Parameter(s):  None
; Return Value(s):  On Success - Array containing coordinates:
;                       $a[0] = left
;                       $a[1] = top
;                       $a[2] = right
;                       $a[3] = bottom
;                  On Failure - 0
;
;===============================================================================
Func _GetWorkingArea()
#cs
BOOL WINAPI SystemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni);
uiAction SPI_GETWORKAREA = 48
#ce
    Local $dRECT = DllStructCreate("long; long; long; long")
    Local $spiRet = DllCall("User32.dll", "int", "SystemParametersInfo", _
                        "uint", 48, "uint", 0, "ptr", DllStructGetPtr($dRECT), "uint", 0)
    If @error Then Return 0
    If $spiRet[0] = 0 Then Return 0
    Local $aRet[4] = [DllStructGetData($dRECT, 1), DllStructGetData($dRECT, 2), DllStructGetData($dRECT, 3), DllStructGetData($dRECT, 4)]
    Return $aRet
EndFunc

提供输出

redChannelData = [46, 49, 50, 51, 53, 53, 54, 56, 59, 59, 60, 61, 62, 62, 64, 64, 65, 65]
redChannelList = []
for value in redChannelData:
    redChannelListBinary = format(value, '07b')
    redChannelList.append(redChannelListBinary)
redChannelListBinaryList = [[int(b) for b in binary] for binary in redChannelList]
print(redChannelListBinaryList)

答案 1 :(得分:1)

您可以尝试这样:

redChannelData = [46, 49, 50, 51, 53, 53, 54, 56, 59, 59, 60, 61, 62, 62, 64, 64, 65, 65]
redChannelList = []
for value in redChannelData:
    redChannelListBinary = bin(value)[2:]
    while len(redChannelListBinary) < 7:
        redChannelListBinary = '0' + redChannelListBinary
    redChannelList.append(redChannelListBinary)
redChannelListBinaryList = [[int(b) for b in binary] for binary in redChannelList]
print(redChannelListBinaryList)

输出:

[[0, 1, 0, 1, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1], [0, 1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 1, 1], [0, 1, 1, 1, 0, 1, 1], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 1], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 1]]