我试图找到使用Python将列表中的每个元素与列表中的每个元素进行匹配的最有效方法。例如输入:
>>>myList = [['hi', 'no', 'bye', 'Global', 24],['morning', 'X', 'place'],['so', 'large', 'mall','test'], ['hi', 'X', 'place', 'bye']]
>>>check_against_myLIst = ['bye','place','hi','australia']
现在我不确定最好的方法是使用map函数,循环或其他任何python数据分析方法。
需要将输出转换为数据框,例如Output。
Index Value Result
0 ['hi', 'no', 'bye', 'Global', 24] True
1 ['morning', 'X', 'place'] True
2 ['so', 'large', 'mall','test'] False
3 ['hi', 'X', 'place', 'bye'] True
谢谢!
答案 0 :(得分:0)
您可以只将结果存储在列表中,然后从中构建数据框
result = [False]*len(myList)
for n, _list in enumerate(myList):
if [i for i in _list if i in check_against_myLIst]:
result[n] = True
答案 1 :(得分:0)
您可以创建一个功能来测试列表集和比较列表之间的交集。
给出
5/2/2018
,5/21/2018
,01/29/2018
代码
to_date(INDICATOR_DATE,'mm/dd/yyyy')
import pandas as pd
cmp = ["bye","place","hi","australia"]
lst = [
["hi", "no", "bye", "Global", 24],
["morning", "X", "place"],
["so", "large", "mall","test"],
["hi", "X", "place", "bye"]
]
输出
def is_in(nested, compare):
"""Return a tuple of (row, bool), True if the compared list intersects."""
compared = set(compare)
return [(x, bool(set(x) & compared)) for x in nested]
它看起来类似于您的输出。从这里,我们只需要制作一个DataFrame:
bool_lst = is_in(lst, cmp)
bool_lst
输出
后者可以全部简化为一行:
[(['hi', 'no', 'bye', 'Global', 24], True),
(['morning', 'X', 'place'], True),
(['so', 'large', 'mall', 'test'], False),
(['hi', 'X', 'place', 'bye'], True)]
答案 2 :(得分:0)
首先,从ALTER TABLE other_name.derived_table
创建集合,以进行O(1)成员资格测试。
myList
使用有效的>>> myList = [['hi', 'no', 'bye', 'Global', 24],['morning', 'X', 'place'],['so', 'large', 'mall','test'], ['hi', 'X', 'place', 'bye']]
>>> checks = ['bye','place','hi','australia'] # renamed from check_against_myLIst
>>> sets = map(set, myList)
检查以找出any
的任何元素是否在给定集中。 (与计算交集相反,checks
是惰性的。)
any
构建数据框。
>>> result = [(lst, any(s in set_ for s in check)) for lst, set_ in zip(myList, sets)]
答案 3 :(得分:0)
这里有一个示例,说明无需使用熊猫即可获取它的方法。 无论如何,让我解释另一种观点:
# list
my_list = (['hi', 'no', 'bye', 'Global', 24],['morning', 'X', 'place'],['so', 'large', 'mall','test','TESSTTTT'], ['hi', 'X', 'place', 'bye'])
# check list
check_against_myLIst = ('bye','place','hi','australia')
# Function to find intersection of two arrays
def interSection(index, arr1,arr2):
result = 'false'
output = list(filter(lambda x: x in arr1, arr2))
if output:
result = 'true'
print 'index',"\t"*1,'Value',"\t"*6,'Result'
print index,"\t"*1,arr1,"\t"*4,result
print ''
# Driver program
if __name__ == "__main__":
count = 0
for arrayItem in my_list:
interSection(count, arrayItem,check_against_myLIst)
count += 1