我有一个清单:
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)))
但这当然会增加每个元素的总计数。
答案 0 :(得分:2)
您可以使用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']
另一种Pythonic方式:
>>> 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']
答案 1 :(得分:1)
您可以使用enumerate
,
[j+'_'+str(list_a[:i+1].count(j)) for i,j in enumerate(list_a)]
<强>观强>
使用
enumerate
我也会得到元素的索引,所以会 切入当前元素并计算出现的数量 切片清单。
<强>结果:强>
['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
我的方法仍然在这方面取得了不错的表现。
答案 2 :(得分:1)
使用Python的Counter()
计算每个单词:
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']
答案 3 :(得分:0)
import collections
list_a = ['hello', 'goodbye', 'goodbye', 'hello', 'whatever', 'whatever', 'whatever', 'hello']
#build a counter for each word and a index list for each word.
counter = {k:list(range(v+1,0,-1)) for k,v in collections.Counter(list_a).items()}
#attach index to each each occurance of a word
list_b = [e+'_'+str(counter[e].pop()) for e in list_a]
print(list_b)
['hello_1', 'goodbye_1', 'goodbye_2', 'hello_2', 'whatever_1', 'whatever_2', 'whatever_3', 'hello_3']
答案 4 :(得分:0)
检查一下 -
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
答案 5 :(得分:0)
您可以使用临时list
添加单词,然后将其添加到list_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']
答案 6 :(得分:0)
如果您知道列表中将包含哪些元素,则可以创建一个变量来计算它们。你甚至可以用两个循环来完成它:
提供的代码并不是最明智的方法,但它应该运行良好
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"