網站首頁 編程語言 正文
1. 字符串反轉
字符串反轉有很多方法,咱們再這里介紹兩種:一種是切片,一種是python字符串的reversed方法。
# -!- coding: utf-8 -!- string = 'hello world' # 方法1 new_str = string[::-1] ic(new_str) # 方法二 new_str2 = ''.join(reversed(string)) ic(new_str2) ''' ic| new_str: 'dlrow olleh' ic| new_str2: 'dlrow olleh' '''
2. 首字母大寫
這里咱們也是介紹兩種方法,區別之處在于**capitalize()**
僅是首字母大寫
**title()**是每個單詞開頭的首字母都大寫
# 首字母大寫 string = 'hello python and world' # 方法一 new_str = string.capitalize() ic(new_str) # 方法二 new_str2 = string.title() ic(new_str2) ''' ic| new_str: 'Hello python and world' ic| new_str2: 'Hello Python And World' '''
3. 查詢唯一元素
我們利用set的唯一性來確定字符串的唯一元素:
string = 'hellohellohello' new_str = set(string) # set類型 ic(new_str) # 字符串類型 new_str = ''.join(new_str) ic(new_str) ''' ic| new_str: {'l', 'o', 'h', 'e'} ic| new_str: 'lohe' '''
4. 變量交換
python
中的變量交換比java簡單多了,交換兩個變量無需定義第三個中間變量,直接交換即可實現
a = 'hello' b = 'world' ic(a+b) # 直接交換兩個變量 a, b = b, a ic(a+b) ''' ic| a+b: 'helloworld' ic| a+b: 'worldhello' '''
5. 列表排序
列表排序這里我們也提供兩種方式。第一個是列表自帶的**sort()
方法;第二個是python內置函數sorted()**
方法
score = [88, 99, 91, 85, 94, 85, 94, 78, 100, 80] # 方法一 new_score = sorted(score) ic('默認升序:', new_score) score = [57, 29, 11, 27, 84, 34, 87, 25, 70, 60] # 方法二 new_score2 = sorted(score, reverse=True) ic('設置降序', new_score2) ''' ic| '默認升序:', new_score: [78, 80, 85, 85, 88, 91, 94, 94, 99, 100] ic| '設置降序', new_score2: [87, 84, 70, 60, 57, 34, 29, 27, 25, 11] '''
6.列表推導式
使用列表推導式可以快速生成一個列表或者根據列表生成滿足需求的列表
# 生成10個10-100以內隨機整數 numbers = [random.randint(10, 100) for x in range(10)] ic(numbers) # 輸入5折后的價格 price = [800, 500, 400, 860, 780, 520, 560] half_price = [(x*0.5)for x in price] ic(half_price) ''' ic| numbers: [64, 22, 80, 70, 34, 81, 74, 35, 85, 12] ic| half_price: [400.0, 250.0, 200.0, 430.0, 390.0, 260.0, 280.0] '''
7. 合并字符串
合并字符串我們使用string
的.join()
方法實現
lists = ['hello', 'world', 'python', 'java', 'c++'] # 合并字符串 new_str = ' '.join(lists) ic(new_str) ''' ic| new_str: 'hello world python java c++' '''
8. 拆分字符串
拆分字符串我們使用string的split()
方法實現
string = 'hello world python java c++' string2 = 'hello|world|python|java|c++' # 拆分字符串 new_str = string.split(' ') ic(new_str) new_str2 = string2.split('|') ic(new_str2) ''' ic| new_str: ['hello', 'world', 'python', 'java', 'c++'] ic| new_str2: ['hello', 'world', 'python', 'java', 'c++'] '''
9. 回文串檢測
回文串是指aba
、abba
、cccbccc
、aaaa
這種左右對稱的字符串。我們可以根據之前提到的切片來檢測這種特殊的字符串序列
str = '20211202' if str == str[::-1]: ? ? print('yes') else: ? ? print('no') ''' yes '''
10. 統計列表元素出現次數
統計列表中元素各自出現的次數我們使用collections
的Counter
方法
from collections import Counter lists = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd'] # 統計所有元素出現的次數 counts = Counter(lists) ic(counts) # 統計某一元素出現的次數 ic(counts['d']) # 統計出現最多次數的一個元素 ic(counts.most_common(1)) ''' ic| counts: Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1}) ic| counts['d']: 5 ic| counts.most_common(1): [('d', 5)] '''
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/121755149
相關推薦
- 2023-03-05 Go語言學習之golang-jwt/jwt的教程分享_Golang
- 2022-08-28 go?zero微服務實戰處理每秒上萬次的下單請求_Golang
- 2024-01-29 Linux信號量以及基于環形隊列的生產者消費者模型
- 2022-06-07 Selenium瀏覽器自動化如何上傳文件_python
- 2023-04-20 ES6:字符串的擴展及新增方法
- 2022-10-26 Pytorch中torch.stack()函數的深入解析_python
- 2022-09-16 selenium.chrome寫擴展攔截或轉發請求功能_C#教程
- 2023-04-01 react組件實例屬性state詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支