[英]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.