数组中[1]和[1:2]之间的差异

时间:2018-02-16 14:02:44

标签: python list tuples

SELECT

CAST((CASE WHEN TRIM(DASM#)='' THEN '0' ELSE TRIM(DASM#) END) AS INTEGER) AS DASM,
DAIDAT,
DAINV# AS DAINV,
DALIN# AS DALIN,
CAST(TRIM(DAITEM) AS INTEGER) AS DAITEM,
TRIM(DABSW) AS DABSW,
TRIM(DAPCLS) AS DAPCLS,
DAQTY,
DAPRIC,
DAICOS,
DADPAL,
(DAPRIC-DAICOS+DADPAL)*DAQTY AS PROFIT

FROM

VIPDTAB.DAILYV

WHERE

CAST((CASE WHEN TRIM(DASM#)='' THEN '0' ELSE TRIM(DASM#) END) AS INTEGER)=30 AND
TRIM(DABSW)='B' AND
DAIDAT BETWEEN (YEAR(CURDATE())*10000) AND (((YEAR(CURDATE())+1)*10000)-1) AND 
CAST(TRIM(DACOMP) AS INTEGER)=1

ORDER BY

CAST((CASE WHEN TRIM(DASM#)='' THEN '0' ELSE TRIM(DASM#) END) AS INTEGER),
DAIDAT,
DAINV#,
DALIN#
  1. 为什么#!/bin/bash perl -e ' while (<>) { if (/^(.{15}) Program(\d+): The pool size: (\d+) \[High: (\d+) Norm: (\d+) Low: (\d+)\]$/) { $timestamp = $1; $program = $2; $size = $3; $high = $4; $norm = $5; $low = $6; if (! defined $array{$timestamp}) { # it takes care of duplicate timestamps push(@timestamps, $timestamp); } $i = ($program - 1) * 4; @{$array{$timestamp}}[$i .. $i + 3] = ($size, $high, $norm, $low); } } foreach (@timestamps) { print "$_,", join(",", map {$_ + 0} @{$array{$_}}[0 .. 15]), "\n"; }' logfile 会返回列表a=([1,2], [3,4], [5,6]) b=[[1,2], [3,4], [5,6]] print(a[1]) print(a[1:2]) print(b[1]) print(b[1:2]) ,但a[1]会返回元组[3,4]
  2. 我一直认为a[2:3]([3,4])相同。但是我错了。为什么list[1](返回列表list[1:2])的长度为2,但b[1]的长度会返回长度为1的列表[3,4]的列表?

1 个答案:

答案 0 :(得分:2)

在python中,语法array[x:y]将数组从索引x切片到y-1。因此,不是返回数组的array[x],而是返回数组,只返回从xy-1的元素