Python数据的处理

一.字符串拼接的几种方式

  1. 使用str.join()方法进行拼接字符串
  2. 直接拼接
  3. 使用格式化字符串进行拼接
python 复制代码
​
s1='hello'
s2='world'
#(1)使用➕进行拼接
print(s1+s2)
#(2)使用字符串的join()方式
print(''.join([s1,s2]))
print('*'.join([s1,s2]))
print('你好'.join([s1,s2]))
#(3)直接拼接
print('hello''world')
#(4)使用格式化字符串拼接
print('%s%s' % (s1,s2))
print(f'{s1}{s2}')
print('{0}{1}'.format(s1,s2))


​

二.字符串的去重操作

python 复制代码
s='helloworldhelloworldhelloworld'
#(1)字符串的拼接及not in
new_s=''
for item in s:
    if item not in new_s:
        new_s+=item
print(new_s)
#(2)使用索引+not in
new_s2=''
for i in range(len(s)):
    if s[i] not in new_s2:
        new_s2+=s[i]
print(new_s2)
#(3)通过集合去重+列表排序
new_s3=set(s)
lst=list(new_s3)
lst.sort(key=s.index)
print(''.join(lst))
相关推荐
深蓝电商API8 分钟前
初级爬虫反爬应对:解决 403、IP 限制的简单方法
爬虫·python
闲人编程39 分钟前
Python协程的演进:从yield到async/await的完整历史
java·前端·python·async·yield·await·codecapsule
q***92511 小时前
PHP操作redis
开发语言·redis·php
睿思达DBA_WGX1 小时前
使用 Python 的第三方库 xlrd 读取 Excel 文件
python·excel
大佬,救命!!!1 小时前
python实现五子棋
开发语言·python·个人开发·pygame·少儿编程·五子棋
动感小麦兜1 小时前
应用-常用工具部署命令
java·开发语言
立志成为大牛的小牛2 小时前
数据结构——五十一、散列表的基本概念(王道408)
开发语言·数据结构·学习·程序人生·算法·散列表
明知道的博客4 小时前
解决WSL环境下DeepSeek-OCR运行时内存不足问题
python·ocr·deepseek·deepseek-ocr
机灵猫4 小时前
java锁:从 Mark Word 锁升级到 AQS
java·开发语言
FreeCode4 小时前
LangGraph1.0智能体开发:Graph API概念与设计
python·langchain·agent