in Education by
I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact string match in list 1 My two lists are: a = ['rtvg_c1.4.doc', 'vgft_c2.1.pdf', 'D4Q_d1.8.ppt', 'ADk_d3.0.pdf'] b = ['rtvg', 'D4Q'] Output desired:- list3=['rtvg_c1.4.doc', 'D4Q_d1.8.ppt'] The Code that I have used: import csv import os import re metadata = [] with open('D:/meta_demo.csv', 'r') as f: rows = csv.reader(f) for i in rows: metadata.append(i) #print(i) b= metadata[1:20] DIR = 'D:/abc_file/' a = [i for i in os.listdir(r"D:\abc_file")] list3 = [] for i in a: if re.search(r"[^_]*", i) in b: list3.append(a) list3 = [i for i in b if re.search(r"[^_]*", i) in a] Select the correct answer from above options

1 Answer

0 votes
by
There are few ways of doing this problem which I am sharing here: We can simply use string function startwith() to do this problem Syntax for this is: str.startwith(prefix[,start[, end]]) Prefix will be any string or tuple of string to be checked while start and end will be optional a = ['rtvg_c1.4.doc', 'vgft_c2.1.pdf', 'D4Q_d1.8.ppt', 'ADk_d3.0.pdf'] b = ['rtvg', 'D4Q'] result = [i for i in a if i.startswith(tuple(b))] print(result) output:-['rtvg_c1.4.doc', 'D4Q_d1.8.ppt'] Second way is to use regular expression to solve this problem. import re a = ['rtvg_c1.4.doc', 'vgft_c2.1.pdf', 'D4Q_d1.8.ppt', 'ADk_d3.0.pdf'] b =['rtvg', 'D4Q'] pattern = re.compile("|".join(b)) result = [i for i in a if pattern.match(i)] print(result) output:-['rtvg_c1.4.doc', 'D4Q_d1.8.ppt'] We have other approaches for matching as well if we want to find strings that is common in both the list. For this we will use set method which is best to do a = ['rtvg_c1.4.doc', 'vgft_c2.1.pdf', 'D4Q_d1.8.ppt', 'ADk_d3.0.pdf'] b = ['rtvg_c1.4.doc', 'D4Q'] set(a) & set(b) output:-{'rtvg_c1.4.doc'} You can have a look at the following video tutorial to know clear all your doubts:-

Related questions

0 votes
    Can the Python list comprehension syntax be used to create dictionaries? For an example: By iterating over pairs of ... #doesn't work Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I want each item to be sorted by a specific property value from a list of dictionaries I have. Take the ... When sorted by name Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Is there any shortcut to make a simple list out of a complex one in python? I already performed for a loop but ... help me with it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way ... 42, 'food' : 'spam'} Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How can I do the following in Python? array = [0, 10, 20, 40] for (i = array.length() - 1; i >= ... but from the end to the beginning. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters? file. ... be a standard way. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am confused about the usage of string.join(list) instead of list.join(string), for instance see this code: ... reason behind this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can someone get a index (1) in Python for a list ["qwe", "asd", "zxc"] and an item "asd" in the list? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can Someone Please tell me the difference between list methods append() and extend() in Python? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Given a dictionary like so: my_map = { 'a': 1, 'b':2 } How can one invert this map to get: inv_map = { 1: 'a', 2: 'b' } Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I wonder what is better to do: d = {'a': 1, 'b': 2} 'a' in d True or: d = {'a': 1, 'b': 2} d.has_key('a') True Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a list of objects in Python and I want to shuffle them. I thought I could use the random.shuffle method, ... (b) This will fail. Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
...