Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何循环遍历包含变量的字符串列表?_Python_Python 3.x - Fatal编程技术网

Python如何循环遍历包含变量的字符串列表?

Python如何循环遍历包含变量的字符串列表?,python,python-3.x,Python,Python 3.x,我正在处理一些数据,并根据incident\u source创建一个查询列表。当我没有添加需要+符号的变量时,它工作得很好。当我添加时,我得到:TypeError:只能将str(而不是“list”)连接到str。有人知道解决这个问题的方法或更好的解决方案吗 source = 1 offense_id = [122,153,142] incident_source = ['source_1', 'source_2'] num_searches_by_id = [] queries_needed_r

我正在处理一些数据,并根据
incident\u source
创建一个查询列表。当我没有添加需要+符号的变量时,它工作得很好。当我添加时,我得到:
TypeError:只能将str(而不是“list”)连接到str
。有人知道解决这个问题的方法或更好的解决方案吗

source = 1
offense_id = [122,153,142]
incident_source = ['source_1', 'source_2']
num_searches_by_id = []
queries_needed_ran = []

for i,j in zip(incident_source, offense_id):
    if i == 'source_1':
        data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + source + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',
                '''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(source) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
            
    elif i == 'source_2':
        data = ['''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(source) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
    else:
        num_searches_by_id.append(0)


runfile('/Users/thomas.gorman/Documents/Python Connectors/riskiq_passivetotal/untitled20.py', wdir='/Users/thomas.gorman/Documents/Python Connectors/riskiq_passivetotal')
Traceback (most recent call last):

  File "/Users/me/Documents/Python Connectors/untitled20.py", line 427, in <module>
    data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + source + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',

TypeError: can only concatenate str (not "list") to str`

因此,我不确定您的一些变量以及它们来自何处,但我消除了
类型错误:只能通过在
if
语句中为您查找的索引放置括号并更改变量,将str(而不是“list”)连接到str
。也不确定开始时间从哪里来

offense_id = [122,153,142]
incident_source = ['source_1', 'source_2']
num_searches_by_id = []
queries_needed_ran = []

for i,j in zip(incident_source, offense_id):
    if i == 'source_1':
        data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + incident_source[0] + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',
                '''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(incident_source[0]) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
    num_searches_by_id.append(len(data))
    for x in data:
        queries_needed_ran.append(x)
        
    elif i == 'source_2':
        data = ['''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(incident_source[1]) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
    else:
        num_searches_by_id.append(0)

什么是源代码?你的预期产出是什么?你的问题不清楚。实际导致错误的输入是什么?您遇到的错误的完整回溯是什么?您发布的错误位意味着您的输入在错误发生时是一个列表,而不是列表中的字符串。source=1@PacketLoss@G.Anderson我的问题是,我可以循环遍历一个不包含“++”的列表,但是当我添加“++”时,我得到一个类型错误:只能将str(而不是“list”)连接到strAh。。您似乎希望
source
指示列表中指定的
source
。你可以通过
i
来访问它,这是你在循环开始时定义的。我现在完全明白了。因此,在它能够很好地处理没有列表变量的查询之前。现在,它从一个列表中提取数据,我试图以字符串的形式传递该列表。哇!应该将列表的索引用作字符串:)。非常感谢。别担心。我将实际值放在这里供您查看,但是@PacketLoss是正确的,您可以根据您的条件传递
I
。此外,如果您使用的是Qradar的API,那么Qradar应该能够处理SQL构造
offense_id = [122,153,142]
incident_source = ['source_1', 'source_2']
num_searches_by_id = []
queries_needed_ran = []

for i,j in zip(incident_source, offense_id):
    if i == 'source_1':
        data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + incident_source[0] + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',
                '''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(incident_source[0]) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
    num_searches_by_id.append(len(data))
    for x in data:
        queries_needed_ran.append(x)
        
    elif i == 'source_2':
        data = ['''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(incident_source[1]) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
    else:
        num_searches_by_id.append(0)