python输入输出特殊处理

输出

需要满足输出一行后,再输出一行,行中每个元素用空格隔开

python 复制代码
length = len(tri)
tmp = []
for i in range(len(tri)):
    tmp = tri[i]
    for j in range(len(tri[i])):
        print(tmp[j],end=' ')
    print()

输入p

一次性输入6个数字到列表中,并且输入的每个数据用空格分开

python 复制代码
# 输入6个数字,每个数字用空格分开
numbers = input("请输入6个数字(用空格分隔): ").split()

# 将输入的字符串转换为整数类型的列表
numbers = [int(num) for num in numbers]

# 打印列表
print("输入的数字列表:", numbers)

二维列表

python 复制代码
# 使用循环和输入函数输入二维列表
rows = 3 # 行
cols = 3 # 列
my_list = []

for i in range(rows):
    row = []
    for j in range(cols):
        num = int(input(f"Enter element at position ({i}, {j}): "))
        row.append(num)
    my_list.append(row)
    
# 获得列表每一列的数据
data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
a = list(zip(*data))
>>> [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

ASCII码

python 复制代码
# 输出ASCII码
num = ord('a')

# ASCII码转字符
print(chr(num))

字典

python 复制代码
dict.get(key, default=None)
#返回指定键的值,如果值不在字典中返回default值

dict.items()
#以列表返回可遍历的(键, 值) 元组数组

dict.keys()
#以列表返回一个字典所有的键

dict.values()
#以列表返回字典中的所有值

pop(key[,default])
#删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。

集合

python 复制代码
# 集合的好处就是无序性 即{1,2,3} == {2,1,3}
# 往集合中添加元素
thisset = set(("Google", "Runoob", "Taobao"))
thisset.add("Facebook")

# 删除集合中的某个元素
thisset.remove("Taobao")

字符串

python 复制代码
str.capitalize() # 把字符串第一个字符大写
str.count(str, beg=0, end=len(string)) #返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
str.swapcase() # 翻转str中的大小写
str.upper() # 把小写全部改为大写
str.casefold() # 把大写全部改为小写

查询方法

python 复制代码
# 如果不记得列表/字典..的方法
dir(list)

# 如果不记得这个方法的用法
help(obj.method)
help(list.count)

文件读取

python 复制代码
with open("2020.txt") as fp:
    for a in fp.readlines():
        nums.append(list(a.strip())) #a.strip() 是Python中用于移除字符串两侧空格(包括换行符、制表符等空白字符)的方法

深浅拷贝

python 复制代码
# 浅拷贝
a = [1,2,3]
b = a.copy()
a.append(4)
a
>>>[1,2,3,4]
b
>>>[1,2,3,4]

# 深拷贝
a = [1,2,3]
import copy
b = copy.deepcopy(a)
a.append(4)
a
>>>[1,2,3,4]
b
>>>[1,2,3]

itertools模块

这个模块的本质是一个迭代器

python 复制代码
import itertools

# 双组合迭代器
# 在传递的两个参数里面,前面和后面相组合
for i in itertools.product([1,2,3],[4,5,6]):
    print(i)
ans:
(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)


# 排列数
# 输出给定元素的排列数
for i in itertools.permutations('abc'):
	print(i)
ans:
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
# 还可以有第二个参数
for i in itertools.permutations('abc',2):
    print(i)
ans:
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')


# 组合数
import itertools
for i in itertools.combinations('1234',2):
    print(i)

('1', '2')
('1', '3')
('1', '4')
('2', '3')
('2', '4')
('3', '4')
相关推荐
TF男孩3 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在8 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP10 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805115 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下15 小时前
最终的信号类
开发语言·c++·算法
c8i15 小时前
python中类的基本结构、特殊属性于MRO理解
python
echoarts16 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
liwulin050616 小时前
【ESP32-CAM】HELLO WORLD
python
Aomnitrix16 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
Doris_202316 小时前
Python条件判断语句 if、elif 、else
前端·后端·python