社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何通过使用python删除重复项从数组中获取唯一的单词集

Nick Code • 3 年前 • 1358 次点击  

下面是我的一系列单词

l = ['Australian Cricket Team',
     'Cricket Team Australian',
     'Won Against England',
     'Against England Team']

['Australian', 'Cricket' ,'Team', 'Won',  'Against', 'England']

我只是想拥有一套独特的词汇。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130704
 
1358 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Henry Ecker Scrimpy
Reply   •   1 楼
Henry Ecker Scrimpy    3 年前

试试这个

def unique(list1):
 
    # initialize a null list
    unique_list = []
     
    # traverse for all elements
    for x in list1:
        # check if exists in unique_list or not
        if x not in unique_list:
            unique_list.append(x)
    # print list
    for x in unique_list:
        print x, 

原帖于 GeekforGeeks https://www.geeksforgeeks.org/python-get-unique-values-list/

mozway
Reply   •   2 楼
mozway    3 年前

可以使用循环:

l = ['Australian Criket Team', 'Cricket Team Australian', 'Won Against England', 'Against England Team']

set(w for s in l for w in s.split())

输出: {'Against', 'Australian', 'Cricket', 'Criket', 'England', 'Team', 'Won'}

或者,如果秩序重要:

list(dict.fromkeys(w for s in l for w in s.split()))

输出: ['Australian', 'Criket', 'Team', 'Cricket', 'Won', 'Against', 'England']

功能变体
from itertools import chain
set(chain.from_iterable(map(str.split, l)))