Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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 - Fatal编程技术网

Python 从列表计数事件,并创建添加计数的新列表

Python 从列表计数事件,并创建添加计数的新列表,python,Python,我有一份清单: list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello'] 我想创建一个新列表,其中包含如下项计数: list_b = ['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3'] list_b=[] for

我有一份清单:

list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']
我想创建一个新列表,其中包含如下项计数:

list_b = ['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
list_b=[]
for item in list_a:
    list_b.append(item+"_"+str(list_a.count(item)))
我尝试过这样的事情:

list_b = ['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
list_b=[]
for item in list_a:
    list_b.append(item+"_"+str(list_a.count(item)))

但这当然会增加每个元素的总计数。

您可以使用dict存储索引:

list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']

d={}
r=[]
for i in list_a:
    d.setdefault(i, 0)
    d[i]+=1
    r.append(i+"_"+str(d[i]))

print r
输出:

['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
另一种类似蟒蛇的方式:

>>> list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']
>>> d={}
>>> [i+"_"+str(len(d[i])) for i in list_a if not d.setdefault(i,[]).append(True)]
['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
检查这个-

list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']
list_b=[]
someDict={}
for item in list_a:
    if item in someDict.keys():
        temp_count=someDict[item]+1
        temp_item=list_b.append(item+"_"+str(temp_count))
        someDict[item]=temp_count
    else:
        list_b.append(item+"_1")
        someDict[item]=1
print list_b

您可以使用
枚举

[j+'_'+str(list_a[:i+1].count(j)) for i,j in enumerate(list_a)]
创意:

['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
使用
enumerate
我也会得到元素的索引,也会得到元素的索引 向上切片到当前元素,并计算中发生的次数 切片列表

结果:

['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
执行时间:

['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
根据我的回答中关于执行时间的讨论,我已经讨论了这里实现的所有方法,这里是时间

In [68]: %timeit Mc_grady_method_1()
100000 loops, best of 3: 4.29 µs per loop

In [69]: %timeit Mc_grady_method_2()
100000 loops, best of 3: 4.35 µs per loop

In [70]: %timeit Rahul_KP()
100000 loops, best of 3: 3.8 µs per loop

In [71]: %timeit Moe_A()
100000 loops, best of 3: 3.94 µs per loop

In [72]: %timeit Allen()
100000 loops, best of 3: 13.1 µs per loop

In [73]: %timeit Mayur_Buragohain()
100000 loops, best of 3: 3.86 µs per loop

In [74]: %timeit Martin_Evans()
100000 loops, best of 3: 10.5 µs per loop

不过,我的方法在这方面还是有一些不错的表现。

您可以使用temp
列表添加单词,然后使用如下计数将其添加到列表b中:

list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']
    list_b = []
    tmp = []
    for word in list_a:
        tmp.append(word)
        list_b.append(word + '_' + str(tmp.count(word)))
    print list_b
输出:

['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']

如果您知道列表中将包含哪些元素,则可以创建一个变量来计算它们。您甚至可以通过两个循环来完成:

提供的代码并不是最聪明的方法,但它应该可以很好地工作

list_items=[]
counters_items=[]
for item in list_a:
    if item in list_items:
        pass
    else:
        list_items.append(item)

# Now we have stored a list of all type of item

list_b = list_a.copy()
for item in list_items:
    counter = 1
    for it in list_b:
        if item == it:
            it = it + "_" + str(counter)
            counter +=1
    # If you want to make sure the whole list has been numbered
    if counter != list_a.count(item) + 1:
        print "Smth wrong happened"
使用Python对每个单词进行计数:

from collections import Counter

word_count = Counter()
list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']
list_b = []

for word in list_a:
    word_count[word] += 1
    list_b.append('{}_{}'.format(word, word_count[word]))

print list_b
给你:

['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']

向我们展示您尝试过的内容。这不是免费的编码服务。在遍历列表时,使用dict跟踪当前的字数。这是一种非常聪明的方法。@Julien更新了我的问题,请检查它。在一个包含3个元素的玩具列表上进行测试不会显示您在O(n)和O(n^2)方法之间受到的巨大影响,在一个1000000项列表上再试一次,我敢打赌你的方法会比一个好的O(n)方法慢(除非Python在后台为你做一些鬼鬼祟祟的记忆…)