我有一个python列表(如下所示),其中包含两种类型的字符串:一种类似于'1556294808 2019-04-26 21:36:48 +0530',另一种类似于'3个文件已更改,126插入(+),18个删除(-)',但这些位置并非严格位于备用位置。
我想通过在类型为1的两个字符串之间插入'0个文件已更改,0个插入(+),0个删除(-)'来在交替位置使用这两种类型的字符串,以防它们位于连续的位置。 元素的顺序不得更改。
['1556294808 2019-04-26 21:36:48 +0530', ' 1 file changed, 1 deletion(-)', '1545318890 2018-12-20 20:44:50 +0530', '1545318858 2018-12-20 20:44:18 +0530', ' 3 files changed, 126 insertions(+), 18 deletions(-)', '1545208408 2018-12-19 14:03:28 +0530', '1545208369 2018-12-19 14:02:49 +0530', ' 2 files changed, 352 insertions(+), 12 deletions(-)', '1545134701 2018-12-18 17:35:01 +0530', ' 3 files changed, 134 insertions(+), 1 deletion(-)', '1545116120 2018-12-18 12:25:20 +0530', '1545115969 2018-12-18 12:22:49 +0530', ' 2 files changed, 25 insertions(+), 17 deletions(-)', '1545114797 2018-12-18 12:03:17 +0530', '1545114768 2018-12-18 12:02:48 +0530', ' 2 files changed, 107 insertions(+), 17 deletions(-)', '1545107389 2018-12-18 09:59:49 +0530', ' 3 files changed, 55 insertions(+), 16 deletions(-)', '1545051306 2018-12-17 18:25:06 +0530', '1545051282 2018-12-17 18:24:42 +0530', ' 3 files changed, 121 insertions(+), 1 deletion(-)', '1545031219 2018-12-17 12:50:19 +0530', '1545031091 2018-12-17 12:48:11 +0530', ' 3 files changed, 327 insertions(+), 6 deletions(-)', '1545027187 2018-12-17 11:43:07 +0530', '1545027168 2018-12-17 11:42:48 +0530', ' 2 files changed, 79 insertions(+), 11 deletions(-)', '1545022804 2018-12-17 10:30:04 +0530', ' 3 files changed, 47 insertions(+), 8 deletions(-)']
答案 0 :(得分:0)
这应该为您解决工作。下面,我遍历整个列表以检查+0530
是否为data = []
for i in range(len(y)):
if "+0530" in y[i]:
data.append(y[i])
if "+0530" in y[i+1]:
data.append("0 files changed, 0 insertions(+)")
else:
data.append(y[i])
,因为它看起来像每个时间戳一样。如果需要,可以编写函数来检查其时间戳,这将更加有效。
data
1556294808 2019-04-26 21:36:48 +0530
1 file changed, 1 deletion(-)
1545318890 2018-12-20 20:44:50 +0530
0 files changed, 0 insertions(+)
1545318858 2018-12-20 20:44:18 +0530
3 files changed, 126 insertions(+), 18 deletions(-)
的输出:
myList = [Qt.Key_A,Qt.Key_B,Qt.Key_C,... Qt.Key_Z]
if event.text() in myList:
print("In My List")
答案 1 :(得分:0)
如果我正确理解了这个问题,则下面的代码应该可以工作,它基本上是遍历列表,并且如果找到两个连续的“ date”字符串,则在两者之间插入一个字符串。该循环向后进行,以避免出现插入新元素的引用问题。
import datetime as dt
items = ['1556294808 2019-04-26 21:36:48 +0530', ' 1 file changed, 1 deletion(-)', '1545318890 2018-12-20 20:44:50 +0530', '1545318858 2018-12-20 20:44:18 +0530', ' 3 files changed, 126 insertions(+), 18 deletions(-)', '1545208408 2018-12-19 14:03:28 +0530', '1545208369 2018-12-19 14:02:49 +0530', ' 2 files changed, 352 insertions(+), 12 deletions(-)', '1545134701 2018-12-18 17:35:01 +0530', ' 3 files changed, 134 insertions(+), 1 deletion(-)', '1545116120 2018-12-18 12:25:20 +0530', '1545115969 2018-12-18 12:22:49 +0530', ' 2 files changed, 25 insertions(+), 17 deletions(-)', '1545114797 2018-12-18 12:03:17 +0530', '1545114768 2018-12-18 12:02:48 +0530', ' 2 files changed, 107 insertions(+), 17 deletions(-)', '1545107389 2018-12-18 09:59:49 +0530', ' 3 files changed, 55 insertions(+), 16 deletions(-)', '1545051306 2018-12-17 18:25:06 +0530', '1545051282 2018-12-17 18:24:42 +0530', ' 3 files changed, 121 insertions(+), 1 deletion(-)', '1545031219 2018-12-17 12:50:19 +0530', '1545031091 2018-12-17 12:48:11 +0530', ' 3 files changed, 327 insertions(+), 6 deletions(-)', '1545027187 2018-12-17 11:43:07 +0530', '1545027168 2018-12-17 11:42:48 +0530', ' 2 files changed, 79 insertions(+), 11 deletions(-)', '1545022804 2018-12-17 10:30:04 +0530', ' 3 files changed, 47 insertions(+), 8 deletions(-)']
new_item = ' 0 files changed, 0 insertions(+), 0 deletions(-)'
fmt = ['%Y %m %d','%H %M %S','%z']
def check_for_date(item, fmt):
try:
date = item.split()
for i in range(3):
dt.datetime.strptime(date[i+1].replace('-',' ').replace(':',' '), fmt[i])
return True
except ValueError as err:
return False
for ii in range(len(items)-1,0,-1):
if (check_for_date(items[ii], fmt)) and (check_for_date(items[ii-1], fmt)):
items.insert(ii, new_item)