Hướng dẫn how do you print unique words in python? - làm thế nào để bạn in các từ duy nhất trong python?

Tôi có một số mã mà tôi dự định sẽ in ra tất cả các từ duy nhất trong một chuỗi sẽ được nhập bởi người dùng:

str1 = input("Please enter a sentence: ")

print("The words in that sentence are: ", str1.split())

unique = set(str1)
print("Here are the unique words in that sentence: ",unique)

Tôi có thể lấy nó để in ra các chữ cái độc đáo, nhưng không phải là những từ duy nhất.

Hướng dẫn how do you print unique words in python? - làm thế nào để bạn in các từ duy nhất trong python?

Jonrsharpe

Huy hiệu vàng 110K2525 gold badges216 silver badges397 bronze badges

Đã hỏi ngày 2 tháng 11 năm 2016 lúc 21:08Nov 2, 2016 at 21:08

2

String.split(' ') lấy một chuỗi và tạo một danh sách các phần tử chia cho một không gian (' ').

set(foo) lấy một bộ sưu tập foo và trả về một bộ sưu tập set chỉ các yếu tố riêng biệt trong foo.

Những gì bạn muốn là đây: unique_words = set(str1.split(' '))

Giá trị mặc định cho bộ phân tách phân chia là khoảng trắng. Tôi muốn cho thấy rằng bạn có thể cung cấp giá trị của riêng bạn cho phương pháp này.

Đã trả lời ngày 2 tháng 11 năm 2016 lúc 21:16Nov 2, 2016 at 21:16

Hướng dẫn how do you print unique words in python? - làm thế nào để bạn in các từ duy nhất trong python?

3

Ngoài ra, bạn có thể sử dụng:

from collections import Counter

str1 = input("Please enter a sentence: ")
words = str1.split(' ')
c = Counter(words)
unique = [w for w in words if c[w] == 1]

print("Unique words: ", unique)

Đã trả lời ngày 2 tháng 11 năm 2016 lúc 21:34Nov 2, 2016 at 21:34

Hướng dẫn how do you print unique words in python? - làm thế nào để bạn in các từ duy nhất trong python?

UkimikuukimikuUkimiku

6081 Huy hiệu vàng5 Huy hiệu bạc13 Huy hiệu đồng1 gold badge5 silver badges13 bronze badges

Một cách khác để làm điều đó:

user_input = input("Input: ").split(' ')

duplicates = []

for i in user_input:
    if i not in duplicates:
        duplicates.append(i)

print(duplicates)

Đã trả lời ngày 11 tháng 8 lúc 7:28Aug 11 at 7:28

