python输入多组数据_Python3算法类多组数据输入输出格式

在 Python3 中舍弃了 Python2 中的 raw_input() 的输入方式,读入的数据全部是字符串类型,需要使用 int() 强转即可,

input()读入一行数据,strip() 去除两端空格, split() 默认按照空格分割

map方式的原理:

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed,function must

take that many arguments and is applied to the items from all iterables

in parallel. If one iterable is shorter than another it is assumed to

be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

map的第一个参数可以是 function.

第一种(有多组输入数据,但是没有告诉你有多少组):# 1)、

while True:

try:

a, b = input().strip().split()

print(int(a) + int(b))

except :

break

# 2)、

# 是另外一种读入方式,下面都会按照这种方式

while True:

try:

a, b = map(int, input().strip().split())

print(a+ b)

except :

break

第二种(输入一个整数,告诉我们接下来有多少组数据)t = int(input().strip())

for i in range(t):

a, b = map(int, input().strip().split())

print(a+ b)

第三种(有多组输入数据,没有告诉你有多少组,但是题目告诉你遇见什么结束)while True:

a, b = map(int, input().strip().split())

if a == 0 and b == 0:

break

print(a+ b)

第四种(有多组输入数据,并告诉我们有多少组和遇见什么结束)t = int(input().strip())

for i in range(t):

a, b = map(int, input().strip().split())

if a == 0 and b == 0:

break

print(a+ b)

有什么问题,请在下面进行留言

待续....

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是代码实现: 首先,我们需要导入所需的库:sklearn、numpy、pandas。 ```python from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression import numpy as np import pandas as pd ``` 接着,我们需要加载鸢尾花数据集。这里使用`load_iris()`函数加载数据集,并将其存储在`iris`变量中。 ```python iris = load_iris() ``` 数据集有四个特征(萼片长度、萼片宽度、花瓣长度、花瓣宽度)和三类鸢尾花(山鸢尾、变色鸢尾、维吉尼亚鸢尾),我们可以通过以下代码来看一下数据集的信息。 ```python print(iris.DESCR) ``` 接下来,我们需要将数据集拆分成特征和对应的标签。我们将使用`iris.data`和`iris.target`属性来获取特征和标签。 ```python X = iris.data y = iris.target ``` 我们可以通过以下代码来查看我们的特征和标签的形状。 ```python print('Shape of features:', X.shape) print('Shape of labels:', y.shape) ``` 接下来,我们需要创建逻辑回归模型并拟合我们的数据。我们可以使用`LogisticRegression()`函数来创建一个逻辑回归模型,然后使用`fit()`函数将其拟合到我们的数据上。 ```python model = LogisticRegression() model.fit(X, y) ``` 现在我们已经拟合了模型,我们可以使用它来对新的数据进行分类。我们可以手动创建一些数据并将其传递给我们的模型,以查看它如何对其进行分类。 ```python # 创建一些新数据 new_data = np.array([5.2, 3.1, 4.5, 1.5]).reshape(1, -1) # 使用拟合的模型进行分类 prediction = model.predict(new_data) # 输出分类结果 print('Prediction:', prediction) ``` 这里我们手动创建了一个新的数据点,即萼片长度为5.2,萼片宽度为3.1,花瓣长度为4.5,花瓣宽度为1.5的数据。我们将其传递给我们的模型,并使用`predict()`函数分类它。最后,我们输出了模型预测的类别。 完整代码如下: ```python from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression import numpy as np import pandas as pd # 加载数据集 iris = load_iris() # 获取特征和标签 X = iris.data y = iris.target # 创建逻辑回归模型并拟合数据 model = LogisticRegression() model.fit(X, y) # 创建新数据并进行分类 new_data = np.array([5.2, 3.1, 4.5, 1.5]).reshape(1, -1) prediction = model.predict(new_data) # 输出分类结果 print('Prediction:', prediction) ``` 输出结果为: ``` Prediction: [1] ``` 这里,我们的模型将新数据点分类为“变色鸢尾”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值