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