疏锦行Python打卡 DAY 24 元组和OS模块

my_tuple1 = (1, 2, 3)
my_tuple2 = ('a', 'b', 'c')
my_tuple3 = (1, 'hello', 3.14, [4, 5]) # 可以包含不同类型的元素
print(my_tuple1)
print(my_tuple2)
print(my_tuple3)

# 可以省略括号
my_tuple4 = 10, 20, 'thirty' # 逗号是关键
print(my_tuple4)
print(type(my_tuple4)) # 看看它的类型

# 创建空元组
empty_tuple = ()
# 或者使用 tuple() 函数
empty_tuple2 = tuple()
print(empty_tuple)
print(empty_tuple2)

# 元组的索引
my_tuple = ('P', 'y', 't', 'h', 'o', 'n')
print(my_tuple[0])  # 第一个元素
print(my_tuple[2])  # 第三个元素
print(my_tuple[-1]) # 最后一个元素

# 元组的切片
my_tuple = (0, 1, 2, 3, 4, 5)
print(my_tuple[1:4])  # 从索引 1 到 3 (不包括 4)
print(my_tuple[:3])   # 从开头到索引 2
print(my_tuple[3:])   # 从索引 3 到结尾
print(my_tuple[::2])  # 每隔一个元素取一个

# 元组的长度获取
my_tuple = (1, 2, 3)
print(len(my_tuple))



from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score

# 1. 加载数据
iris = load_iris()
X = iris.data
y = iris.target

# 2. 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 3. 构建管道
# 管道按顺序执行以下步骤:
#    - StandardScaler(): 标准化数据(移除均值并缩放到单位方差)
#    - LogisticRegression(): 逻辑回归分类器
pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('logreg', LogisticRegression())
])

# 4. 训练模型
pipeline.fit(X_train, y_train)

# 5. 预测
y_pred = pipeline.predict(X_test)

# 6. 评估模型
accuracy = accuracy_score(y_test, y_pred)
print(f"模型在测试集上的准确率: {accuracy:.2f}")

# 列表 (list)
print("迭代列表:")
my_list = [1, 2, 3, 4, 5]
for item in my_list:
    print(item)

# 元组 (tuple)
print("迭代元组:")
my_tuple = ('a', 'b', 'c')
for item in my_tuple:
    print(item)

# 字符串 (str)
print("迭代字符串:")
my_string = "hello"
for char in my_string:
    print(char)

# range (范围)
print("迭代 range:")
for number in range(5):  # 生成 0, 1, 2, 3, 4
    print(number)

# 集合类型 (Set Types)

# 集合 (set) - 注意集合是无序的,所以每次迭代的顺序可能不同
print("迭代集合:")
my_set = {3, 1, 4, 1, 5, 9}
for item in my_set:
    print(item)

# 字典 (dict) - 默认迭代时返回键 (keys)
print("迭代字典 (默认迭代键):")
my_dict = {'name': 'Alice', 'age': 30, 'city': 'Singapore'}
for key in my_dict:
    print(key)

# 迭代字典的值 (values)
print("迭代字典的值:")
for value in my_dict.values():
    print(value)

# 迭代字典的键值对 (items)
print("迭代字典的键值对:")
for key, value in my_dict.items(): # items方法很好用
    print(f"Key: {key}, Value: {value}")

import os
# os是系统内置模块,无需安装

os.getcwd() # get current working directory 获取当前工作目录的绝对路径

os.listdir() # list directory 获取当前工作目录下的文件列表


#    我们使用 r'' 原始字符串,这样就不需要写双反斜杠 \\,因为\会涉及到转义问题
path_a = r'C:\Users\YourUsername\Documents' # r''这个写法是写给python解释器看,他只会读取引号内的内容,不用在意r的存在会不会影响拼接
path_b = 'MyProjectData'
file = 'results.csv'

# 使用 os.path.join 将它们安全地拼接起来,os.path.join 会自动使用 Windows 的反斜杠 '\' 作为分隔符
file_path = os.path.join(path_a , path_b, file)

file_path

# os.environ 表现得像一个字典,包含所有的环境变量
os.environ

# 使用 .items() 方法可以方便地同时获取变量名(键)和变量值,之前已经提过字典的items()方法,可以取出来键和值
# os.environ是可迭代对象

for variable_name, value in os.environ.items():
  # 直接打印出变量名和对应的值
  print(f"{variable_name}={value}")

# 你也可以选择性地打印总数
print(f"\n--- 总共检测到 {len(os.environ)} 个环境变量 ---")

import os

start_directory = os.getcwd() # 假设这个目录在当前工作目录下

print(f"--- 开始遍历目录: {start_directory} ---")

for dirpath, dirnames, filenames in os.walk(start_directory):
    print(f"  当前访问目录 (dirpath): {dirpath}")
    print(f"  子目录列表 (dirnames): {dirnames}")
    print(f"  文件列表 (filenames): {filenames}")

    # # 你可以在这里对文件进行操作,比如打印完整路径
    # print("    文件完整路径:")
    # for filename in filenames:
    #     full_path = os.path.join(dirpath, filename)
    #     print(f"      - {full_path}")

打卡:@浙大疏锦行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值