马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
就是把一个列表里的连续的负数提取出来组成一个新列表存在一个大列表里
a = [1.96, 7.0, -4.3, -4.12, -8.91, 0.0, 12.73, 0.0, 2.08, 2.13, 1.09, 8.79, 25.3, 19.35, -12.0, 10.71, -14.0, 10.53, -15.69, -5.08, 16.13, 15.38, 38.64, -11.11, 6.67, -17.86, -24.24, -17.07, -6.25, -5.88, -5.56, 1.75, 1.79, -25.45, 0.0, -14.49, 3.8, -6.58, -3.7, -1.19, 28.24, 18.03, -36.0, 0.0, -5.88, 1.39, 0.0, 1.41, -8.57, 1.32, -5.33, 0.0, 3.8, 0.0, -3.95, -2.53, -4.94, -2.35, 9.2, 5.06, 65.33, 223.08, 125.0, 8.33, 10.26, -1.16, 15.29, 10.2, -18.52, 40.91, -1.61, 86.07, 54.19, -11.43, 17.1, -11.02, -4.95, -5.21, 25.09, -4.4, -8.62, 2.83, -2.75, -6.92, -4.73, -5.67, -21.43, 1.44, -10.38, -3.68, -9.29, 10.84, -10.33, -4.85, -10.83, 1.43, -3.52, 72.99, 73.84, -25.73, 16.34, -7.58, -6.69, -11.73, -16.24, 9.25, -1.61, 0.41, 5.31, -7.75, 0.84, 3.33, -6.05, -6.44, 5.05, 1.75, -10.73, 5.77, -10.45, 27.41, -6.77, 7.26, -9.16, -16.23, -15.18, 0.62, 16.56, -5.26, -16.11, -29.14, -8.41, 22.45, 10.0, -6.82, 78.86, 1.82, 4.46, -17.52, -10.88, -12.21, -27.15, 26.36, -6.47, 10.0, 11.19, 1.89, -22.84, -9.6, -6.19, -24.53, -32.5, 11.11, 6.67, 29.69, 33.73, 6.31, -28.81, 9.52, -28.26, -18.18, 1.85, 0.0, -45.45, -10.0, -44.44, -86.67, 500.0, -266.67, 125.0, -100.0, 0.0, 0.0, 400.0, -200.0, -16.67, 42.86, 200.0, 125.0, 133.33, -52.38, 380.0, 47.92, -43.66, -55.0, 72.22, 77.42, 63.64, 86.67, 27.38, 55.14, -0.6, -1.21, 54.91, 13.66, 6.97, 39.58, 5.72, 6.07, 23.41, -28.08, 8.21, -16.03, -0.52, -17.9, 6.32, -18.13, -29.76, 10.34, -3.75, -35.28, -42.86, 15.13, 9.71, 41.15, 28.78, 22.06, 3.29, 12.5, 29.09, 24.73, -36.26, 12.8, 15.71, 9.65, 0.55, -13.13, 4.25, 4.08, -13.93, -18.55, 0.0, -9.73, 18.35, -14.34, -4.75, -4.28, 5.71, 1.17, 17.87, -3.94, -4.3, 2.14, -3.77, -6.32, 6.98, -10.65, 0.73, 8.94, 3.77, -10.68, 7.42, -7.13, -4.56, -0.5, -8.33, -10.47, 1.23, -3.95, 37.03, 42.26, 11.2, -15.18, -8.78, 15.47, -28.76, -5.05, 14.25, 15.64, 21.39, -2.71, -12.07, -8.63, -2.89, -4.17, 4.35, 4.17, 6.1, 12.03, -7.05, -1.21, 10.82, 15.43, 32.2, -13.73, 0.48, 14.52, 32.22, -9.04, -5.62, -16.58, -11.86, -5.98, 9.54, 3.87, 2.44, -18.86, -5.18, 13.29, -0.65, -0.26, 0.53, 6.54, 3.56, -4.98, 1.0, 4.94, -5.3, 2.86, 1.81, -3.68, -2.22, 2.65, 2.33, 17.17, 6.25, -1.64, 4.22, 17.31, -0.64, 6.05, -15.22, -10.32, -0.5, -6.74, -11.76, 5.75, 8.21, 9.83, -1.36, -5.42, -10.22, 19.98, 12.2, 3.97, 8.55, -12.77, 15.25, 2.66, 0.22, 10.57, 4.48, -6.21, 7.91, -5.75, -6.98, -12.26, 9.29, -5.34, -1.59, 4.36, 13.46, 0.61, 17.48, 11.48, -2.07, 19.81, -7.54, 3.0, 5.93, 4.76, -6.59, -22.02, 0.63, 0.74, 8.87, -10.69, -5.58, -4.0, -0.51, -2.38, -0.4, -7.69, 5.89, -14.03, 0.55]
就是结果输出成这样:
[[-4.3, -4.12, -8.91], [-12.0], [-14.0], [-15.69, -5.08], [-11.11], [-17.86, -24.24, -17.07, -6.25, -5.88, -5.56], [-25.45], [-14.49], [-6.58, -3.7, -1.19], [-36.0], [-5.88], [-8.57], [-5.33], [-3.95, -2.53, -4.94, -2.35], [-1.16]...........................................]
就是相邻的负数就存在子列表里, 也包括只有一个负数的时候, 一遇到正数或者0就断开, 后面再遇到负数就再存到一个新的子列表里, 有没有一个看上去不是太复杂效率又比较高的写法, 请各位大神大佬教教我
a = [1.96, 7.0, -4.3, -4.12, -8.91, 0.0, 12.73, 0.0, 2.08, 2.13, 1.09, 8.79, 25.3, 19.35, -12.0, 10.71, -14.0, 10.53, -15.69, -5.08, 16.13, 15.38, 38.64, -11.11, 6.67, -17.86, -24.24, -17.07, -6.25, -5.88, -5.56, 1.75, 1.79, -25.45, 0.0, -14.49, 3.8, -6.58, -3.7, -1.19, 28.24, 18.03, -36.0, 0.0, -5.88, 1.39, 0.0, 1.41, -8.57, 1.32, -5.33, 0.0, 3.8, 0.0, -3.95, -2.53, -4.94, -2.35, 9.2, 5.06, 65.33, 223.08, 125.0, 8.33, 10.26, -1.16, 15.29, 10.2, -18.52, 40.91, -1.61, 86.07, 54.19, -11.43, 17.1, -11.02, -4.95, -5.21, 25.09, -4.4, -8.62, 2.83, -2.75, -6.92, -4.73, -5.67, -21.43, 1.44, -10.38, -3.68, -9.29, 10.84, -10.33, -4.85, -10.83, 1.43, -3.52, 72.99, 73.84, -25.73, 16.34, -7.58, -6.69, -11.73, -16.24, 9.25, -1.61, 0.41, 5.31, -7.75, 0.84, 3.33, -6.05, -6.44, 5.05, 1.75, -10.73, 5.77, -10.45, 27.41, -6.77, 7.26, -9.16, -16.23, -15.18, 0.62, 16.56, -5.26, -16.11, -29.14, -8.41, 22.45, 10.0, -6.82, 78.86, 1.82, 4.46, -17.52, -10.88, -12.21, -27.15, 26.36, -6.47, 10.0, 11.19, 1.89, -22.84, -9.6, -6.19, -24.53, -32.5, 11.11, 6.67, 29.69, 33.73, 6.31, -28.81, 9.52, -28.26, -18.18, 1.85, 0.0, -45.45, -10.0, -44.44, -86.67, 500.0, -266.67, 125.0, -100.0, 0.0, 0.0, 400.0, -200.0, -16.67, 42.86, 200.0, 125.0, 133.33, -52.38, 380.0, 47.92, -43.66, -55.0, 72.22, 77.42, 63.64, 86.67, 27.38, 55.14, -0.6, -1.21, 54.91, 13.66, 6.97, 39.58, 5.72, 6.07, 23.41, -28.08, 8.21, -16.03, -0.52, -17.9, 6.32, -18.13, -29.76, 10.34, -3.75, -35.28, -42.86, 15.13, 9.71, 41.15, 28.78, 22.06, 3.29, 12.5, 29.09, 24.73, -36.26, 12.8, 15.71, 9.65, 0.55, -13.13, 4.25, 4.08, -13.93, -18.55, 0.0, -9.73, 18.35, -14.34, -4.75, -4.28, 5.71, 1.17, 17.87, -3.94, -4.3, 2.14, -3.77, -6.32, 6.98, -10.65, 0.73, 8.94, 3.77, -10.68, 7.42, -7.13, -4.56, -0.5, -8.33, -10.47, 1.23, -3.95, 37.03, 42.26, 11.2, -15.18, -8.78, 15.47, -28.76, -5.05, 14.25, 15.64, 21.39, -2.71, -12.07, -8.63, -2.89, -4.17, 4.35, 4.17, 6.1, 12.03, -7.05, -1.21, 10.82, 15.43, 32.2, -13.73, 0.48, 14.52, 32.22, -9.04, -5.62, -16.58, -11.86, -5.98, 9.54, 3.87, 2.44, -18.86, -5.18, 13.29, -0.65, -0.26, 0.53, 6.54, 3.56, -4.98, 1.0, 4.94, -5.3, 2.86, 1.81, -3.68, -2.22, 2.65, 2.33, 17.17, 6.25, -1.64, 4.22, 17.31, -0.64, 6.05, -15.22, -10.32, -0.5, -6.74, -11.76, 5.75, 8.21, 9.83, -1.36, -5.42, -10.22, 19.98, 12.2, 3.97, 8.55, -12.77, 15.25, 2.66, 0.22, 10.57, 4.48, -6.21, 7.91, -5.75, -6.98, -12.26, 9.29, -5.34, -1.59, 4.36, 13.46, 0.61, 17.48, 11.48, -2.07, 19.81, -7.54, 3.0, 5.93, 4.76, -6.59, -22.02, 0.63, 0.74, 8.87, -10.69, -5.58, -4.0, -0.51, -2.38, -0.4, -7.69, 5.89, -14.03, 0.55]
res, r = [], []
for i in a:
flg = True if i < 0 else False
if not flg:
if r:
res.append(r)
r = []
else:
r.append(i)
else:
if r:
res.append(r)
print(res)
|