简体   繁体   English

检查一个列表的元素是否在另一个子列表的元素中

[英]Check if elements of one list are in elements of other sub-list

I am using Faker library.我正在使用Faker库。

from faker import Faker
fake = Faker()

I have a list of sub-lists (2 elements each);我有一个子列表列表(每个 2 个元素); a column name to give the dataframe, and the function invocation itself.给出 dataframe 和 function 调用本身的列名称。


A list of all function names, excluding names of functions that start with a "_":所有 function 名称的列表,不包括以“_”开头的函数名称:

my_list = [[m, 'fake.'+m+'()'] for m in dir(fake) if m[0] != '_']
my_list
>>> [['add_provider', 'fake.add_provider()'],
 ['address', 'fake.address()'], ...

Now, I want to add another condition to the same for loop, excluding function names.现在,我想在同一个 for 循环中添加另一个条件,不包括 function 名称。

Attempted Solution:尝试的解决方案:

exclude = ['add_provider']
fake_cols = [[m, 'fake.'+m+'()'] for m in dir(fake) if function_name in exclude or function_name[:1] == "_"]
fake_cols

Output is empty: Output 为空:

[]

Any solutions that are more concise would be appreciated.任何更简洁的解决方案将不胜感激。

Maybe a little overhaul would do it, look at the following, if that does what you are aiming for:也许稍微大修一下就可以了,看看以下内容,如果这符合您的目标:

import faker

faker = faker.Faker()

exclude = ["add_provider", "address"]
faker_functions ={}
for function_name in dir(faker):
    if function_name in exclude or function_name[:1] == "_":
        continue
    try:
        faker_functions[function_name] =  getattr(faker, function_name)
    except:
        continue

def get_list_of_fake_functions():
    return list(faker_funcktions.keys())

def get_fake(fake_function):
    return faker_functions[fake_function]()

my_list =  [[key, "faker."+ key+ "()"] for key in faker_functions.keys()]
my_list

Error as expected because excluded:错误如预期,因为排除:

get_fake("zipcode_plus4")
KeyError: 'zipcode_plus4'

or result if not或结果,如果不是

get_fake("zipcode_plus4")
Out[38]: '71875-8723'

my_list我的列表

my_list[:10]
Out[57]: 
[['administrative_unit', 'faker.administrative_unit()'],
 ['am_pm', 'faker.am_pm()'], ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Python 中的子列表中获取元素 - Getting elements from a sub-list in Python 如何创建具有 3 个元素的子列表,但是如果子列表只有一个元素,则改为创建两个具有 2 个元素的子列表? - How to create sub-lists with 3 elements, but if a sub-list gets a single element then instead create two with 2 elements? Python 。 如果第二个元素相等,如何对子列表的元素进行排序 按第一个元素对子列表进行排序 - Python . How to sort elements of sub-list if second elements are equal sort the sub-lists by first element 从列表中创建一个子列表,该列表的最小和最大元素之间的正差最小 - make a sub-list from a list which has minimum positive difference between its minimum and maximum elements 如何打印其元素总和与原始列表所有元素总和相同的子列表? - How do I print a sub-list where the sum of its elements is the same as the sum of all the elements of the original list? 如何提取两个不同嵌套列表中相同元素的子列表信息并存储? - How to extract sub-list information of identical elements in two different nested lists and storing them? 将一个列表的元素附加到另一个列表的子列表 - Appending element of one list to sub-list of another list python if/else 语句,如果存在于另一个子列表中,则保留元素 - python if/else statement with exception that keep elements if exist in another sub-list 不同子列表项的可能集合,每个子列表有一个元素 - Possible sets of different sub-list items with one element of each sub-list 检查子列表是否在大列表中的最快方法 - The fastest way to check if the sub-list exists on the large list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM