R 'which' 関数は、文字ベクトルの長い文字列内の文字を検索する効率的で並列化された方法です。Pythonでこれを実装する関数または簡単な方法はありますか?
3004 次
3 に答える
0
R がどのように機能するかはよくわかりませんが、開始するためのいくつかのアイデアを次に示します。
lookingFor = 'a'
print "the string of all '%s's is" %lookingFor, ''.join(char for char in myLongString if char==lookingFor)
print "there are %s many instances of the character '%s' in the string '%s'" %(myLongString.count(lookingFor), lookingFor, myLongString)
import collections
print "there are %s many instances of the character '%s' in the string '%s'" %(collections.Counter(myLongString)[lookingFor], lookingFor, myLongString)
于 2013-09-16T07:37:21.580 に答える
0
または汎用関数にラップ:
>>> which = lambda targetList, f: [index for index, item in enumerate(targetList) if f(item)]
例えば:
>>> mylist = [1,2,3,4,5,6,7,8]
>>> which(mylist, lambda x: x % 2)
于 2017-04-17T13:37:08.173 に答える