Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用';在';在一组数组中_Python_Arrays - Fatal编程技术网

Python 使用';在';在一组数组中

Python 使用';在';在一组数组中,python,arrays,Python,Arrays,我有一个列表列表,所以是一个二维数组。我试图在中使用来确定这些数组中是否有字符串。因此,如果单词列表中的单词:,我使用的不是,而是: for i in range(len(wordlist)): if word in wordlist[i]: wordlist[i][2] += 1 #this accounts for the frequency of a word in a file if (i + 1 == len(wordlist) and word !=wordlist

我有一个列表列表,所以是一个二维数组。我试图在中使用
来确定这些数组中是否有字符串。因此,如果单词列表中的单词:
,我使用的不是
,而是:

for i in range(len(wordlist)):
   if word in wordlist[i]:
      wordlist[i][2] += 1 #this accounts for the frequency of a word in a file
if (i + 1 == len(wordlist) and word !=wordlist[len(wordlist)-1]):
   wordlist.append([word,linenum,1])
有没有更优雅的方法

编辑: 示例
wordlist

['civilizati',1,1],'of',1,1],'science',2,1],'is',2,1],'knowledge',3,1],'is',3,1]

编辑:

我想我的问题不清楚。如果我使用1d列表,我可以这样做:

if(word in wordlist1d):
   print("It's here")
else:
   wordlist1d.append(word)

对于
循环怪物,这比我的
要优雅得多。然而,由于wordlist不是1d,wordlist中的语句word永远不会返回true,即使word位于wordlist的子列表中。我想知道是否有一种比使用
for
循环更优雅的方法。

直接迭代列表,而不是使用索引指针来迭代列表:

for item in wordlist:
    if word in item:
        ....
为了证明它们是一样的:

In [2]: for item in wordlist:
   ...:     print item
   ...:
['civilizati', 1, 1]
['of', 1, 1]
['science', 2, 1]
['is', 2, 1]
['knowledge', 3, 1]
['is', 3, 1]

In [3]: for i in range(len(wordlist)):
   ...:     print wordlist[i]
   ...:
['civilizati', 1, 1]
['of', 1, 1]
['science', 2, 1]
['is', 2, 1]
['knowledge', 3, 1]
['is', 3, 1]
现在您已经提供了数据格式,您需要的是

for item in wordlist:
    if word == item[0]:
        item[2] += 1

如果要查找文件中的字数,可以执行以下操作:

from collections import Counter
wordlist = Counter(myfile.read().split())
wordlist.keys()
[[k] + v for k, v in wordlist.items()]
如果要获取文件中的所有单词,可以执行以下操作:

from collections import Counter
wordlist = Counter(myfile.read().split())
wordlist.keys()
[[k] + v for k, v in wordlist.items()]
如果要获得分词的计数:

wordlist['myword']

您最好使用
dict
,将单词作为键,将
[linenum,count]
对作为值。您的整个功能变得简单:

wordlist.setdefault(word, [linenum, 0])[1] += 1
如果必须将其转换为列表,可以执行以下操作:

from collections import Counter
wordlist = Counter(myfile.read().split())
wordlist.keys()
[[k] + v for k, v in wordlist.items()]

我建议使用
else
子句进行循环:

for sublist in wordlist:
    if word == sublist[0]:  # compare directly with the relevant part of the sublist
        sublist[2] += 1
        break  # we only will match at most once, so break after finding one
else:    # this clause is run if no break was hit
    wordlist.append([word, linenum, 1])
一种更有效的方法可能是使用字典从单词映射到
lineno,count
子列表。搜索字典是
O(1)
,而不是
O(n)
来搜索列表。为了使代码适用于未找到的单词,您可以使用
setdefault
方法:

d = {}
for word, lineno in document:
    d.setdefault(word, [lineno, 0])[1] += 1 # works whether or not word was in d before

您是否正在获取将字符串作为子字符串的字符串?请解释您到底想做什么。如果您发布一个单词列表的小示例,可能会有所帮助。您的内部列表似乎是
word、line、count
三元组,因此可能不需要对
in
进行测试(您只想针对
word
部分进行测试).@Blckknght我猜使用in会增加问题的顺序,不是吗。+1这当然很优雅。而且干净。而且很快。而且容易理解。实际上,这与依赖于i的输出的
if
语句不兼容。不,它与您的代码完全相同。看看上面。@MatthewTrevor也许我弄错了。代码中的第二个
if
语句在哪里?我想知道这个词是否在列表中,或者我是否必须添加它。你好像说我的
for
循环和你的
for
循环是相同的,这是真的。但是,在您的
for
循环之后,没有第二个案例计划我必须将单词添加到列表中。我认为它没有做您认为它是做的事情。如果(True和word!=单词列表中的最后一个单词)
,你所拥有的实际上就是
。你可能是对的。我读到,如果我改变dict中的值,它可能不会正确更新?这是在pydoc里。你知道这意味着什么吗?你不能改变钥匙,但你可以改变价值这合法吗?这正是我要找的for@JFA:什么是合法的?
其他
?这是合法的,尽管这可能会让那些更习惯于其他语言的人感到困惑,因为这些语言的循环中没有
else
子句。