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))
相关推荐
Wang's Blog1 小时前
Java 项目实战: 交友项目 - FastDFS:入门案例
java·开发语言·交友
apcipot_rain2 小时前
C++转Java日记(03)
java·开发语言·c++
布莱克6052 小时前
C++ 中 map 的 insert 与 emplace:key 已存在时会覆盖吗?
开发语言·数据结构·c++·算法·map
xys_6782 小时前
深挖C++:类和对象(中)
开发语言·c++
灿飞屋3 小时前
Rust 编程其实很简单
开发语言·rust
MOONICK5 小时前
QT元数据
开发语言·qt
卷无止境5 小时前
RAGFlow 项目全景报告:从文档解析引擎到 Agent 原生数据库
后端·python
D e re k ZH5 小时前
C++ 系统学习手记:从语法细节到工程实践
开发语言·数据结构·c++·算法
灿飞屋6 小时前
Kotlin与Java虚拟机三:控制流
android·java·开发语言·后端·python·kotlin·安卓
benchmark_cc6 小时前
单只股票五档盘口接入个人研究仓库:QuantDash 读取、校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash