9
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi đang cố gắng tìm một cách dễ dàng để làm điều này:
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table']Tôi muốn có được các yếu tố phổ biến của hai danh sách, với thứ tự của List1 không bị ảnh hưởng, vì vậy kết quả này được dự kiến.
list3 = ['little','blue']tôi đang dùng
list3 = list(set(list1)&set(list2))Tuy nhiên, điều này chỉ trả về list3 = ['little','blue'] 2, rõ ràng, set () chỉ cần bỏ qua thứ tự.
user202729
2.9403 Huy hiệu vàng20 Huy hiệu bạc32 Huy hiệu Đồng3 gold badges20 silver badges32 bronze badges
Hỏi ngày 16 tháng 8 năm 2013 lúc 1:33Aug 16, 2013 at 1:33
Bạn gần như ở đó, chỉ cần sắp xếp list3 = ['little','blue'] 3 theo list3 = ['little','blue'] 4
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k))Result:
>>> list4 ['little', 'blue']Đã trả lời ngày 16 tháng 8 năm 2013 lúc 1:48Aug 16, 2013 at 1:48
AkavallakavallAkavall
78.8K48 Huy hiệu vàng199 Huy hiệu bạc244 Huy hiệu Đồng48 gold badges199 silver badges244 bronze badges
1
Sử dụng danh sách hiểu:
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue']Đã trả lời ngày 16 tháng 8 năm 2013 lúc 1:35Aug 16, 2013 at 1:35
Falsetrufalsetrufalsetru
345K58 Huy hiệu vàng688 Huy hiệu bạc610 Huy hiệu Đồng58 gold badges688 silver badges610 bronze badges
4
Điều này thực hiện những gì bạn yêu cầu sử dụng Python 2.7, không đặc biệt thanh lịch nhưng nó trả lời câu hỏi của bạn.
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue']Đã trả lời ngày 16 tháng 8 năm 2013 lúc 1:40Aug 16, 2013 at 1:40
Đây là triển khai bằng bộ lọc:
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] set2 = set(list2) f = lambda x:x in set2 list3 = filter(f, list1)Đã trả lời ngày 16 tháng 8 năm 2013 lúc 1:35Aug 16, 2013 at 1:35
FalsetrufalsetruBrionius
345K58 Huy hiệu vàng688 Huy hiệu bạc610 Huy hiệu Đồng3 gold badges36 silver badges48 bronze badges
1
Cho hai danh sách, in tất cả các yếu tố phổ biến của hai danh sách. & NBSP; & nbsp;
Examples:
Input : list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 9] Output : {5} Explanation: The common element of the lists is 5. Input : list1 = [1, 2, 3, 4, 5] list2 = [6, 7, 8, 9] Output : No common elements Explanation: They do not have any elements in common in between themPhương pháp 1: Sử dụng Set & Thuộc tính
Chuyển đổi danh sách thành các bộ và sau đó in SET1 & SET2. SET1 & SET2 Trả về các phần tử phổ biến được đặt, trong đó SET1 là List1 và Set2 là List2. & NBSP; bên dưới là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2.
Below is the Python3 implementation of the above approach:
Python3
list3 = ['little','blue'] 5 list3 = ['little','blue'] 6
list3 = ['little','blue'] 7list3 = ['little','blue'] 8list3 = ['little','blue'] 9 list3 = list(set(list1)&set(list2)) 0list3 = list(set(list1)&set(list2)) 1
list3 = ['little','blue'] 7list3 = list(set(list1)&set(list2)) 3list3 = ['little','blue'] 9 list3 = list(set(list1)&set(list2)) 0list3 = list(set(list1)&set(list2)) 6
list3 = ['little','blue'] 7list3 = list(set(list1)&set(list2)) 8 list3 = list(set(list1)&set(list2)) 9
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 0list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 1list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 2
list3 = ['little','blue'] 7list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 4list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 5
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 0list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 1list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 8list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 9>>> list4 ['little', 'blue'] 0
>>> list4 ['little', 'blue'] 1list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list4 ['little', 'blue'] 4>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 6>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 8>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 0__
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 2>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5____53
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 7
>>> list4 ['little', 'blue'] 1list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list4 ['little', 'blue'] 4>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 6>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 8>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 0__
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 2>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5____53
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 7
Output:
{5} No common elements>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 3
Phương pháp 2: Sử dụng thuộc tính của Set Set Intersection
Below is the Python3 implementation of the above approach:
Python3
Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi. Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không. Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ. & Nbsp; bên dưới là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
list3 = ['little','blue'] 7list3 = ['little','blue'] 8list3 = ['little','blue'] 9 list3 = list(set(list1)&set(list2)) 0list3 = list(set(list1)&set(list2)) 1
list3 = ['little','blue'] 7list3 = list(set(list1)&set(list2)) 3list3 = ['little','blue'] 9 list3 = list(set(list1)&set(list2)) 0list3 = list(set(list1)&set(list2)) 6
list3 = ['little','blue'] 7list3 = list(set(list1)&set(list2)) 8 list3 = list(set(list1)&set(list2)) 9
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 0list3 = ['little','blue'] 12list3 = ['little','blue'] 13
list3 = ['little','blue'] 7list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 4list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 5
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 0list3 = ['little','blue'] 12list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 8list3 = ['little','blue'] 20>>> list4 ['little', 'blue'] 0
>>> list4 ['little', 'blue'] 1list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list4 ['little', 'blue'] 4>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 6>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 8>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 0__
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 2>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5____53
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 1list3 = ['little','blue'] 49
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 3
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9>>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 3
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 1list3 = ['little','blue'] 49
Output:
{5} No common elementsPhương pháp 2: Sử dụng thuộc tính của Set Set IntersectionUsing for loop
Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi. Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không. Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ. & Nbsp; bên dưới là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
list3 = ['little','blue'] 5 list3 = ['little','blue'] 6
list3 = ['little','blue'] 7list3 = ['little','blue'] 8list3 = ['little','blue'] 9 list3 = list(set(list1)&set(list2)) 0list3 = list(set(list1)&set(list2)) 1
list3 = ['little','blue'] 7list3 = list(set(list1)&set(list2)) 3list3 = ['little','blue'] 9 list3 = list(set(list1)&set(list2)) 0list3 = list(set(list1)&set(list2)) 6
>>> list4 ['little', 'blue'] 1list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list4 ['little', 'blue'] 4>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 6>>> list4 ['little', 'blue'] 5>>> list4 ['little', 'blue'] 8>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 0__
>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 4list3 = ['little','blue'] 9 >>> list4 ['little', 'blue'] 3>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 2>>> list4 ['little', 'blue'] 5>>> list1 = ['little','blue','widget'] >>> list2 = ['there','is','a','little','blue','cup','on','the','table'] >>> s = set(list2) >>> list3 = [x for x in list1 if x in s] >>> list3 ['little', 'blue'] 9>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 1>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 3>>> list4 ['little', 'blue'] 5list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = [] for l1 in list1: for l2 in list2: if l2 == l1: list3.append(l2) print list3 # ['little', 'blue'] 5____53
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 1list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 8list3 = list(set(list1)&set(list2)) 21>>> list4 ['little', 'blue'] 0
list1 = ['little','blue','widget'] list2 = ['there','is','a','little','blue','cup','on','the','table'] list3 = set(list1)&set(list2) # we don't need to list3 to actually be a list list4 = sorted(list3, key = lambda k : list1.index(k)) 1list3 = ['little','blue'] 49
Output:
list3 = ['little','blue'] 1