Python:IndexError:列表索引超出范围错误(Python: IndexError: list index out of range Error)
更新,看看底部!
我卡住了! 我得到一个IndexError:列表索引超出范围错误。
def makeInverseIndex(strlist): numStrList = list(enumerate(strlist)) n = 0 m = 0 dictionary = {} while (n < len(strList)-1): while (m < len(strlist)-1): if numStrList[n][1].split()[m] not in dictionary: dictionary[numStrList[n][1].split()[m]] = {numStrList[n][0]} m = m+1 elif {numStrList[n][0]} not in dictionary[numStrList[n][1].split()[m]]: dictionary[numStrList[n][1].split()[m]]|{numStrList[n][0]} m = m+1 n = n+1 return dictionary
它给了我这个错误
>>> makeInverseIndex(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "./inverse_index_lab.py", line 23, in makeInverseIndex if numStrList[n][1].split()[m] not in dictionary: IndexError: list index out of range
我不明白......是什么原因造成的? 即使我改变了while循环的条件,它也会发生。 我没有得到什么问题。 我在这方面很陌生,所以如果一块西兰花问你这个问题就像解释一样。
编辑:
谢谢大家,我忘了提到输入的例子,我想输入这样的东西:
L=['A B C', 'B C E', 'A E', 'C D A']
并将此作为输出:
D={'A':{0,2,3}, 'B':{0,1}, 'C':{0,3}, 'D':{3}, 'E':{1,2}}
因此,要创建一个字典,显示列表中的位置,例如,您可能会找到“A”。 它应该有一个巨大的列表。 有人有任何提示吗? 我希望它迭代并挑选出每个字母,然后为它们分配一个字典值。
编辑第二个:
感谢你们的帮助,我的代码看起来很漂亮:
def makeInverseIndex(strList): numStrList = list(enumerate(strList)) n = 0 dictionary = {} while (n < len(strList)): for word in numStrList[n][1].split(): if word not in dictionary: dictionary[word] = {numStrList[n][0]} elif {numStrList[n][0]} not in dictionary[word]: dictionary[word]|={numStrList[n][0]} n = n+1 return dictionary
但是当我尝试运行模块时,我仍然设法得到此错误:
>>> makeInverseIndex(L) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "./inverse_index_lab.py", line 21, in makeInverseIndex for word in numStrList[n][1].split(): NameError: global name 'StrList' is not defined
我没有看到错误的来源。
Updated, look bottom!
I am stuck! I get a IndexError: list index out of range Error.
def makeInverseIndex(strlist): numStrList = list(enumerate(strlist)) n = 0 m = 0 dictionary = {} while (n < len(strList)-1): while (m < len(strlist)-1): if numStrList[n][1].split()[m] not in dictionary: dictionary[numStrList[n][1].split()[m]] = {numStrList[n][0]} m = m+1 elif {numStrList[n][0]} not in dictionary[numStrList[n][1].split()[m]]: dictionary[numStrList[n][1].split()[m]]|{numStrList[n][0]} m = m+1 n = n+1 return dictionary
it gives me this error
>>> makeInverseIndex(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "./inverse_index_lab.py", line 23, in makeInverseIndex if numStrList[n][1].split()[m] not in dictionary: IndexError: list index out of range
I don't get it... what causes this? It happens even when I change the conditions of the while loop. I don't get what is the problem. I am pretty new at this, so explain it like you would if a piece of broccoli asked you this question.
Edit:
Thanks guys, I forgot to mention examples of input, I want to input something like this:
L=['A B C', 'B C E', 'A E', 'C D A']
and get this as output:
D={'A':{0,2,3}, 'B':{0,1}, 'C':{0,3}, 'D':{3}, 'E':{1,2}}
so to create a dictionary that shows where in the list you might find a 'A' for example. It should work with a huge list. Do anyone have any tips? I want it to iterate and pick out each letter and then assign them a dictionary value.
Edit number two:
Thanks to great help from you guys my code looks beautiful like this:
def makeInverseIndex(strList): numStrList = list(enumerate(strList)) n = 0 dictionary = {} while (n < len(strList)): for word in numStrList[n][1].split(): if word not in dictionary: dictionary[word] = {numStrList[n][0]} elif {numStrList[n][0]} not in dictionary[word]: dictionary[word]|={numStrList[n][0]} n = n+1 return dictionary
But I still manage to get this error when I try to run the module:
>>> makeInverseIndex(L) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "./inverse_index_lab.py", line 21, in makeInverseIndex for word in numStrList[n][1].split(): NameError: global name 'StrList' is not defined
I do not see where the error can come from.
原文:
最满意答案
简单的JOIN查询: -
SELECT Analyze.Name, Data.Standard FROM Analyze INNER JOIN Data ON Data.id_analyze = Analyze.id_analyze ORDER BY Analyze.Name, Data.Standard
请注意,您可以加入如何完成它们,但更新的方法是显式使用INNER JOIN / LEFT OUTER JOIN /等。
然后你可以绕过这一次的结果
Simple JOIN query:-
SELECT Analyze.Name, Data.Standard FROM Analyze INNER JOIN Data ON Data.id_analyze = Analyze.id_analyze ORDER BY Analyze.Name, Data.Standard
Note that you can do joins how you have done them but the newer way is explicitly using INNER JOIN / LEFT OUTER JOIN / etc.
You can then just loop around the results of this once
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
用于将PHP MySQL应用程序部署到多个客户的重复表(Duplicate tables for deploying a PHP MySQL app to multiple customers)[2023-09-11]
这个评论太长了。 假设您具有适当的索引和表分区,SQL旨在在具有数百万行的表上执行良好。 在这种情况下,我不担心数据量是一个问题。 但是,您可能遇到安全问题。 您可能不希望不同的客户看到彼此的数据。 行级安全性是SQL的一个难点。 表级更容易。 另一种方法是为每个客户创建一个单独的数据库。 除了安全优势之外,这还允许您将不同的客户移动到不同的服务器以满足需求。 这需要付出代价。 如果您有公共表,那么您需要复制它们或拥有“公用表”数据库。 并且,当您更新代码时,您需要更新所有数据库。 后者实际上也可能是一个优 ... -
如何使用PHP一次将数据插入两个mysql表[重复](How can I insert data into two mysql tables at once using PHP [duplicate])[2022-03-10]
不,这是不可能的。 Mysql INSERT语法接受一个且只有一个表作为目标 No, it is not possible. Mysql INSERT syntax accepts one and only one table as a target -
在查询中使用alias 。 $user_id = 1; $sql = "SELECT login.salt as loginSalt, cookie.salt as cookieSalt FROM login, cookie WHERE login.user_id AND cookie.user_id = :user_id"; $params = array( 'user_id' => $user_id ); $stmt = $db->justConnect()->pr ...
-
您可以为PHP AMFPHP选择Flex远程处理,也可以使用A HTTPService进行连接 托管在您的服务器上的php文件返回Db行。 尝试谷歌搜索 - 连接Flex Mysql http://www.sephiroth.it/tutorials/flashPHP/as3flexdb/ http://cookbooks.adobe.com/post_Simple_MySQL_to_Flex-5561.html http://www.actionscript.org/resources/articles/ ...
-
这不是有效的SQL; 你需要分开做。 也许: $sql = " INSERT INTO `directory`(`First_Name`,`Surname`,`Nicknames`) VALUES (:firstname, :surname, :nicknames); INSERT INTO `nicknames`(`First_Name`,`Surname`,`Nicknames`) VALUES (:firstname, :surname, :nicknames); "; That’s ...
-
您想要找到id不相等的行,但您需要避免行的乘法。 这是两种技术。 使用LEFT JOIN但不包括连接存在的那些 SELECT rfc.id AS Ready_For_Client_id , rfc.rts_id , t.Truck_Number , u.Name AS User_Name , c.Name AS Client_Name FROM ready_for_client rfc INNER JOIN trucks t ON rfc.truck_id = t ...
-
你必须添加条件: challenges.id = verify.challenge_id 到where子句如下 "SELECT * FROM `challenges`,`verifications` WHERE `challenges`.`id` = `verify`.`challenge_id` AND (`challenges`.`user_id`='".$this->record['id']."' OR `challenges`.`opponent_id`='".$this- ...
-
MySQL连接表[重复](MySQL connect tables [duplicate])[2023-03-03]
简单的JOIN查询: - SELECT Analyze.Name, Data.Standard FROM Analyze INNER JOIN Data ON Data.id_analyze = Analyze.id_analyze ORDER BY Analyze.Name, Data.Standard 请注意,您可以加入如何完成它们,但更新的方法是显式使用INNER JOIN / LEFT OUTER JOIN /等。 然后你可以绕过这一次的结果 Simple JOIN query:- SELE ... -
连接到mysql [重复](Connect to mysql [duplicate])[2022-03-24]
您可以使用MySQL-Python包。 在从源代码安装之前先搜索你的disto的包数据库(你可能已经有了它)。 请注意,所有数据库包(我们应该)都遵守Python DB API 。 这为所有数据库提供了标准化接口。 如果您对编写SQL感到满意,那就足够了。 如果你更喜欢在纯python中查询和定义数据,我也会推荐SQLAlchemy 。 You can use the MySQL-Python package. Search your disto's package database first befor ...