Python 列表中輸入資料的分類


引言

列表是Python語言中一種資料結構,可以在這些“[]”括號記憶體儲不同資料型別元素。給定的資料根據資料型別(如整數和字串)列出。主要涉及三個函式:過濾、排序和分組。為了根據特定條件(例如字母順序或升序或降序數值)對列表進行排序,我們可以使用sorted()函式,它接受兩個引數:要排序的物件和排序技術的鍵引數。

Python 列表中輸入資料的分類

空列表也可以初始化為包含結果。當列表命名為list_1且空列表表示為:

List_1 =[]

列表可以包含不同資料型別的元素,例如:

List_1 = [“hello”, 1, ‘a’, 78.9]。

列表可以在括號內包含另一個列表以及其他元素,例如:

List_1 = [“hello”,[4,5,6], [‘a’]]

方法

方法1 - 使用isinstance()函式

方法2 - 使用排序函式

方法3 - 使用type()函式

方法1:使用isinstance()和filter()函式對輸入資料進行分類的Python程式

filter()函式用於從給定列表中獲取所需資料型別元素,並使用引數“a”定義lambda函式,並以布林變數形式返回結果。

演算法

  • 步驟1 - 變數定義的值為“book”、23、“note”、56、“pen”和129。

  • 步驟2 - filter()函式通常與lambda函式一起使用,以使用字母或引數“a”檢查資料。

  • 步驟3 - 另一種方法是使用isinstance()和filter()根據元素過濾元素。

  • 步驟4 - 當以下程式碼執行時,它將根據特定資料型別返回輸入。

示例

#initialize the list with strings and integers data type
list_1 = ["book", 23, "note",56, 'pen', 129]

#filtering the strings items in the list and adding them
strings = list(filter(lambda a: isinstance(a, str), list_1))
#finding the number items in the list
num = list(filter(lambda a: isinstance(a, (int, float)), list_1))

#printing the categorized input data
print("Strings:", strings)
print("Numbers:", num)

輸出

Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 129]

方法2:使用isinstance()和排序函式對輸入資料進行分類的Python程式

isinstance()函式初始化為兩個引數,用於檢查列表中是否存在元素,並使用sort()函式按某種順序對元素進行排序。

演算法

  • 步驟1 - 變數定義的值為“book”、23、“note”、56、“pen”和129。

  • 步驟2 - 定義另一個列表資料結構來儲存返回的變數。

  • 步驟3 - 另一種方法是使用isinstance()和sorted()根據元素對元素進行排序。

  • 步驟4 - 當以下程式碼執行時,它將根據特定資料型別返回輸入。

示例

#initialize the list with strings and integers data type
list_1 = ["book", 23, "note",56, 'pen', 129]
#declaring empty lists
strings = []
num = []
#defining the pro and iterating through the list_1
for pro in list_1:
   #finding the strings items in the list and adding them
   if isinstance(pro, str):
      strings.append(pro)
   else:
      #finding the number items in the list and adding them
      num.append(pro)
#using the sorting function
strings_sorted = sorted(strings)
num_sorted = sorted(num)

#printing the categorized input data
print("Strings:", strings_sorted)
print("Numbers:", num_sorted)

輸出

Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 34.5]

方法3:使用type()函式對輸入資料進行分類的Python程式

type()函式用於根據列表中元素的型別對元素進行分類。

演算法

  • 步驟1 - 初始化包含數字和字串元素的列表。

  • 步驟2 - 定義兩個空列表,分別儲存數字和字串元素。

  • 步驟3 - for迴圈可用於迭代列表資料結構。

  • 步驟4 - 另一種方法是使用for迴圈和type()根據元素檢查元素。

  • 步驟5 - 當以下程式碼執行時,它將根據特定資料型別返回輸入。

示例

#initializes the list with strings and integers data types
list_1 = ["book", 23, "note", 56, 'pen', 129]
#empty lists are initialized
strings = []
num = []
#for loop is used to iterate through the list using type() function
for a in list_1:
   if type(a) == str:
      strings.append(a)
   else:
      num.append(a)
#printing the categorized input data
print("Strings:", strings)
print("Numbers:", num)

輸出

Strings: ['book', 'note', 'pen']
Numbers: [23, 56, 129]

結論

Python是一種用途廣泛的高階語言,使用者可以輕鬆理解。本文介紹了三種不同的方法來對Python列表中的輸入資料進行分類。

更新於:2023年8月25日

725 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.