如何找到一个列表元素

问题描述:

我有这个名单如何找到一个列表元素

['{"activities":[{"activity":"111","interface":"eds","clientIp":"12.207.212.130","logTime":1469811993000},{"activity":"121","dbCount":33,"totalHits":24,"query":"TI', 'the', 'plague","searchedFrom":"Unknown","searchType":"And","logTime":1469811994000}],"session":-2147479722,"customerId":"s8905647","groupId":"main","profileId":"eds"}'] 

,我想这在一个文件中写入这整个名单只有"activity":"121"仅存的"activity":"111"发生后,这个名单。就像在这个例子中,第一个"activity":"111"存在,后来"activity":"121"也存在,我希望这个列表写在文件和任何列表中,其中"activity":"121"不在"activity":"111"之后的任何地方,我不想写。

我该怎么做?请帮忙。

+1

你的意思是这个列表是一个字符串看起来像字典的单元素列表?或者它应该是一个字典清单? – SO44

+0

您应该在字符串上使用'json.loads()'。这将返回一个更简单,更高效的字典。 – IanAuld

我的解决方案是基于您试图通过词典列表进行搜索的假设,因此我已经更正了列表。如果列表中的字典不包含您正在搜索的密钥,则还会出现错误。为此,我为该函数添加了简单的错误处理。

我是一个Python新手,所以可能存在比我更优雅的解决方案,但它可能已经足够满足您的需求。它的工作原理是这样的:如果找到值为'111'的关键'活动'的发生,则利用'值121'搜索关键'活动'的其余部分。够简单。

但是,如果你只考虑条件满足,如果活动121活动111发生后的第二天词典中找到,你可以简单地改变线14本:

if i[key] == valueTwo and foundOne and (dictCount - 1) == countHelp: 

而且,我不确定你是否试图在活动111之后找到第一个找到活动121的词典,或者如果你想写出整个词典列表。变量'myDictionaries'是整个列表,变量'i'只是第一个字典,其中活动121在活动111之后被发现。

您将从第16行写入,在我的解决方案中,归档。所以只需将其更改为您的文件编写解决方案。

# -*- coding: utf-8 -*- 
from __future__ import print_function # You can remove this line if you're using Python 3. 

def searchDictionaries(key, valueOne, valueTwo, myDictionaries): # Define the function with four arguments 
    dictCount = 0 # Initialize the count of dictionaries in the list 
    foundOne = False # Initialize the state for meeting the first condition 
    countHelp = 0 # This will help us determine if the second condition is met in the dictionary right after the first condition was met 
    for i in myDictionaries: # Start looping through the list of dictionaries 
     dictCount = dictCount + 1 # Increase count at every iteration 
     try: 
      if i[key] == valueOne: # Check if the first condition is met (if the value of activity is 111) 
       foundOne = True # Change the state of meeting the first condition to True 
       countHelp = dictCount # Keep this in case you want to modify the next line to only search in the next dictionary 
      if i[key] == valueTwo and foundOne: # Check if the second condition (activity value of 121) is present in any subsequent dictionary 
       # If you made it here, both conditions were met and you can write to file 
       print(myDictionaries) # Write the whole list of dictionaries to file. Use print(i) if you want to just print the first dictionary where you found 121 after 111 was found. 
       break # Stop searching 
     except Exception as e: # Error handling 
      print('Warning: %s - There is no key %s in dictionary %s.' % (e, e, dictCount)) 

    return 

# Your example list of dictionaries 
myListOfDicts = [ 
{'activity': '111', 'interface': 'eds', 'clientIp': '12.207.212.130', 'logTime': 1469811993000}, 
{'session': -2147479722, 'dbCount': 33, 'totalHits': 24, 'query': 'TI', 'the': 'plague', 'searchedFrom': 'Unknown', 'searchType': 'And', 'logTime': 1469811994000}, 
{'activity': '121', 'customerId': 's8905647', 'groupId': 'main', 'profileId': 'eds'} 
] 

# Now you can call the function searchDictionaries with your desired values > key, first value, second value, name of your list of dictionaries 
searchDictionaries('activity', '111', '121', myListOfDicts) 

我希望别人能帮助您与任何后续问题,因为我没有足够的积分,使用注释功能。

作为另一个答案,我添加一个基于假设的解决方案,您的列表是一个字符串的元素,在这种情况下,您最初的发布列表不需要更正。

# -*- coding: utf-8 -*- 

# Your example list 
myListOfDicts = ['{"activities":[{"activity":"111","interface":"eds","clientIp":"12.207.212.130","logTime":1469811993000},{"activity":"121","dbCount":33,"totalHits":24,"query":"TI', 'the', 'plague","searchedFrom":"Unknown","searchType":"And","logTime":1469811994000}],"session":-2147479722,"customerId":"s8905647","groupId":"main","activity":"111"}'] 

sanitizedList = str(myListOfDicts).replace('"', '') # Convert the list to string and emove double-quotes for simpler search 

activityOne = 'activity:111,' # Set the search pattern for string 1 
activityTwo = 'activity:121,' # Set the search pattern for string 2 
foundFirst = False # Initialize status of whether the first string was found 

search111 = sanitizedList.find(activityOne) # Check position of activity 111 
search121 = sanitizedList.find(activityTwo) # Check position of activity 121 

# Set status of foundFirst to True if activity 111 was found 
if search111 > 0: 
    foundFirst = True 

# If activity 111 was found before activity 121, you can print 
if foundFirst and search111 < search121: 
    print 'Now you can write to file' 

我很好奇,你正在试图做的究竟是,因为解决问题的方法很简单。我假设你正在动态创建列表,在这种情况下,你已经知道在活动121之前是否添加了活动111,并且可以基于此来采取行动。

无论如何,我希望这有助于。

+0

这是很好的解决方案,删除双引号是伟大的,并将列表转换为字符串是我没有想到的,仍然是一个初学者..我基本上有一个文件3。500万行和你在myListOfDicts中添加的行只是其中的1行,我不得不遍历那个庞大的文件,并且只在存在活动121的地方获取行,然后是随后的活动115/116,我可以找到线索从你的代码..谢谢一吨 –

+0

我也有另一个难题要解决,现在我有200,000行类似于这里: –