我有一个这样的列表
['1133', '1300', '1418', '1443', '1473', '1600', '1601', '1988', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '2000', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2012', '2013', '2014', '2015', '2153', '2600', '3000', '3714', '3785', '3896', '3995', '4001', '4436', '5094', '5346', '8000']
如何找到包含连续的数字字符串(例如:'2012','2013','2014','2015')的子组?
谢谢!
答案 0 :(得分:3)
尝试这样的事情:
def create_model():
lay1=Conv2D(2,kernel_size=(1,1),activation='relu',padding='same')(inputs)
lay1=Conv2D(2,kernel_size=(7,7),strides=(2,2),activation='relu',padding='same')(lay1)
lay1=Conv2D(2,kernel_size=(1,1),activation='relu',padding='same')(lay1)
lay1=MaxPooling2D(pool_size=(7,7),strides=(2,2),padding='same')(lay1)
lay2=Conv2D(4,kernel_size=(1,1),activation='relu',padding='same')(inputs)
lay2=Conv2D(4,kernel_size=(7,7),strides=(2,2),activation='relu',padding='same')(lay2)
lay2=Conv2D(4,kernel_size=(1,1),activation='relu',padding='same')(lay2)
lay2=MaxPooling2D(pool_size=(7,7),strides=(2,2),padding='same')(lay2)
lay3=Conv2D(6,kernel_size=(1,1),activation='relu',padding='same')(inputs)
lay3=Conv2D(6,kernel_size=(7,7),strides=(2,2),activation='relu',padding='same')(lay3)
lay3=Conv2D(6,kernel_size=(1,1),activation='relu',padding='same')(lay3)
lay3=MaxPooling2D(pool_size=(7,7),strides=(2,2),padding='same')(lay3)
fc=concatenate([lay1,lay2,lay3])
fc=Flatten()(fc)
fc=Dense(10,activation='relu')(fc)
outputs=Dense(10,activation='softmax')(fc)
model=Model(input=inputs,output=outputs)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
输出:
l = ['1133', '1300', '1418', '1443', '1473', '1600', '1601', '1988', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '2000', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2012', '2013', '2014', '2015', '2153', '2600', '3000', '3714', '3785', '3896', '3995', '4001', '4436', '5094', '5346', '8000']
size = len(l)
i = 0
while i < size:
j = i
while j + 1 < size and int(l[j]) + 1 == int(l[j + 1]):
j += 1
if j != i:
print (l[i:j+1])
i = j + 1
答案 1 :(得分:2)
如果您将指定的所有元素都放在同一个result
列表中,这是一种实现方法
a = ['1133', '1300', '1418', '1443', '1473', '1600', '1601', '1988', '1990', '1991', '1992', '1993', '1994', '1995',
'1996', '1997', '1998', '2000', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2012', '2013',
'2014', '2015', '2153', '2600', '3000', '3714', '3785', '3896', '3995', '4001', '4436', '5094', '5346', '8000']
result = []
for i, x in enumerate(a):
previous_e = -9999 if i == 0 else int(a[i - 1])
next_e = -9999 if i == len(a) - 1 else int(a[i + 1])
if int(x) == previous_e + 1 or int(x) == next_e - 1:
result.append(x)
print(result)
['1600','1601','1991','1992','1993','1994','1995','1996','1997','1998','2004','2005 ','2006','2007','2008','2009','2010','2013','2014','2015']
答案 2 :(得分:1)
如果将列表转换为int列表。
mylist = list(map(int, mylist))
然后,您可以应用写成here的答案。但是,如果您使用的是python 3,请不要忘记将其转换为python 3语法。