迭代和替换槽双阵列(Iteration and replace through double array)
如果之前询问过,先抱歉。
拥有这个数组:
a = [1, None, 2], [3, 4, 5], [6, None, 7]
我需要替换None的值为0.我的第一个实现是通过数组,但我想在一行中完成。
我尝试过这样的,但没有工作:
a = [0 if value is None else value for value in a[i][1] for i in range(len(a))]
显然不工作,那么正确的方法是什么?
编辑:我不想触摸其他位置,只是在arrya中的每个元素的第二个位置。
这个:
a = [None, None, 2], [3, 4, 5], [6, None, 7]
将返回这个:
a = [None, 0, 2], [3, 4, 5], [6, 0, 7]
Having this array:
a = [1, None, 2], [3, 4, 5], [6, None, 7]
I need to replace the values of None for 0. My first implementation is to go trough the array, but I want to do it in one line.
I tried something like this, but didn't work:
a = [0 if value is None else value for value in a[i][1] for i in range(len(a))]
Obviously is not working, what would be the right approach?
Edit: I don't want to touch the other positions, just the second position of every element in the array.
This:
a = [None, None, 2], [3, 4, 5], [6, None, 7]
Will return this:
a = [None, 0, 2], [3, 4, 5], [6, 0, 7]
原文:https://stackoverflow.com/questions/19281500
最满意答案
确保指向SQlite数据库的位置正确。在执行查询之前,请确保表处于打开状态并准备好进行操作。 参考此代码:
sqlite3_stmt *statement; sqlite3 *cPDB; NSString *docsDir; NSArray *dirPaths; dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; // Data base path databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"cPDB.db"]]; const char *dbpath = [databasePath UTF8String]; if (sqlite3_open(dbpath, &cPDB) == SQLITE_OK) { NSString *querySQL = [NSString stringWithFormat: @"UPDATE USER_SESSION_INFO SET \"%@\" = \"%@\", synState = 'false' WHERE sessionName=\"%@\"",categoryTy,state,sessionName]; const char *query_stmt = [querySQL UTF8String]; if (sqlite3_prepare_v2(cPDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) { if((sqlite3_step(statement)) != SQLITE_DONE) NSLog(@"Not Updated"); else { NSLog(@"Succesfully updated row !"); return YES; } } }
Found the problem. There were no single quotes around ?8 in the update statement. Well, its always the small problems. Thanks everyone!
相关问答
更多-
无法在ios中的tableview sqlite3中显示正确的数据(can't display a correct data in tableview sqlite3 in ios)[2023-10-09]
您只创建一个DBPersonDetails对象,然后覆盖其字段并多次将一个对象添加到您的数组中。 为每个DB步骤创建新对象。 Finally my problem is solved. It's a great blogger. http://iphone4workshop.blogspot.in/2013/04/48-sqlite-create-table-and-retrieve.html Thanks -
“我想扩展它,这样如果我尝试输入现有的reg id,它将用新的属性覆盖剩余的属性” 如果这是您要完成的任务,那么您不应该使用INSERT语句。 主键的目的是保护数据库免受具有相同主键的重复行的影响。 当你这样看时,拒绝是恰当的。 你真正想要的是使用INSERT OR REPLACE 如果你是sql和objective c的初学者,我建议我创建一个针对初学者的sqlite包装器: TankDB 编辑: 代码示例: 插入或替换studentDetail(regno,name,department,year)值( ...
-
确保指向SQlite数据库的位置正确。在执行查询之前,请确保表处于打开状态并准备好进行操作。 参考此代码: sqlite3_stmt *statement; sqlite3 *cPDB; NSString *docsDir; NSArray *dirPaths; dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomain ...
-
在iOS中的sqlite3数据库上一次管理多个查询(Managing multiple queries at a time on sqlite3 database in iOS)[2024-03-05]
1)您确定您的方法不需要在主线程上工作吗? 也许某些指令需要它。 2)检测特定的exeption(或EXC_BAD_ACCESS)。 我建议你启用所有断点。 见下图: 3)这里主要用于打开查询和用sqlite关闭数据库的功能。 我使用这样的东西没有任何问题 打开数据库连接的示例函数 - (NSError *) openDatabase { NSError *error = nil; sqlite3* db = self.db; // temp ref const cha ... -
我能够使用节点模块sqlite3加载.db文件,并包含一个javascript函数,如下所示: var sqlite3 = require('sqlite3').verbose(); let dbSrc = 'processlist.db'; var fetchDBData = (tablename) => { var db = new sqlite3.Database(dbSrc); var queries = []; db.each("SELECT * FROM " + tablena ...
-
从UITextfield中给出的输入中搜索iPhone中的SQLite3数据库(Searching SQLite3 DB in iPhone from input given in UITextfield)[2023-08-12]
你想做什么? 您可以将readPlayersFromDatabase方法的参数作为参数发送到UITextfield输入。 这是我选择命令的方法:我有一个名为DBOperations的类。 我希望有所帮助 + (int) getUserID{ //check DB if (![DBOperations checkDB]) { NSString *msgAlert = @"can not access db file."; UIAlertView *alert = [[UIAlertView ... -
在我看来,在SQLite中保存imagePath,并将图像保存在文档目录中。 我也一样。 可能对你有所帮助。 -(void)saveDataToDatabase:(NSString *)fileNameToSave { NSString *docsDir; NSArray *dirPaths; dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dir ...
-
每个客户的SQLite3数据库(SQLite3 database per customer)[2022-03-28]
[我想知道]如果通常的做法是拥有一个专用的SQLite3数据库PER CLIENT 仅当数据库与应用程序一起部署时,例如在电话上。 否则我从来没有听说过这样的事情。 我从来没有使用SQLite,我不知道更新模式和复制所有数据库中的更改的过程是否与其他RDBMS相同 SQLite是一个SQL数据库,它响应ALTER TABLE等。 至于更新所有模式,您必须重新运行所有模式的更新。 模式同步通常由外部实用程序处理,通常您的ORM会有一些东西。 有些是服务器不可知的,有些只支持特定的服务器。 还有专门的数据库变更 ... -
一种方法是在发布时检查表是否有新字段。 您可以使用以下SQLite命令从表中检索所有字段 PRAGMA table_info(table-name); 如果您要查找的字段不在结果集中,请执行ALTER TABLE table-name ADD COLUMN column-name column-type来更新表。 您可以使用此方法遍历表的字段 - (BOOL)getFields { const char *sql = "PRAGMA table_info(myTable)"; sqlite ...
-
如何从IOS中的sqlite3_stmt sqlite3获取长期值?(How get long long value from sqlite3_stmt sqlite3 in IOS?)[2022-11-17]
您可以使用sqlite3_column_int64() 。 You can use sqlite3_column_int64().