如何在python中重命名字符串重复项?

时间:2019-02-19 15:29:47

标签: python duplicates rename

我正在尝试将重复的字符串替换为重复的整数。 例如:

mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip", "name"]

我希望列表为

mylist = [1, 'State', 1, 2,2,1,3,3,1]

所有名称都重命名为1,城市分别为2和zip3。如果有更多重复项,这些名称也将重命名为4、5 6等。

我尝试过

mylist = ["name", "state", "name", "city", "name", "zip", "zip"]
from collections import Counter 
counts = Counter(mylist) 
for s,num in counts.items():
    if num > 1:
       mylist[mylist.index(s)] = 1

但是得到了

mylist = [1, 'state', 'name', 'city', 'name', 1, 'zip']

如何获得1个名称,2个城市,3个邮政编码和4个下一个重复值?

6 个答案:

答案 0 :(得分:1)

只需修改您的代码

mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip", "name"]
from collections import Counter
counts = Counter(mylist)
counts
Out[309]: Counter({'city': 1, 'name': 3, 'state': 1, 'zip': 2})
Count=1
for s,num in counts.items():
    if num > 1:
       for  i, j in enumerate(mylist):
           if j==s:
               mylist[i] = Count
       Count=Count+1
mylist
Out[320]: [1, 'state', 1, 2, 2, 1, 3, 3, 1]

答案 1 :(得分:1)

快到了!我评论了其他代码:

from collections import Counter 

mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip", "name"]

counts = Counter(mylist) 

c = 0

for s,num in counts.items():
    if num > 1:
      c+= 1 # create a variable (integer) to replace the var in the list (starting with 1 as in your example)
      for x in mylist: # since index returns only the first instance, iterate over your list
        if x == s: 
          mylist[mylist.index(x)] = c # replace with your new integer variable

print(mylist)
# [1, 'state', 1, 2, 2, 1, 3, 3, 1]

答案 2 :(得分:1)

mylist中的每个项目构造一个索引字典-这类似于使用collections.Counter,但它保留了项目索引。使用OrderedDict保留列表中项目的顺序。

import collections
mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip", "name"]       
d = collections.OrderedDict()
for index, item in enumerate(mylist):
    try:
        d[item].append(index)
    except KeyError:
        d[item] = [index]

遍历字典值;检查长度;如果符合条件,请更改项目。

count = 1
for indices in d.values():
    if len(indices) > 1:
        for index in indices:
            mylist[index] = count
        count+=1
print(mylist)

答案 3 :(得分:1)

也许不是最漂亮的解决方案,但这可行:

mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip"]
from collections import Counter 
counts = Counter(mylist) 
val = 1
for s,num in counts.items():
    if num > 1:
        counts[s] = val
        val += 1
    else:
        counts[s] = 0
mylist = [x if counts[x]==0 else counts[x] for x in mylist]
mylist

然后您会得到[1,'state',1,2,2,1,1,3,3]

答案 4 :(得分:0)

您非常接近...

for s in counts:
    if counts[s] > 1: 
        mylist[mylist.index(s)] = mylist.index(s)
# myList is now [0, 'state', 2, 'city', 'name', 5, 6]

答案 5 :(得分:0)

您可以使用以下解决方案:

from collections import Counter
from itertools import count
from operator import itemgetter

mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip", "name"]
C = Counter(mylist)
c = count(start=1)
C = {k: next(c) if v > 1 else k for k, v in C.items()}
itemgetter(*mylist)(C)
# (1, 'state', 1, 2, 2, 1, 3, 3, 1)