是否有可能在python中添加元组作为字典中的值?
如果是,那么我们如何添加新值呢?我们如何删除和更改它?
一个名字列表:(未分类)例如[保罗,犯规,标记]
另一个带整数的列表:例如 [5,2,6]
第二个列表中的值是每个人(名称)"选择"的数字,因此paul的数字为5,犯规的数字为2,标记的数字为6.
我正在尝试根据降序排列的第二个列表的值对名称列表进行排序.我不能使用地图,因为我需要在我的程序上的其他场合使用这两个列表.
通过排序方法,我得到了这样的列表: [paul,mark,foul]
正如你所看到的,它没有像我想要的那样排序.
在正确的一个是: [大关,保罗犯规]
但我无法找到代码上的错误.
public ArrayList<String> sortNames(ArrayList<Integer> results){
String tmp;
for (int k=0; k<Names.size()-1; k++) {
boolean isSorted=true;
for (int i=1; i<Names.size()-k; i++) {
if (results.get(i)>results.get(i-1) ) {
tmp=Names.get(i);
Names.set(i,Names.get(i-1));
Names.set(i-1,tmp);
isSorted=false;
}
}
if (isSorted) break;
}
return Names;
}
Run Code Online (Sandbox Code Playgroud)
编辑!!!在下面的答案的帮助下,代码是:
public ArrayList<String> sortNames(ArrayList<Integer> results){
String tmp2;
int tmp;
for (int k=0; k<Names.size()-1; k++) {
boolean isSorted=true;
for (int i=1; i<Names.size()-k; i++) {
if (results.get(i)>results.get(i-1) ) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Java中实现"添加"两个数组的元素.我有两个包含整数的数组,我想添加它们.我不想使用不可变变量.我更喜欢这样做:a.plus(b); 问题是当我添加2个不同长度的数组时.它尝试将b的元素添加到a,但如果b的长度更长,则会标记错误"ArrayIndexOutOfBoundsException".我能理解为什么会这样.但我怎么能解决这个问题呢?我怎样才能扩展阵列?:/
public void plus(int[] b)
{
int maxlength = Math.max( this.length, b.length );
if (maxlength==a.length)
{
for (int i = 0; i <= maxlength; i++)
{
a[i] = a[i] + b[i]; //ArrayIndexOutOfBoundsException error
}
}
}
Run Code Online (Sandbox Code Playgroud) 我是编程c的新手,带有数组和文件.我只是试图运行以下代码,但我得到这样的警告:
23 44警告:赋值时将整数指针,未作铸
53错误:'char'之前的预期表达式
有帮助吗?这可能是愚蠢的...但我找不到什么是错的.
#include <stdio.h>
FILE *fp;
FILE *cw;
char filename_game[40],filename_words[40];
int main()
{
while(1)
{
/* Input filenames. */
printf("\n Enter the name of the file \n");
gets(filename_game);
printf("\n Give the name of the file2 \n");
gets(filename_words);
/* Try to open the file with the game */
fp=fopen(/* args omitted */); //line23**
if (fp!= NULL)
{
printf("\n Successful opening %s \n",filename_game);
fclose(fp);
puts("\n Enter x to exit,any other to continue! \n ");
if ( (getc(stdin))=='x') …Run Code Online (Sandbox Code Playgroud) 运行程序时,以下deleteNode函数得到以下结果: *检测到glibcfree():下一个大小无效(正常):0x000000000103dd90**
即使我做'自由(这里); '评论,我收到上述消息.我不认为其他"免费"电话会引发这样的问题.但我不明白为什么这会是错的.:/
struct List *deleteNode(int Code,int i,char* Number)
{
struct List *here;
here=Head;
for (here; here!=Tail; here=here->next)
{
if ( (here->number==Number) && (here->code==Code) )//found node on the List
{
if (here->previous==Head) //delete from beginning
{
Head=here->next;
here->next->previous=Head;
}
else if (here->next==Tail) //delete from the end
{
here->previous->next=Tail;
Tail=here->previous;
}
else //delete from the middle of the list
{
here->previous->next=here->next;
here->next->previous=here->previous;
}
break;
}
}
free (here);
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果我使用并理解valgring然后问题是我的主要功能.我也有一些'免费'但我在此消息之前更改了deleteNode所以我认为问题出在deleteNode函数上.
现在,天下没有免费的()无效的下一个大小....但遗憾的是这样的:glibc的检测*:双重释放或腐败(出):0x00007fff1aae9ae0* :(
主要的一部分:
FILE *File;
if ( …Run Code Online (Sandbox Code Playgroud) 尝试使用链接列表实现哈希表来解决冲突问题我正在面对我的代码初始化哈希表的一些问题.我遇到了分段错误.试着看看问题究竟在哪里我使用了valgrind.使用此工具,我收到警告:
"地址0x8没有堆叠,malloc'd或(最近)免费"
我几乎每次尝试"编辑"哈希表.例如,对于大小,插入,删除等我一次又一次地看了我的代码,但我找不到什么是错的.我以为我有malloc'd并正确堆叠所有内容.但是有了这个消息,显然是错的.有什么想法吗?
我的代码:
//hash table structure
typedef struct HashTable
{
int size; //size of table with connections
struct List **table; //table elements
}HashTable;
typedef struct List
{
char* number;
struct List *next;
}List;
struct HashTable *initHashTable(int size)
{
struct HashTable *blankTable=(struct HashTable *)malloc(sizeof(struct HashTable));
if (size<1)
{
return NULL;
}
if ((blankTable=malloc(sizeof(HashTable)))==NULL)
{
return NULL;
}
if ( (blankTable->table=malloc(size*sizeof(List))) == NULL)
{
return NULL;
}
int i;
for (i=0; i<size; i++) //initializes hash …Run Code Online (Sandbox Code Playgroud) 我们可以在构造函数上使用wait()方法吗?我有一个构造函数方法,其中我调用一些其他初始化方法和gui方法之后.但它接缝是它在第一种方法之前加载gui.因此它会给尚未初始化的对象带来错误.我尝试在gui的调用之前使用wait()但是有一个IllegalMonitorStateException错误,因为它不在同步块中.
试着这样做:
dice = new Dice();
this.generateBoard();
this.generateCells();
this.wait(200,100); //otherwise??
//GUI
board = new GUI(this);
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现连接到数组的双链表.使数组的结构包含列表的Head和Tail指针.
typedef struct myStruct{
int code;
struct myStruct *Head;
struct myStruct *Tail;
}myStruct;
myStruct MyArray[10];
Run Code Online (Sandbox Code Playgroud)
这是我的双链表:
struct myList
{
int data;
struct myList *previous;
struct myList *next;
}head;
struct myList *start =NULL;
Run Code Online (Sandbox Code Playgroud)
在下面的代码中我得到了我在帖子标题上写的警告
警告:从不兼容的指针类型分配
void add_neighbor_to_neighborList(struct neighborList *start,int code,int Data)
{
struct myList *newNode = (struct myList *) malloc (sizeof(struct myList )); /* creates a new node of the correct data size */
newNode->data = Data;
if (start == NULL) { /*WARNING!checks to see if the pointer points somewhere in …Run Code Online (Sandbox Code Playgroud) 我的订单有以下表格:
订单:order_no(密钥),ISBN,数量,类型
ISBN:书籍代码, 数量:客户订购的书籍数量: 类型:购买或退货
我的目标是找到销量最多的书(ISBN).这意味着我必须找到书籍的销售情况,但也要考虑它们的回报.
例如:购买3本ISBN 1010的书籍,购买5本ISBN 2020书籍,返回3本ISBN 2020书籍
畅销书:ISBN:1010
我尝试使用此查询:
"SELECT
ISBN, SUM( quantity ) as sum
FROM
orders
GROUP BY
ISBN
ORDER BY
sum DESC"
Run Code Online (Sandbox Code Playgroud)
但显然这是假的.
有什么好主意吗?
我有一个问题是我得到的警告信息.对于这一行,使用qsort库函数:
qsort(catalog, MAX ,sizeof catalog, struct_cmp_by_amount);
Run Code Online (Sandbox Code Playgroud)
我收到这个警告:
警告:传递'qsort'的参数4使得指针来自整数而没有强制转换
编辑:
struct_cmp_by_amount是程序中的以下函数.(--->)catalog是一个struct,MAX定义为100
但是,对于具有相同代码的另一个程序,具有完全相同的struct_cmp_by_amount函数,我不会得到第4个参数的警告!
编辑:我也必须说,在这两个程序我没有使用功能的原型!但对于第二个程序,它通常与第一个程序相反.
qsort(structs, structs_len, sizeof(struct st_ex), struct_cmp_by_amount);
Run Code Online (Sandbox Code Playgroud)
编辑:
st_ex是一个结构
struct st_ex structs[]={./*elements*/..}
size_t structs_len = sizeof(structs) / sizeof(struct st_ex);
int struct_cmp_by_amount(const void *a, const void *b)
{
struct catalogue *ia = (struct catalogue *)a;
struct catalogue *ib = (struct catalogue *)b;
return (int)(100.f*ia->amount - 100.f*ib->amount);
}
Run Code Online (Sandbox Code Playgroud)
我在徘徊为什么会发生这种情况.你有什么想法吗?
我正在尝试使用字符串(PlayersNames)和imageIcons(PlayersIcons)对ArrayList进行排序,这些值基于我存储在具有整数(结果)的其他arrayList中的值.正如你所看到我得到一个indexOutOfBoundsException,但我不明白为什么.也许早上的收获让我不会看到简单的事情.
ArrayList<String> PlayersNames=new ArrayList<String>;
ArrayList<ImageIcon> PlayersIcons=new ArrayList<ImageIcons>;
public void sortPlayers(ArrayList<Integer> results){
String tmp;
ImageIcon tmp2;
for (int i=0; i<PlayersNames.size(); i++) {
for (int j=PlayersNames.size(); j>i; j--) {
if (results.get(i) < results.get(i+1) ) { //IndexOutOfBoundsException!
tmp=PlayersNames.get(i+1);
PlayersNames.set(i+1,PlayersNames.get(i));
PlayersNames.set(i,tmp);
tmp2=PlayersIcons.get(i+1);
PlayersIcons.set(i+1,PlayersIcons.get(i));
PlayersIcons.set(i,tmp2);
}
}
}
}
Run Code Online (Sandbox Code Playgroud) c ×5
java ×4
arrays ×3
warnings ×3
sorting ×2
arguments ×1
arraylist ×1
callback ×1
chaining ×1
concurrency ×1
constructor ×1
dictionary ×1
file ×1
free ×1
hashtable ×1
linked-list ×1
list ×1
mysql ×1
python ×1
sql ×1
sql-order-by ×1
sum ×1
tuples ×1
wait ×1