【问题标题】:Concatenate Python lists of different lengths by ID按 ID 连接不同长度的 Python 列表
【发布时间】:2021-05-05 06:08:45
【问题描述】:

我有一个在列表中查找子字符串的脚本,但在最后一个循环中它没有正确连接字符串。 结果正确:

IDS00388680 Block Commands
IDS00389632 empty
IDS00389629 Run queue offset
IDS00389628 empty

脚本结果:

['IDS00388680', 'Verification error', 'Block Commands']
['IDS00389632', 'Run queue offset', 'Team LS']
['IDS00389629', 'empty', 'empty']
['IDS00389628', 'empty', 'empty']

“Step3”循环中的错误。 我需要按编号 IDS 组合结果“数据”、“结果周期”*** 有关如何解决此错误的任何想法?

我的脚本:

from itertools import zip_longest

task_list = [('IDS00388680', 'https://jira.domain/browse/30682\r\n\r\nA call to the block command has been registered: IDS0147121164\r\nerror: [Teradata][ODBC Teradata Driver] will be decided in circulation.')
,('IDS00389632', 'https://jira.domain/browse/30752\r\n\r\nOne-time failure, probably due to the procedure freezing for a second, the problem was fixed by restarting.\r\n incident resolved')
,('IDS00389629', 'https://jira.domain/browse/30753\r\n\r\nThe package did not have time to finish work due to launch at the end of the day.\r\nReconfigured the cluster\r\n incident resolved')
,('IDS00389628', 'https://jira.domain/browse/30754\r\n\r\nReduced resources as a result, there were not enough threads to execute.\r\n incident resolved')]

result_cycle = [['IDS00388680', 'Verification error', 'Block Commands'],
                ['IDS00389629', 'Run queue offset', 'Team LS']]
result = []

#Step3 - concatenate the IDS field in the task_list and the values ​​from the result_cycle
ziplong = zip_longest(task_list, result_cycle, fillvalue='empty')
for i in ziplong:
    if i[1] != "empty":
        result.append([i[0][0], i[1][1], i[1][2]])
    else:
        result.append([i[0][0], "empty", "empty"])

for m in result:
    print(m)

【问题讨论】:

    标签: python-3.x list string-concatenation


    【解决方案1】:

    这是因为您将字符串列表附加到列表中。 试试

    result.append(' '.join([i[0][0], i[1][1], i[1][2]]))
    

    这会创建一个空格连接的字符串并将其附加到您的列表中。

    【讨论】:

    • 它没有解决我在 ziplong 中的问题:如果 i[1] != "empty": result.append(' '.join([i[0][0], i [1][1], i[1][2]])) else: result.append(' '.join([i[0][0], "empty", "empty"]))
    • 您只需要 2 列吗?在这种情况下,删除您不需要的索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多