通过对两个连续的零进行条件检查进行分组来创建子列表

时间:2016-10-05 01:12:57

标签: python python-2.7

我有一个清单:

lst = [0, -7, 0, 0, -8, 0, 0, -4, 0, 0, 0, -6, 0, -4, -29, -10, 0, -16, 0, 0, 2, 3, 0, 18, -1, -2, 0, 0, 0, 0, 0, 0, 21, 10, -10, 0, -12, 3, -5, -10]

当一个值后跟两个连续的零时,我想用条件中断创建一组子列表。

所以我的中间名单要

newlst = [-7,-8,-4,[-6,-4,-29,-10,-16],[2,3,18,-1,-2],[21,10,-10,-12,3,-5,-10]]

而最终输出将是子列表的总和:

[-7,-8,-4,-65,18,-3]

我尝试在带有枚举的for循环中使用索引号但是我没有得到我想要的输出。

5 个答案:

答案 0 :(得分:6)

我根据他们是否持有任何真相来对相邻数字组成对。然后拿起真正的小组并总结它们。可能有点太复杂,但我喜欢使用any键。

>>> from itertools import groupby
>>> [sum(a for a, _ in g) for k, g in groupby(zip(lst, lst[1:] + [0]), any) if k]
[-7, -8, -4, -65, 20, -3]

(感谢blhsing和ShadowRanger的改进。)

将对数转回单身的缩短方式(首先是Python 2,第二个是Python 3):

>>> [sum(zip(*g)[0]) for k, g in groupby(zip(lst, lst[1:] + [0]), any) if k]
>>> [sum(next(zip(*g))) for k, g in groupby(zip(lst, lst[1:] + [0]), any) if k]

答案 1 :(得分:2)

以下是a_backend = Rugged::InMemory::Backend.new(opt1: 'setting', opt2: 'setting') repo = Rugged::Repository.init_at('repo_name', :bare, backend: a_backend) 循环的选项:

for

要获取相应的索引,您可以使用zeros = 0 result = [0] for i in lst: if i == 0: zeros += 1 elif zeros >= 2: # if there are more than two zeros, append the new # element to the result result.append(i) zeros = 0 else: # Otherwise add it to the last element result[-1] += i zeros = 0 result # [-7, -8, -4, -65, 20, -3]

enumerate

答案 2 :(得分:0)

如果您的所有值都出现在split中,那么Cheesy解决方案就是使用replace的{​​{1}}和bytearray方法(方便地转换为int和来自lst = [0, -7, 0, 0, -8, 0, 0, -4, 0, 0, 0, -6, 0, -4, -29, -10, 0, -16, 0, 0, 2, 3, 0, 18, -1, -2, 0, 0, 0, 0, 0, 0, 21, 10, -10, 0, -12, 3, -5, -10] # Adjust all values up by 0x80 so they fall in bytearray's expected 0-255 range bts = bytearray(x + 0x80 for x in lst) # Split on double \x80 (the value of 0 after adjustment) # then remove single \x80 from each part parts = [b.replace(b'\x80', b'') for b in bts.split(b'\x80\x80')] # Undo adjustment and unpack (and filter empty sublists out completely) newlst = [b[0] - 0x80 if len(b) == 1 else [x - 0x80 for x in b] for b in parts if b] # Or to just get the sums, no need to create newlst, just filter and sum en masse: sums = [sum(b) - 0x80 * len(b) for b in parts if b] )进行分裂:

<!DOCTYPE html>
<html>
  <head>
    <style>
      ul {
          list-style-type: none;
          margin: 0;
          padding: 0;
          overflow: hidden;
          border: 1px solid #e7e7e7;
          background-color: grey;
      }
      li {
          float: left;
      }
      li a {
          display: block;
          color: white;
          text-align: center;
          padding: 34px 85px;
          text-decoration: none;
      }
      li a:hover:not(.active) {
          background-color: #grey;
      }
      li a.active {
          color: white;
          background-color: #4CAF50;
      }
    </style>
  </head>
  <body>
    <ul>
      <li><a class="active" href="#home">Home</a></li>
      <li><a href="#Social">Social</a></li>
      <li><a href="#Comercial">Comercial</a></li>
      <li><a href="#Sobre el fotógrafo">Sobre el fotógrafo</a></li>
      <li><a href="#Contacto">Contacto</a></li>
    </ul>
  </body>
</html>

答案 3 :(得分:0)

list = lst = [0, -7, 0, 0, -8, 0, 0, -4, 0, 0, 0, -6, 0, -4, -29, -10, 0, -16, 0, 0, 2, 3, 0, 18, -1, -2, 0, 0, 0, 0, 0, 0, 21, 10, -10, 0, -12, 3, -5, -10]
lastElement = None
new_list=[]
sub_list=[]
for element in list:
    if element == 0 == lastElement:
        if sub_list == []:
            continue
        new_list.append(sub_list)
        sub_list = []
    elif element == 0:
        pass
    else:
        sub_list.append(element)
    lastElement = element
new_list.append(sub_list)
print(new_list)
[[-7], [-8], [-4], [-6, -4, -29, -10, -16], [2, 3, 18, -1, -2], [21, 10, -10, -12, 3, -5, -10]]

答案 4 :(得分:0)

map(lambda x:eval(re.sub('^,','',x,1).replace(',','+')) if(len(x))>3 else eval(x.replace(',','+')),filter(lambda x:x!=',0',re.split(r',0,0,|\|',"|".join(re.split(r'(,0){3,}',l)))))