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个数字到列表中,并且输入的每个数据用空格分开

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

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

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

二维列表

# 使用循环和输入函数输入二维列表
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码

# 输出ASCII码
num = ord('a')

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

字典

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

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

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

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

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

集合

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

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

字符串

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

查询方法

# 如果不记得列表/字典..的方法
dir(list)

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

文件读取

with open("2020.txt") as fp:
    for a in fp.readlines():
        nums.append(list(a.strip())) #a.strip() 是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模块

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

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')


相关推荐

  1. python输入输出特殊处理

    2024-04-23 13:36:04       40 阅读
  2. 【笔试】输入输出处理

    2024-04-23 13:36:04       47 阅读
  3. Python系列」Python输入输出

    2024-04-23 13:36:04       59 阅读
  4. python笔记(17)输入输出

    2024-04-23 13:36:04       29 阅读
  5. Python输入输出流学习笔记

    2024-04-23 13:36:04       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 13:36:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 13:36:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 13:36:04       82 阅读
  4. Python语言-面向对象

    2024-04-23 13:36:04       91 阅读

热门阅读

  1. 单链表(详解)

    2024-04-23 13:36:04       29 阅读
  2. 腾讯云开通幻兽帕鲁服务器需要多少钱?30元

    2024-04-23 13:36:04       38 阅读
  3. 回归决策树的构建

    2024-04-23 13:36:04       32 阅读
  4. 【Camera Sensor Driver笔记】七、点亮指南之Flash

    2024-04-23 13:36:04       36 阅读
  5. 总结:IP地址、网络地址与子网掩码的理解

    2024-04-23 13:36:04       37 阅读
  6. UE5.1_Subsystem

    2024-04-23 13:36:04       28 阅读
  7. 【前端】node.js常用命令

    2024-04-23 13:36:04       35 阅读
  8. 使用 mysql 命令行访问 ClickHouse 服务器

    2024-04-23 13:36:04       35 阅读
  9. 替换正则表达式c#

    2024-04-23 13:36:04       31 阅读