说我有这样的字典:
my_dict = {2:3, 5:6, 8:9}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以切换键和值来获得:
{3:2, 6:5, 9:8}
Run Code Online (Sandbox Code Playgroud) 我目前有一个叫做Polynomial的类,初始化看起来像这样:
def __init__(self, *termpairs):
self.termdict = dict(termpairs)
Run Code Online (Sandbox Code Playgroud)
我通过使键成为指数并且相关的值是系数来创建多项式.要创建此类的实例,请输入如下内容:
d1 = Polynomial((5,1), (3,-4), (2,10))
Run Code Online (Sandbox Code Playgroud)
这使得字典如此:
{2: 10, 3: -4, 5: 1}
Run Code Online (Sandbox Code Playgroud)
现在,我想创建一个名为Quadratic的Polynomial类的子类.我想在Quadratic类构造函数中调用Polynomial类构造函数,但是我不太清楚如何做到这一点.我试过的是:
class Quadratic(Polynomial):
def __init__(self, quadratic, linear, constant):
Polynomial.__init__(self, quadratic[2], linear[1], constant[0])
Run Code Online (Sandbox Code Playgroud)
但我得到错误,任何人都有任何提示?当我调用Polynomial类构造函数时,我觉得我使用了不正确的参数.
可能重复:
在C中替换字符串的功能是什么?
我试图用多个字符替换我的字符串中的某个字符.这是我想要做的一个例子.
说我有字符串"aaabaa"
我想用5"c"替换所有出现的字符"b".
所以,当我完成后,"aaabaa"变成"aaacccccaa"
我写了以下代码:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[20] = "aaabaa";
int i, j;
for (i=0; s[i]!= '\0'; i++)
{
if (s[i] == 'b')
{
for (j=0; j<5; j++)
{
s[i+j] = 'c';
}
}
}
printf("%s\n", s);
}
Run Code Online (Sandbox Code Playgroud)
我从这个函数输出的是"aaaccccc".它似乎只是用c来覆盖最后两个a.我有没有办法让这些最后几个不被覆盖?
我有以下程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
typedef struct {int key; char data[MAXLEN];} record;
main(int argc, char *argv[])
{
int n, i;
record x;
FILE *fp;
fp = fopen(argv[1], "w+");
printf("How many records will be entered? \n");
scanf("%d", &n);
for (i=0; i<n; i++)
{
printf("Enter record: \n");
scanf("%d", &x.key);
scanf("%s", &x.data);
fwrite(&x, sizeof(record), 1, fp);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在做的是从用户输入创建记录,然后将这些"记录"存储到文件中.但是,当我使用fwrite()时,创建的文件中会写入许多奇怪的字符,而不是简单地让每条记录都包含其键和数据.有人能告诉我为什么写这些奇怪的人物吗?
我在python中创建了两个列表,如下所示:
list1 = [2, 3, 3, 4, 4, 5]
list2 = [-4, 8, -4, 8, -1, 2]
Run Code Online (Sandbox Code Playgroud)
现在我将这两个列表压缩成一个字典,如下所示:
d = dict(zip(list1, list2))
Run Code Online (Sandbox Code Playgroud)
这给了我:
{2: -4, 3: -4, 4: -1, 5: 2}
Run Code Online (Sandbox Code Playgroud)
我想得到的是这样的结果:
{2: -4, 3: 4, 4: 7, 5: 2}
Run Code Online (Sandbox Code Playgroud)
list1正在成为新词典的关键.如果我在list1中有两个相同的值,我希望它添加这两个值.例如,在list2中,8和-4都具有相同的键3.有没有办法将这两个值一起添加,所以键看起来像
{3: 4}
Run Code Online (Sandbox Code Playgroud) 所以我刚刚开始使用 openGL,我在尝试弄清楚如何删除已经绘制的对象时遇到了麻烦。我有 2 个不同的矩形
glBegin(GL_POLYGON)
glColor3f(0.5, 0.5, 0.5)
glVertex2f(0, 0)
glVertex2f(0, 10)
glVertex2f(10, 10)
glVertex2f(10, 0)
glEnd()
glBegin(GL_POLYGON)
glColor3f(0.5, 0.5, 0.5)
glVertex2f(30, 30)
glVertex2f(30, 40)
glVertex2f(40, 40)
glVertex2f(40, 30)
glEnd()
Run Code Online (Sandbox Code Playgroud)
使用鼠标,我希望能够用鼠标右键单击其中一个矩形并将其删除。我可以调用某种函数来执行此操作吗?
我有一个像这样的交换函数:
void swap(int i, int j, void* arr[])
{
void *temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Run Code Online (Sandbox Code Playgroud)
我在主要调用swap这样:
main()
{
int arr[8] = {4,7,9,2,6,7,8,1};
void *ptr = arr;
swap(0, 1, ptr);
int k;
for (k=0; k<8; k++)
printf("%d ", arr[k]);
}
Run Code Online (Sandbox Code Playgroud)
现在,交换似乎工作正常,但不是将1值与另一个值交换,而是将另外2个值交换为2个值.例如,当我交换(0,1,ptr)时,我得到了数组
9,2,4,7,6,7,8,1
Run Code Online (Sandbox Code Playgroud)
当我应该得到:
7,4,9,2,6,7,8,1
Run Code Online (Sandbox Code Playgroud)
它不是交换4和7,而是用9,2交换4,7.它为什么这样做?