简体   繁体   中英

Find elements in a list

I have a file and want to find elements within it. import unittest import json import requests

class Test(unittest.TestCase):


    def test_description(self):
        api_url = 'https://api.myjson.com/bins/mtthu'
        r = requests.get(api_url))

if __name__ == '__main__':
    unittest.main()

Add another line to test_description and use the in keyword:

self.assertTrue(any('young' in c['Description'] for c in charities))

see:

print(('young' in 'abc', 'young' in 'abc young'))

First, you need to fetch a list of all Charities containing 'Make a Wish' in the Description .

haveWish = [c for c in charities if c['Description'] == 'Make a Wish']

Then you can check if all of them have 'young' in Slogan

self.assertTrue(all('young' in c['Slogan'] for c in haveWish))

try this code

self.assertTrue(any((c['Description'] == 'Make a Wish') and ('young' in c['Slogan']) for c in charities))                

using "in" for checking substring check out this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM