python删除类方法,Python类别-Set&删除方法?

First I'd like to mention that I am completely new to Python and I've found it a bit difficult to transition from C++. I apologize if my question comes off as elementary.

I have a class for 'songs' which I have initialized as following. It takes in data from a file that contains a song's ID, name, genre etc. all separated by ::.

def __init__(self):

self.song_names = dict()

self.song_genres = dict()

def load_songs(self, song_id):

f = open(song_id)

for line in f:

line = line.rstrip()

component = line.split("::")

sid = components[0]

same= components[1]

sgenre=components[2]

self.song_names[mid] = sname

self.song_genres[mid] = sgenre

f.close()

The program also takes in data from a file with 'users' information, separated as

UserID::Gender::Age::Occupation::Zip etc. and a file with 'ratings'.

I would I implement a function like def set_song(sid, list((title,genres)))

and something like delete_song(sid) ?

I'm going to have to wind up doing a ton more other functions, but if someone could help me with those two - at least to have a better idea of structure and syntax - handling the others should be easier.

解决方案

Why not just inherit from dict and use its interface? That way you can use Python's standard mapping operations instead of rolling your own:

class Songs(dict):

def load(self, song_id):

with open(song_id, 'r') as f:

for line in f:

sid, name, genre = line.rstrip().split('::')[:3]

self[sid] = [name, genre]

mysongs = Songs()

mysongs.load('barnes_and_barnes__fish_heads')

mysongs['barnes_and_barnes__fish_heads'] = ['roly poly', 'strange'] # set

del mysongs['barnes_and_barnes__fish_heads'] # delete

好的,您可以使用Python的matplotlib库来实现互交式折线图。具体实现步骤如下: 1. 导入必要的库 ```python import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons ``` 2. 创建画布和子图 ```python fig, ax = plt.subplots() plt.subplots_adjust(left=0.1, bottom=0.25) ``` 3. 定义折线图的x轴和y轴初始值 ```python x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25] ``` 4. 绘制折线图 ```python line, = ax.plot(x, y, lw=2) ``` 5. 定义滑动条的位置和大小 ```python axcolor = 'lightgoldenrodyellow' axfreq = plt.axes([0.1, 0.1, 0.8, 0.03], facecolor=axcolor) axamp = plt.axes([0.1, 0.15, 0.8, 0.03], facecolor=axcolor) ``` 6. 定义滑动条的取值范围和初始值 ```python sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=15) samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=1) ``` 7. 定义滑动条的响应函数 ```python def update(val): freq = sfreq.val amp = samp.val line.set_ydata(amp*np.sin(2*np.pi*freq*t)) fig.canvas.draw_idle() sfreq.on_changed(update) samp.on_changed(update) ``` 8. 显示图形界面 ```python plt.show() ``` 完整代码如下: ```python import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons import numpy as np fig, ax = plt.subplots() plt.subplots_adjust(left=0.1, bottom=0.25) x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25] line, = ax.plot(x, y, lw=2) axcolor = 'lightgoldenrodyellow' axfreq = plt.axes([0.1, 0.1, 0.8, 0.03], facecolor=axcolor) axamp = plt.axes([0.1, 0.15, 0.8, 0.03], facecolor=axcolor) sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=15) samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=1) def update(val): freq = sfreq.val amp = samp.val line.set_ydata(amp*np.sin(2*np.pi*freq*t)) fig.canvas.draw_idle() sfreq.on_changed(update) samp.on_changed(update) plt.show() ``` 这个代码中,我们定义了一个sin曲线,其中x轴的值为[1,2,3,4,5],y轴的值为[1, 4, 9, 16, 25],您可以根据自己的需求修改这些值。滑动条可以调节sin曲线的振幅和频率,您也可以根据需要修改滑动条的取值范围和初始值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值