I am trying to be able to return a boolean when part of a string is present from a list of strings, so for example here a data set:
"issues": [
{
"id": "1",
"fields": {
"labels": [
"customer:asdf",
"hello"
]
}
},
{
"id": "2",
"fields": {
"labels": [
"icanthearyou",
"hellotoyou"
]
}
},
"id": "3",
"fields": {
"labels": [
"customer:num2",
"hellotoyou"
]
}
}
]
i am currently grabbing data from this set like so:
def grab_data(self, data):
created_values = list((item['id'],
# key
True if "customer:" is in item['fields']['labels'] else false
#fromcustomer boolean
) for item in data['issues'])
But obviously the above line True if "customer:" is in item['fields']['labels'] else false
only works if one of the labels is only "customer:"
So to be clear how would I grab data in this format i have above list((item['labels']) for item in data['issues']
to see that one of the labels
has this string "customer:" within any entry and return true...
id 1 and 3 should return True and id 2 return False.
So expected value should be [True, False, True]
答案 0 :(得分:1)
I changed your original list a little bit to make it a valid Python list variable, but one way to solve this is to join
all of your labels together into one string, then search that string for "customer:".
def filter_issues(issues):
return [ "customer:" in ' '.join(d["fields"]["labels"]) for d in issues]
issues = [{"id": "1", "fields": {"labels": ["customer:asdf", "hello"]}},
{"id": "2", "fields": {"labels": ["icanthearyou", "hellotoyou"]}},
{"id": "3", "fields": {"labels": ["customer:num2","hellotoyou"]}} ]
print(filter_issues(issues))
# Prints: [True, False, True]
The reason I used a space in the join instead of just an empty string, is because otherwise something like ["abccust", "omer:foo"]
would pass.