Python 中元组字符串中的常用词
当需要查找元组字符串中的常见词时,可以使用“连接”方法、“set”方法、“&”运算符和“split”方法。
“join”方法可以根据特定值连接多个值,
Python 具有称为“set”的数据类型。此“set”仅包含唯一的元素。该 set 可用于执行诸如交集、差集、并集和对称差集之类的运算。
“split”函数根据需要拆分的 value 拆分给定的数据为多个部分。
“&”运算符执行乘法,即 AND 运算。
以下是相同的演示 −
示例
my_tuple_1 = ('Hi there', 'Hi Will,', 'Hi ', 'Hi there')
print("The tuple is : ")
print(my_tuple_1)
my_result = ", ".join(sorted(set(my_tuple_1[0].split()) & set(my_tuple_1[1].split()) &
set(my_tuple_1[2].split())))
print("Common words among the tuples are : ")
print(my_result)输出
The tuple is :
('Hi there', 'Hi Will,', 'Hi ', 'Hi there')
Common words among the tuples are :
Hi说明
- 定义元组并显示在控制台上。
- 使用“split”方法根据空格拆分元组。
- 然后,分别使用“sorted”和“join”方法对其进行排序和连接。
- 将其分配给一个值。
- 显示在控制台上。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP