Hướng dẫn how do you find the common words in two lists in python? - làm thế nào để bạn tìm thấy các từ phổ biến trong hai danh sách trong python?

9

Show

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ự.

Hướng dẫn how do you find the common words in two lists in python? - làm thế nào để bạn tìm thấy các từ phổ biến trong hai danh sách trong python?

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

Hướng dẫn how do you find the common words in two lists in python? - làm thế nào để bạn tìm thấy các từ phổ biến trong hai danh sách trong python?

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

Hướng dẫn how do you find the common words in two lists in python? - làm thế nào để bạn tìm thấy các từ phổ biến trong hai danh sách trong python?

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 them

Phươ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']
7
list3 = ['little','blue']
8
list3 = ['little','blue']
9
list3 = list(set(list1)&set(list2))
0
list3 = list(set(list1)&set(list2))
1

list3 = ['little','blue']
7
list3 = list(set(list1)&set(list2))
3
list3 = ['little','blue']
9
list3 = list(set(list1)&set(list2))
0
list3 = list(set(list1)&set(list2))
6

list3 = ['little','blue']
7
list3 = 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))
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))
1
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))
2

list3 = ['little','blue']
7
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))
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))
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))
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))
1
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))
8
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))
9
>>> list4
['little', 'blue']
0

>>> list4
['little', 'blue']
1
list3 = ['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']
4
list3 = ['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']
5
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']
1
>>> list4
['little', 'blue']
5
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']
3
>>> list4
['little', 'blue']
5
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']
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']
1
list3 = ['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']
4
list3 = ['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']
5
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']
1
>>> list4
['little', 'blue']
5
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']
3
>>> list4
['little', 'blue']
5
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']
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']
7
list3 = ['little','blue']
8
list3 = ['little','blue']
9
list3 = list(set(list1)&set(list2))
0
list3 = list(set(list1)&set(list2))
1

list3 = ['little','blue']
7
list3 = list(set(list1)&set(list2))
3
list3 = ['little','blue']
9
list3 = list(set(list1)&set(list2))
0
list3 = list(set(list1)&set(list2))
6

list3 = ['little','blue']
7
list3 = 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))
0
list3 = ['little','blue']
12
list3 = ['little','blue']
13

list3 = ['little','blue']
7
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))
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))
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))
0
list3 = ['little','blue']
12
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))
8
list3 = ['little','blue']
20
>>> list4
['little', 'blue']
0

>>> list4
['little', 'blue']
1
list3 = ['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']
4
list3 = ['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']
5
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']
1
>>> list4
['little', 'blue']
5
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']
3
>>> list4
['little', 'blue']
5
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']
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))
1
list3 = ['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']
4
list3 = ['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']
5
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']
1
>>> list4
['little', 'blue']
5
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']
3
>>> list4
['little', 'blue']
5
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']
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']
4
list3 = ['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']
5
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']
1
>>> list4
['little', 'blue']
5
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']
3
>>> list4
['little', 'blue']
5
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']
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))
1
list3 = ['little','blue']
49

Output:  

{5}
No common elements

Phươ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']
7
list3 = ['little','blue']
8
list3 = ['little','blue']
9
list3 = list(set(list1)&set(list2))
0
list3 = list(set(list1)&set(list2))
1

list3 = ['little','blue']
7
list3 = list(set(list1)&set(list2))
3
list3 = ['little','blue']
9
list3 = list(set(list1)&set(list2))
0
list3 = list(set(list1)&set(list2))
6

>>> list4
['little', 'blue']
1
list3 = ['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']
4
list3 = ['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']
5
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']
1
>>> list4
['little', 'blue']
5
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']
3
>>> list4
['little', 'blue']
5
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']
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))
1
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))
8
list3 = 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))
1
list3 = ['little','blue']
49

Output:

list3 = ['little','blue']
1

Làm thế nào để bạn tìm thấy những từ phổ biến trong Python?

Khoa học dữ liệu thực tế sử dụng Python..
Chuyển đổi S0 và S1 thành chữ thường ..
S0List: = một danh sách các từ trong S0 ..
S1List: = một danh sách các từ trong S1 ..
Chuyển đổi tập hợp từ các từ trong S0List và S1List, sau đó giao với chúng để có được các từ chung và trả về số lượng kết quả giao lộ ..

Làm cách nào để so sánh nội dung của hai danh sách trong Python?

Phương thức python sort () và == Toán tử để so sánh các danh sách chúng ta có thể câu lạc bộ phương thức python sort () với toán tử == để so sánh hai danh sách. Phương thức python sort () được sử dụng để sắp xếp các danh sách đầu vào với mục đích nếu hai danh sách đầu vào bằng nhau, thì các phần tử sẽ nằm ở cùng một vị trí chỉ mục. to compare lists We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Làm thế nào để bạn tìm thấy các giá trị chung trong Python?

Sử dụng hàm giao lộ () Đây là phương pháp dễ nhất để tìm các yếu tố phổ biến trong hai danh sách trong Python.Như tên cho thấy, hàm giao lộ () là hàm python tích hợp được sử dụng để trả về một tập hợp chứa các phần tử phổ biến trong hai bộ. This is the easiest method to find common elements in two lists in Python. As the name suggests, the intersection() function is a built-in python function that is used to return a set that contains the elements which are common in two sets.

Làm thế nào để bạn kiểm tra xem hai danh sách có cùng một yếu tố trong Python không?

Python: Kiểm tra xem hai danh sách có chứa các yếu tố giống nhau bất kể .....
Sử dụng set () trên kết hợp cả hai danh sách để tìm các giá trị duy nhất ..
Lặp lại chúng với một vòng lặp so sánh số lượng () của mỗi giá trị duy nhất trong mỗi danh sách ..
Trả về sai nếu số lượng không khớp với bất kỳ yếu tố nào, đúng nếu không ..