小编Abl*_*her的帖子

如何使用 3 个函数调用一个类?

我想创建一个具有 3 个函数的 Python 类。

  • 功能1将要求用户输入一个数字。
  • 功能2将要求用户输入另一个数字。
  • 函数 3 将函数 1 * 函数 2 相乘并返回乘积。

这是我到目前为止的代码:

class Product:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_x(self):
        x = int(input('What is the first number?: ')

    def get_y(self):
        y = int(input('What is the second number?: ')

    def mult_XY(self):
        return x * y  
Run Code Online (Sandbox Code Playgroud)

当我尝试打电话给班级时,我得到了'y' is not defined

我尝试使用以下代码:

num = Product(x, y)
print(num.mult_XY)
Run Code Online (Sandbox Code Playgroud)

python methods class

7
推荐指数
1
解决办法
176
查看次数

如何从web scrape创建pandas数据框?

我想使用这个web scrape创建一个pandas数据框,这样我可以将数据导出到excel.有人熟悉这个吗?我在网上和网站上看到了不同的方法,但是无法通过这种方法成功复制结果.

这是迄今为止的代码:

import requests

source = requests.get("https://api.lineups.com/nba/fetch/lineups/gateway").json()

for team in source['data']:
    print("\n%s players\n" % team['home_route'].capitalize())
    for player in team['home_players']:
        print(player['name'])
    print("\n%s players\n" % team['away_route'].capitalize())
    for player in team['away_players']:
        print(player['name'])
Run Code Online (Sandbox Code Playgroud)

这个网站似乎很有用,但示例不同:

https://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htm

这是stackoverflow.com的另一个例子:

将Web抓取结果加载到Pandas DataFrame中

我是编码/抓取的新手,所以任何帮助都会非常感激.提前感谢您的时间和精力!

python json dataframe web-scraping pandas

2
推荐指数
1
解决办法
901
查看次数

标签 统计

python ×2

class ×1

dataframe ×1

json ×1

methods ×1

pandas ×1

web-scraping ×1