如何从列表列表中切片

时间:2018-10-15 18:38:19

标签: python python-3.x list

我有一个列表,列表中的每个项目都是2个数字的列表:

Update Attendance 
Set Seated = '1' 
where name in 
(select t.CC_FullName from (
    select s.*,
    r.CC_FullName,
    r.CC_Supervisor,
    r.StaffCimID
    from (
    select AgentFirstName + ' ' + AgentLastName as AgentName,
    Agent
    from pia.dbo.Five9IntraDayExtract with(nolock)
    group by AgentFirstName + ' ' + AgentLastName,
    Agent
    ) s
    inner join pia.dbo.StaffInformationNew r with(nolock)
        ON CASE
            WHEN s.Agent LIKE '%_manual' AND s.Agent = r.Five9Name_MTM THEN 1
            WHEN s.Agent NOT LIKE '%_manual' AND s.Agent = r.Five9Name THEN 1
            ELSE 0
            END = 1
            and r.EndDate is null
     ) t
     where t.CC_FullName is not null
     and t.StaffCimID is not null)

但是,当我尝试在索引处对列表进行切片时,出现错误。例如:

myList = [[1,2], [4,3], [6,7]]

返回:

n = myList[1]

那么,如何在新变量中存储此列表的索引?我的最终目标是能够访问主列表中每个列表中的各个值。

1 个答案:

答案 0 :(得分:0)

这些工具可能会有用。

myList = [[1,2], [3,4], [5,6], [7,8], [9,0]]

# iterate through the list 
# prints the list in order 1,2,3,4,5,6,7,8,9,0
for subList in myList:
    for number in subList:
        print(number)

# index the list
myList[1] == [3,4]
myList[1][0] == 3

# slice the list
myList[1:4] == [[3,4], [5,6], [7,8]]
myList[1:]  == [[3,4], [5,6], [7,8], [9,0]]
myList[:4]  == [[1,2], [3,4], [5,6], [7,8]]