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.