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))
相关推荐
之歆4 分钟前
DAY_10 JavaScript 深度解析:原型链 · 引用类型 · 内置对象 · 数组方法全攻略(下)
开发语言·前端·javascript·ecmascript
risc1234566 分钟前
python 的字符串前缀
开发语言·python
小程故事多_806 分钟前
Agent Loop 核心突破,上下文压缩四大流派,重新定义窗口资源利用率
java·开发语言·人工智能
如竟没有火炬14 分钟前
字符串相乘——int数组转字符串
开发语言·数据结构·python·算法·leetcode·深度优先
Pkmer17 分钟前
古法编程·新解:Python 类型注解的"一箭三雕"之术
python·ai编程
吃好睡好便好18 分钟前
在Matlab中绘制三维等高线图
开发语言·python·学习·算法·matlab·信息可视化
天若有情67323 分钟前
自制C++万能字符串流式库 formort.h|对标标准库endl,零拷贝链式拼接神器
开发语言·c++
keineahnung234530 分钟前
PyTorch symbolic_shapes 模組的 is_contiguous 從哪來?── sizes_strides_user 安裝與實作解析
人工智能·pytorch·python·深度学习
njsgcs39 分钟前
制作solidworks插件 装配体导出展开耗时分析
开发语言·c#·solidworks
C137的本贾尼39 分钟前
别怕异步:`async` 和 `await` 的简单理解
开发语言·python