Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Tuples - Fatal编程技术网

在Python的列表中添加元组

在Python的列表中添加元组,python,list,tuples,Python,List,Tuples,我有元组列表,当元组=(x,y=-1)时,我将每个列表分成两部分 代码如下: testList = [ [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)], [(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)], [(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0

我有元组列表,当元组=(x,y=-1)时,我将每个列表分成两部分

代码如下:

testList = [
[(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)],
[(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)],
[(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0, -0.44)],
[(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)]]

def c_slice(lst):
    for slst in lst:
        start = 0
        for idx,(_,y) in enumerate(slst):
            if y == -1:
               yield [slst[start:idx+1], slst[idx:]]
               break

out = list(c_slice(testList))

print(out[0])
# [[(0.0, -1.0)], [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)]]
print(out[1])
# [[(0.0, -0.99604032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)]]
print(out[2])
# [[(4.5, 0.154484), (5.0, -1.0)], [(5.0, -1.0), (5.5, -0.34), (6.0, -0.44)]]
print(out[3])
# [[(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)], [(90.0, -1.0)]]
我如何对我的结果设置一些限制?例如

  • 如果结果中的第一个列表没有(x,y>0),请将(0,0)添加到列表的开头。(如out[0][0]和out[1][0])
  • 如果结果中的第二个列表没有(x,y>0),请将(90,0)添加到列表的末尾。(如out[2][1]和out[3][1])
  • p、 添加元组的位置基于x坐标。例如,(0,0)具有最小的x,因此它始终添加在列表的开头。相反,(90,0)具有最大的x坐标,因此它总是添加到列表的最后一个

    我正在努力做到这一点:

    print(out[0])
    # [[(0.0, 0.0), (0.0, -1.0)], [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)]]
    print(out[1])
    # [[(0.0, 0.0), (0.0, -0.99604032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)]]
    print(out[2])
    # [[(4.5, 0.154484), (5.0, -1.0)], [(5.0, -1.0), (5.5, -0.34), (6.0, -0.44), (90.0, 0.0)]]
    print(out[3])
    # [[(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)], [(90.0, -1.0), (90.0, 0.0)]]
    

    你可以这样试试

    testList = [
        [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)],
        [(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)],
        [(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0, -0.44)],
        [(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)]
    ]
    
    
    def c_slice(rows):
        for row in rows:
            for idx, (_, y) in enumerate(row):
                if y == -1:
                    start = row[:idx + 1]
                    end = row[idx:]
                    start = [(0, 0)] + start if all([y < 0 for _, y in start]) else start
                    end = end + [(90, 0)] if all([y < 0 for _, y in end]) else end
                    yield [start, end]
                    break
    
    
    out = list(c_slice(testList))
    
    for i in out:
        print(i)
    
    testList=[
    [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)],
    [(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)],
    [(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0, -0.44)],
    [(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)]
    ]
    def c_切片(行):
    对于行中的行:
    对于idx,枚举(行)中的(_,y):
    如果y==-1:
    开始=行[:idx+1]
    结束=行[idx:]
    start=[(0,0)]+如果全部启动([y<0表示启动中的uy,y])否则启动
    结束=结束+[(90,0)]如果所有([y<0表示结束,y表示结束])否则结束
    屈服[开始,结束]
    打破
    输出=列表(c_切片(测试列表))
    对于我输入输出:
    印刷品(一)
    
    我将此解决方案编码为:

    testList = [
    [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)],
    [(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)],
    [(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0, -0.44)],
    [(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)]]
    
    def c_slice(lst):
        for slst in lst:
            start = 0
            for idx,(x,y) in enumerate(slst):
                if y == -1:
                    if x <= len(slst)/2:
                        yield [[(x, 0)] + slst[start:idx+1], slst[idx:]]
                    else:
                        yield [slst[start:idx+1] + [(x, 0)], slst[idx:]]
                    break
    
    out = list(c_slice(testList))
    
    print(out[0])
    print(out[1])
    print(out[2])
    print(out[3])
    
    testList=[
    [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)],
    [(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)],
    [(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0, -0.44)],
    [(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)]]
    def c_切片(lst):
    对于lst中的slst:
    开始=0
    对于枚举(slst)中的idx,(x,y):
    如果y==-1:
    
    如果你能深入解释你应该做什么样的工作?是否有一些规则来执行此
    [(0.0,0.0),(0.0,-1.0)]
    [(90.0,-1.0),(90.0,0.0)]
    ?你们的目标是基于内部列表的索引还是基于外部列表的索引?在前两种情况下,你会预先准备元组,但在第二种情况下,你会出现,为什么?在
    (5.0,-1.0)
    这样的情况下,你不会添加元组,为什么?@sorcereprentice嗨,我编辑了我的问题,请看一看。如果
    x,我们会在开头添加元组