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')
相关推荐
emplace_back10 分钟前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk16 分钟前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶26 分钟前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl31 分钟前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
收破烂的小熊猫~39 分钟前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
蹦蹦跳跳真可爱5891 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij1 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien1 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
阿蒙Amon1 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#