我需要在列表中找到元素的频率
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
Run Code Online (Sandbox Code Playgroud)
输出 - >
b = [4,4,2,1,2]
Run Code Online (Sandbox Code Playgroud)
另外我想从a中删除重复项
a = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud) 我有一个布尔列表:
[True, True, False, False, False, True]
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来计算True列表中的数量(所以在上面的例子中,我希望返回3.)我找到了寻找特定元素出现次数的例子,但还有更多我和Booleans一起工作的有效方法呢?我想的东西类似all或any.
我需要一种快速的方法来计算python中整数的位数.我目前的解决方案是
bin(n).count("1")
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更快的方法这样做?
PS :(我代表一个大的2D二进制数组作为数字和按位操作的单一列表,并且将时间从几小时缩短到几分钟.现在我想摆脱那些额外的分钟.
编辑:1.它必须在python 2.7或2.6中
并且对小数量进行优化并不重要,因为那不是一个明确的瓶颈,但我确实在某些地方有10 000 +位的数字
例如,这是一个2000位的情况:
12448057941136394342297748548545082997815840357634948550739612798732309975923280685245876950055614362283769710705811182976142803324242407017104841062064840113262840137625582646683068904149296501029754654149991842951570880471230098259905004533869130509989042199261339990315125973721454059973605358766253998615919997174542922163484086066438120268185904663422979603026066685824578356173882166747093246377302371176167843247359636030248569148734824287739046916641832890744168385253915508446422276378715722482359321205673933317512861336054835392844676749610712462818600179225635467147870208L
Run Code Online (Sandbox Code Playgroud) 我如何计算 Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch 中的字母?
print(len('Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'))
Run Code Online (Sandbox Code Playgroud)
说 58
好吧,如果有那么容易,我就不会问你了,现在是吗?!
维基百科说(https://en.wikipedia.org/wiki/Llanfairpwllgwyngyll#Placename_and_toponymy)
名称的长格式是英国最长的地名之一,也是世界上最长的地名之一,有 58 个字符(51 个“字母”,因为“ch”和“ll”是二合字母,在威尔士语)。
所以我想数一数并得到答案 51。
对。
print(len(['Ll','a','n','f','a','i','r','p','w','ll','g','w','y','n','g','y','ll','g','o','g','e','r','y','ch','w','y','r','n','d','r','o','b','w','ll','ll','a','n','t','y','s','i','l','i','o','g','o','g','o','g','o','ch']))
51
Run Code Online (Sandbox Code Playgroud)
是的,但那是作弊,显然我想使用这个词作为输入,而不是列表。
维基百科也说威尔士语的有向图是ch, dd, ff, ng, ll, ph, rh, th
https://en.wikipedia.org/wiki/Welsh_orthography#Digraphs
所以我们走了。让我们把长度加起来,然后去掉重复计算。
word='Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'
count=len(word)
print('starting with count of',count)
for index in range(len(word)-1):
substring=word[index]+word[index+1]
if substring.lower() in ['ch','dd','ff','ng','ll','ph','rh','th']:
print('taking off double counting of',substring)
count=count-1
print(count)
Run Code Online (Sandbox Code Playgroud)
这让我走到这一步
starting with count of 58
taking off double counting of Ll
taking off double counting of ll
taking off double counting of ng
taking off …Run Code Online (Sandbox Code Playgroud) 哪里可以找到LogLog算法的有效实现?试图自己实现它,但我的草案实现产生了奇怪的结果.
这是:
function LogLog(max_error, max_count)
{
function log2(x)
{
return Math.log(x) / Math.LN2;
}
var m = 1.30 / max_error;
var k = Math.ceil(log2(m * m));
m = Math.pow(2, k);
var k_comp = 32 - k;
var l = log2(log2(max_count / m));
if (isNaN(l)) l = 1; else l = Math.ceil(l);
var l_mask = ((1 << l) - 1) >>> 0;
var M = [];
for (var i = 0; i < m; ++i) M[i] = 0; …Run Code Online (Sandbox Code Playgroud) 找到正整数位数的最佳方法是什么?
我找到了这3种基本方法:
转换为字符串
String s = new Integer(t).toString();
int len = s.length();
Run Code Online (Sandbox Code Playgroud)for循环
for(long long int temp = number; temp >= 1;)
{
temp/=10;
decimalPlaces++;
}
Run Code Online (Sandbox Code Playgroud)对数计算
digits = floor( log10( number ) ) + 1;
Run Code Online (Sandbox Code Playgroud)你可以在大多数语言中计算log10(x)= ln(x)/ ln(10).
首先我认为字符串方法是最脏的,但我想的越多,我认为这是最快的方法.或者是吗?
我是一个蟒蛇新手,所以也许我的问题非常棒.假设我有一个单词列表,我想找到每个单词出现在该列表中的次数.明显的做法是:
words = "apple banana apple strawberry banana lemon"
uniques = set(words.split())
freqs = [(item, words.split().count(item)) for item in uniques]
print(freqs)
Run Code Online (Sandbox Code Playgroud)
但是我发现这个代码不是很好,因为这种方式程序运行两次单词列表,一次构建集合,第二次计算出现次数.当然,我可以编写一个函数来运行列表并进行计数,但这不会那么pythonic.那么,有更高效和pythonic的方式吗?
在Python中,我有一个列表:
L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 56, 6, 7, 67]
Run Code Online (Sandbox Code Playgroud)
我想确定发生次数最多的项目.我能够解决它,但我需要最快的方法来解决它.我知道有一个很好的Pythonic答案.
有很多方法可以编写计算直方图的Python程序.
通过直方图,我的意思是一个函数,它计算a中对象的出现次数iterable并输出字典中的计数.例如:
>>> L = 'abracadabra'
>>> histogram(L)
{'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2}
Run Code Online (Sandbox Code Playgroud)
编写此函数的一种方法是:
def histogram(L):
d = {}
for x in L:
if x in d:
d[x] += 1
else:
d[x] = 1
return d
Run Code Online (Sandbox Code Playgroud)
是否有更简洁的方法来编写此功能?
如果我们在Python中有字典理解,我们可以写:
>>> { x: L.count(x) for x in set(L) }
Run Code Online (Sandbox Code Playgroud)
但由于Python 2.6没有它们,我们必须写:
>>> dict([(x, L.count(x)) for x in set(L)])
Run Code Online (Sandbox Code Playgroud)
虽然这种方法可以读取,但效率不高:L经过多次.此外,这对单寿命发电机不起作用; 该函数应该对迭代器生成器同样有效,例如:
def gen(L):
for x in L:
yield x
Run Code Online (Sandbox Code Playgroud)
我们可能会尝试使用该reduce函数(RIP):
>>> reduce(lambda d,x: dict(d, …Run Code Online (Sandbox Code Playgroud) 我需要一个STL算法,该算法需要一个谓词和一个集合,并在集合中true只有一个成员满足该谓词的情况下返回,否则返回false。
我将如何使用STL算法来做到这一点?
例如,用STL算法代码替换以下内容以表示相同的返回值。
int count = 0;
for( auto itr = c.begin(); itr != c.end(); ++itr ) {
if ( predicate( *itr ) ) {
if ( ++count > 1 ) {
break;
}
}
}
return 1 == count;
Run Code Online (Sandbox Code Playgroud)