简体   繁体   English

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

[英]How to rename string duplicates in python?

I am trying to replace duplicate string with duplicate integer. 我正在尝试将重复的字符串替换为重复的整数。 For example: 例如:

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

I want the list as 我希望列表为

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

All name renamed as 1, city as 2 and zip 3. If there are more duplicates those will be also renamed as 4, 5 6 etc. 所有名称都重命名为1,城市分别为2和zip3。如果有更多重复项,这些名称也将重命名为4、5 6等。

I have tried with 我尝试过

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

But got 但是得到了

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

How to get 1 for name, 2 for city, 3 for zip and 4 for next duplicate value? 如何获得1的名称,2的城市名称,3的邮政编码和4的下一个重复值?

Just modified your code 刚刚修改了您的代码

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]

Almost there! 快好了! I commented the additional code: 我评论了附加代码:

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]

Construct a dictionary of indices for each item in mylist - this is similar to using collections.Counter except it preserves the item indices. mylist每个项目构造一个索引字典-与使用collections.Counter相似,只不过它保留了项目索引。 Use an OrderedDict to preserve the order of the items in the list. 使用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]

Iterate over the dictionary values; 遍历字典值; check the length; 检查长度; change the item if the criteria is met. 如果符合条件,则更改项目。

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

Maybe not the most beautiful solution, but this works: 也许不是最漂亮的解决方案,但这可行:

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

You get then [1, 'state', 1, 2, 2, 1, 3, 3] 您会得到[1,'state',1,2,2,1,3,3]

You're very close... 你很近...

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]

You can use this solution: 您可以使用以下解决方案:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM