最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)

 更新時(shí)間:2023年11月23日 11:32:30   作者:蟲(chóng)無(wú)涯  
關(guān)于數(shù)據(jù)去重,咱們這里簡(jiǎn)單理解下,就是刪除掉重復(fù)的數(shù)據(jù),應(yīng)用的場(chǎng)景比如某些產(chǎn)品產(chǎn)生的大數(shù)據(jù),有很多重復(fù)的數(shù)據(jù),為了不影響分析結(jié)果,我們可能需要對(duì)這些數(shù)據(jù)進(jìn)行去重,所以本文給大家總結(jié)了Python中列表和字符串常用的數(shù)據(jù)去重方法,需要的朋友可以參考下

1 關(guān)于數(shù)據(jù)去重

  • 關(guān)于數(shù)據(jù)去重,咱們這里簡(jiǎn)單理解下,就是刪除掉重復(fù)的數(shù)據(jù);
  • 應(yīng)用的場(chǎng)景比如某些產(chǎn)品產(chǎn)生的大數(shù)據(jù),有很多重復(fù)的數(shù)據(jù),為了不影響分析結(jié)果,我們可能需要對(duì)這些數(shù)據(jù)進(jìn)行去重,刪除重復(fù)的數(shù)據(jù),提高分析效率等等。

2 字符串去重

2.1 for方法

  • 基本思路是for循環(huán)先遍歷字符串;
  • 遍歷的字符要是沒(méi)在結(jié)果字符串中,就添加到結(jié)果字符串即可。
  • 代碼如下:
import unittest

class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.char_date = "12344312abcdcbdaABCDDCBA張王李張"
        print(f"原始字符串為:{cls.char_date}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    def test_char_for(self):
        char_date01 = ""
        for data in self.char_date:
            if data not in char_date01:
                char_date01 += data
        print(f"for方法去重后數(shù)據(jù):{char_date01}")
        
if __name__ == "__main__":
    unittest.main()
  • 結(jié)果輸出為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
for方法去重后數(shù)據(jù):1234abcdABCD張王李

2.2 while方法

  • 思路和for差不多;
  • 這里主要是通過(guò)通過(guò)索引的方式查找;
  • 代碼如下:
import unittest

class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.char_date = "12344312abcdcbdaABCDDCBA張王李張"
        print(f"原始字符串為:{cls.char_date}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    def test_char_while(self):
        char_date02 = ""
        flag = len(self.char_date) - 1
        while True:
            if flag >= 0:
                if self.char_date[flag] not in char_date02:
                    char_date02 += self.char_date[flag]
                flag -= 1
            else:
                break
        print(f"while方法去重后數(shù)據(jù):{char_date02}")

if __name__ == "__main__":
    unittest.main()
  • 輸出結(jié)果為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
while方法去重后數(shù)據(jù):張李王ABCDadbc2134

2.3 列表方法

  • 我們先把字符串轉(zhuǎn)為集合去重;
  • 再將集合轉(zhuǎn)為列表;
  • 將列表轉(zhuǎn)為字符串,最后排序進(jìn)行輸出即可;
  • 部分代碼如下,其他關(guān)于類(lèi)的內(nèi)容和以上一樣:
    def test_char_list(self):
        char_date03 = set(self.char_date)
        char_date04 = list(char_date03)
        char_date04.sort(key=self.char_date.index)
        print(f"列表方法去重后數(shù)據(jù):{''.join(char_date04)}")
  • 輸出后為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
列表方法去重后數(shù)據(jù):1234abcdABCD張王李

2.4 直接刪除法

  • 這個(gè)主要是直接對(duì)原字符串直接操作;
  • 通過(guò)下標(biāo)以及字符串切片方法實(shí)現(xiàn);
  • 部分代碼如下:
    def test_char_delete(self):
        for data in self.char_date:
            if self.char_date[0] in self.char_date[1:len(self.char_date)]:
                self.char_date = self.char_date[1:len(self.char_date)]
            else:
                self.char_date = self.char_date[1:len(self.char_date)] + self.char_date[0]
        print(f"直接刪除方法去重后數(shù)據(jù):{''.join(self.char_date)}")
  • 輸出為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
直接刪除方法去重后數(shù)據(jù):4312cbdaDCBA王李張

2.5 fromkeys方法

  • 直接使用fromkeys()方法,它的作用是從序列鍵和值設(shè)置為value來(lái)創(chuàng)建一個(gè)新的字典;
  • 部分代碼如下:
    def test_char_fromkeys(self):
        char_date05 = {}
        char_date06 = char_date05.fromkeys(self.char_date)
        list_char = list(char_date06.keys())
        print(f"fromkeys方法去重后數(shù)據(jù):{''.join(list_char)}")
  • 輸出為:

原始字符串為:12344312abcdcbdaABCDDCBA張王李張
fromkeys方法去重后數(shù)據(jù):1234abcdABCD張王李

3 列表去重

3.1 for方法

  • 循環(huán)遍歷列表后添加到新的列表即可;
  • 這個(gè)方法不會(huì)改變?cè)瓉?lái)的順序;
  • 代碼如下:
class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.list_data = ["A", "B", "C", "D", "E", "C", "A", "B"]
        print(f"原始列表為:{cls.list_data}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass
        
    def test_list_for(self):
        list_data01 = []
        for data in self.list_data:
            if data not in list_data01:
                list_data01.append(data)
        print(f"for方法:{list_data01} ")

if __name__ == "__main__":
    unittest.main()
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
for方法:['A', 'B', 'C', 'D', 'E'] 

3.2 set方法1

  • 直接使用set方法后轉(zhuǎn)為列表即可;
  • 這個(gè)方法會(huì)改變?cè)瓉?lái)的順序;
  • 部分代碼如下:
 def test_list_set(self):
        list_data02 = list(set(self.list_data))
        print(f"set方法1:{list_data02}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
set方法1:['D', 'C', 'B', 'E', 'A']

3.3 set方法2

  • 直接使用set方法后轉(zhuǎn)為列表;
  • 這個(gè)方法會(huì)改變?cè)瓉?lái)的順序,可進(jìn)行排序;
  • 部分代碼:
 def test_list_set01(self):
        list_data03 = list(set(self.list_data))
        list_data03.sort(key=self.list_data.index)
        print(f"set方法2:{list_data03}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
set方法2:['A', 'B', 'C', 'D', 'E']

3.4 count方法

  • 先對(duì)原序列進(jìn)行排序;
  • 循環(huán)遍歷列表后使用count()方法;
  • 部分代碼:
    def test_list_count(self):
        self.list_data.sort()
        for data in self.list_data:
            while self.list_data.count(data) > 1:
                del self.list_data[self.list_data.index(data)]
        print(f"count方法:{self.list_data}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
count方法:['A', 'B', 'C', 'D', 'E']

3.5 轉(zhuǎn)字典法

  • 直接把列表轉(zhuǎn)為字典方法即可;
  • 部分代碼:
 def test_list_dict(self):
        list_data04 = {}
        list_data05 = list_data04.fromkeys(self.list_data).keys()
        list_data06 = list(list_data05)
        print(f"字典法:{list_data06}")
  • 輸出為:

原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']
字典法:['A', 'B', 'C', 'D', 'E']

4 完整代碼

  • 以下為列表和字符串常用的數(shù)據(jù)去重方法的完整代碼;
  • 使用unittest中的TestCase類(lèi)組織測(cè)試用例;
  • 代碼如下:
# -*- coding:utf-8 -*-
# 作者:蟲(chóng)無(wú)涯
# 日期:2023/11/22 
# 文件名稱(chēng):test_deduplication.py
# 作用:字符串和列表去重
# 聯(lián)系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


import unittest


class TestDeduplication(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.char_date = "12344312abcdcbdaABCDDCBA張王李張"
        cls.list_data = ["A", "B", "C", "D", "E", "C", "A", "B"]
        print(f"原始字符串為:{cls.char_date}")
        print(f"原始列表為:{cls.list_data}")

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    def test_char_for(self):
        char_date01 = ""
        for data in self.char_date:
            if data not in char_date01:
                char_date01 += data
        print(f"for方法去重后數(shù)據(jù):{char_date01}")

    def test_char_while(self):
        char_date02 = ""
        flag = len(self.char_date) - 1
        while True:
            if flag >= 0:
                if self.char_date[flag] not in char_date02:
                    char_date02 += self.char_date[flag]
                flag -= 1
            else:
                break
        print(f"while方法去重后數(shù)據(jù):{char_date02}")

    def test_char_list(self):
        char_date03 = set(self.char_date)
        char_date04 = list(char_date03)
        char_date04.sort(key=self.char_date.index)
        print(f"列表方法去重后數(shù)據(jù):{''.join(char_date04)}")

    def test_char_delete(self):
        for data in self.char_date:
            if self.char_date[0] in self.char_date[1:len(self.char_date)]:
                self.char_date = self.char_date[1:len(self.char_date)]
            else:
                self.char_date = self.char_date[1:len(self.char_date)] + self.char_date[0]
        print(f"直接刪除方法去重后數(shù)據(jù):{''.join(self.char_date)}")

    def test_char_fromkeys(self):
        char_date05 = {}
        char_date06 = char_date05.fromkeys(self.char_date)
        list_char = list(char_date06.keys())
        print(f"fromkeys方法去重后數(shù)據(jù):{''.join(list_char)}")

    print("===============================================")

    def test_list_for(self):
        list_data01 = []
        for data in self.list_data:
            if data not in list_data01:
                list_data01.append(data)
        print(f"for方法:{list_data01} ")

    def test_list_set(self):
        list_data02 = list(set(self.list_data))
        print(f"set方法1:{list_data02}")

    def test_list_set01(self):
        list_data03 = list(set(self.list_data))
        list_data03.sort(key=self.list_data.index)
        print(f"set方法2:{list_data03}")

    def test_list_count(self):
        self.list_data.sort()
        for data in self.list_data:
            while self.list_data.count(data) > 1:
                del self.list_data[self.list_data.index(data)]
        print(f"count方法:{self.list_data}")

    def test_list_dict(self):
        list_data04 = {}
        list_data05 = list_data04.fromkeys(self.list_data).keys()
        list_data06 = list(list_data05)
        print(f"字典法:{list_data06}")
        
        
if __name__ == "__main__":
    unittest.main()
  • 全部輸出為:

===============================================
原始字符串為:12344312abcdcbdaABCDDCBA張王李張
原始列表為:['A', 'B', 'C', 'D', 'E', 'C', 'A', 'B']

直接刪除方法去重后數(shù)據(jù):4312cbdaDCBA王李張
for方法去重后數(shù)據(jù):1234abcdABCD張王李
fromkeys方法去重后數(shù)據(jù):1234abcdABCD張王李
列表方法去重后數(shù)據(jù):1234abcdABCD張王李
while方法去重后數(shù)據(jù):張李王ABCDadbc2134
count方法:['A', 'B', 'C', 'D', 'E']
字典法:['A', 'B', 'C', 'D', 'E']
for方法:['A', 'B', 'C', 'D', 'E'] 
set方法1:['B', 'A', 'D', 'C', 'E']
set方法2:['A', 'B', 'C', 'D', 'E']

放一張圖吧(雖然用處不大,哈哈):

以上就是Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Python列表和字符串?dāng)?shù)據(jù)去重的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python爬蟲(chóng)指南之xpath實(shí)例解析(附實(shí)戰(zhàn))

    python爬蟲(chóng)指南之xpath實(shí)例解析(附實(shí)戰(zhàn))

    在進(jìn)行網(wǎng)頁(yè)抓取的時(shí)候,分析定位html節(jié)點(diǎn)是獲取抓取信息的關(guān)鍵,目前我用的是lxml模塊,下面這篇文章主要給大家介紹了關(guān)于python爬蟲(chóng)指南之xpath實(shí)例解析的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • 淺析PyTorch中nn.Module的使用

    淺析PyTorch中nn.Module的使用

    這篇文章主要介紹了淺析PyTorch中nn.Module的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python中順序表原理與實(shí)現(xiàn)方法詳解

    Python中順序表原理與實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Python中順序表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python順序表的概念、原理及增刪查等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • 深入淺析Python 函數(shù)注解與匿名函數(shù)

    深入淺析Python 函數(shù)注解與匿名函數(shù)

    這篇文章主要介紹了Python 函數(shù)注解與匿名函數(shù)的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • pygame游戲之旅 添加游戲暫停功能

    pygame游戲之旅 添加游戲暫停功能

    這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第13篇, 教大家如何添加游戲暫停功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 一篇文章教你掌握python數(shù)據(jù)類(lèi)型的底層實(shí)現(xiàn)

    一篇文章教你掌握python數(shù)據(jù)類(lèi)型的底層實(shí)現(xiàn)

    這篇文章主要介紹了Python 數(shù)據(jù)類(lèi)型的底層實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-09-09
  • Python自動(dòng)化辦公之Excel和Word文件自動(dòng)整理與歸檔

    Python自動(dòng)化辦公之Excel和Word文件自動(dòng)整理與歸檔

    在數(shù)字化浪潮席卷全球的今天,"效率"已經(jīng)成為職場(chǎng)核心競(jìng)爭(zhēng)力的代名詞,本文將深度解析 Python 自動(dòng)化辦公的原理,并提供真實(shí)可用的代碼案例,帶你開(kāi)啟高效辦公的新世界
    2025-12-12
  • python 對(duì)key為時(shí)間的dict排序方法

    python 對(duì)key為時(shí)間的dict排序方法

    今天小編就為大家分享一篇python 對(duì)key為時(shí)間的dict排序方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python中單線程、多線程和多進(jìn)程的效率對(duì)比實(shí)驗(yàn)實(shí)例

    Python中單線程、多線程和多進(jìn)程的效率對(duì)比實(shí)驗(yàn)實(shí)例

    這篇文章主要介紹了Python單線程多線程和多進(jìn)程效率對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python結(jié)合Tkinter手搓一個(gè)寄存器計(jì)算器

    Python結(jié)合Tkinter手搓一個(gè)寄存器計(jì)算器

    寄存器計(jì)算器是一款專(zhuān)業(yè)的32位寄存器計(jì)算工具,專(zhuān)為程序員和硬件工程師設(shè)計(jì),提供直觀的位操作和進(jìn)制轉(zhuǎn)換功能,下面我們就來(lái)看看如何使用Python和Tkinter手搓一個(gè)寄存器計(jì)算器吧
    2026-01-01

最新評(píng)論

新余市| 浦东新区| 观塘区| 开原市| 临高县| 通渭县| 和龙市| 噶尔县| 隆安县| 平和县| 长汀县| 新疆| 阳高县| 秦皇岛市| 南木林县| 香格里拉县| 托克托县| 古田县| 沈阳市| 安塞县| 运城市| 潍坊市| 南郑县| 昌邑市| 宾川县| 绥棱县| 东港市| 东光县| 云阳县| 上蔡县| 东乌珠穆沁旗| 汽车| 鄂托克旗| 施甸县| 玉屏| 桂阳县| 永泰县| 麻江县| 曲周县| 平远县| 闻喜县|