Hướng dẫn how do you print unique words in python? - làm thế nào để bạn in các từ duy nhất trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also

    Bàn luận
    The idea is to use map to keep track of words already occurred. But first, we have to extract all words from a String, as a string may contain many sentences with punctuation marks.
    For extracting words from a String, refer Extracting each word from a String.

    Viết một hàm lấy một chuỗi làm đối số và in tất cả các từ duy nhất trong đó.The idea is to use a Dictionary for calculating the count of each word. But first, we have to extract all words from a String because a string may contain punctuation marks. This is done using regex or regular expression. The word which has count 1 in the dictionary is a unique word.

    Cách tiếp cận: Ý tưởng là sử dụng MAP để theo dõi các từ đã xảy ra. Nhưng trước tiên, chúng ta phải trích xuất tất cả các từ từ một chuỗi, vì một chuỗi có thể chứa nhiều câu có dấu chấm câu. Để trích xuất các từ từ một chuỗi, giới thiệu trích xuất từng từ từ một chuỗi.

    Python: Ý tưởng là sử dụng từ điển để tính toán số lượng của từng từ. Nhưng trước tiên, chúng ta phải trích xuất tất cả các từ từ một chuỗi vì một chuỗi có thể chứa dấu chấm câu. Điều này được thực hiện bằng cách sử dụng regex hoặc biểu thức chính quy. Từ có đếm 1 trong từ điển là một từ duy nhất.

    Java

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    1

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    3

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    5

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    7

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    3

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    9

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    3

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    1
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    2
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    3

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    5

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    0
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    1
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    2

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    5
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    6
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    7

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    3

    String.split(' ')5String.split(' ')6

    String.split(' ')5String.split(' ')8String.split(' ')9

    ' '0' '1' '2

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    3

    String.split(' ')5' '5

    ' '0' '7' '2

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    3

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0set(foo)1

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0set(foo)3

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0set(foo)5

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0String.split(' ')1set(foo)8

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    7
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    8
    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    9

    String.split(' ')5set2

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0String.split(' ')1 String.split(' ')2

    ' '0set9

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0foo1

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4set(foo)1

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    3

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    3

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0unique_words = set(str1.split(' '))2unique_words = set(str1.split(' '))3unique_words = set(str1.split(' '))4

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0unique_words = set(str1.split(' '))6

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4set(foo)1

    set(foo)1

    Python3

    String.split(' ')5String.split(' ')8 set5' '22

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    0
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    5
    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    6 foo8

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    01

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    02
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    03
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    04

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    05
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    06

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4' '5
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    12

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    02
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    24' '2
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    26

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4String.split(' ')8
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    09
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    10
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    022

    Các

    String.split(' ')8

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    28
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    03
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    03
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    31
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    12

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    34
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    03 unique_words = set(str1.split(' '))3

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    49

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    38
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    03
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    40
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    41
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    42

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    44
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    45
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    10
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    47

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    44
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    52
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    10
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    02
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    12

    CácGaurav Ahirwar and Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

    String.split(' ')5

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    65
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    66

    Approach:

    Bài viết này được đóng góp bởi Gaurav Ahirwar và Gaurav Miglani. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. Xin vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. & NBSP;

    Phương pháp 2: Sử dụng Set ()

    Python3

    Theo cách tiếp cận này, chúng tôi lưu trữ chuỗi dưới dạng một tập hợp các từ riêng lẻ và in các từ. Để giải quyết vấn đề này trong phương pháp này, trước tiên chúng ta cần biết về https://www.geeksforgeek.org/sets-in-python/

    Dưới đây là việc thực hiện phương pháp trên:

    Input : Java is great. Grails is also great
    Output : Java
             Grails
             also
    0
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    65
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    76

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    05
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    68

    user_input = input("Input: ").split(' ')
    
    duplicates = []
    
    for i in user_input:
        if i not in duplicates:
            duplicates.append(i)
    
    print(duplicates)
    
    4
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    44
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    71
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    10
    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    73

    from collections import Counter
    
    str1 = input("Please enter a sentence: ")
    words = str1.split(' ')
    c = Counter(words)
    unique = [w for w in words if c[w] == 1]
    
    print("Unique words: ", unique)
    
    88


    Làm thế nào để bạn liệt kê các từ độc đáo trong Python?

    Sử dụng Numpy nhập khẩu của Python, các yếu tố duy nhất trong mảng cũng thu được. Trong bước đầu tiên, hãy chuyển đổi danh sách thành xconvert the list to x=numpy.array(list) and then use numpy.unique(x) function to get the unique values from the list.

    Làm thế nào để bạn tìm thấy các từ độc đáo trong một chuỗi?

    Tìm các từ duy nhất trong chuỗi bằng cách sử dụng Hashset..
    Start..
    Đọc chuỗi đầu vào ..
    Chia chuỗi phân chia với một dấu phân cách, thường là một không gian duy nhất. Điều này trả về một loạt các từ ..
    Tạo một băm với mảng từ. Bây giờ, Hashset chỉ chứa các từ duy nhất ..
    Bạn có thể in các từ duy nhất ..

    Làm thế nào bạn sẽ in một danh sách tất cả các từ duy nhất từ một chuỗi có thể chứa các từ lặp lại?

    Trích xuất các từ từ Chuỗi bằng phương thức Split () và lưu trữ chúng trong một mảng ..
    Lặp lại trên mảng từ sử dụng cho vòng lặp ..
    Sử dụng một vòng lặp khác để tìm sự xuất hiện của từ hiện tại mảng ..
    Nếu lần xuất hiện thứ hai được tìm thấy số lượng tăng và đặt từ thành từ.
    Nếu số lượng của từ hiện tại bằng một bản in ..

    Làm thế nào để bạn đếm số lượng từ duy nhất trong một chuỗi python?

    Để đếm số lượng từ duy nhất trong một chuỗi: sử dụng str.phương thức chia () để chia chuỗi thành một danh sách các từ.Sử dụng lớp SET () để chuyển đổi danh sách thành một tập hợp.Sử dụng hàm Len () để có được số lượng các từ duy nhất trong chuỗi.Use the len() function to get the count of unique words in the string.