【问题标题】:How to rename string duplicates in python?如何在python中重命名字符串重复项?
【发布时间】:2019-02-20 01:12:58
【问题描述】:

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

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

我希望列表为

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

所有名称重命名为 1,城市为 2,邮编为 3。如果有更多重复项,它们也将重命名为 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、zip 3 和下一个重复值 4?

【问题讨论】:

  • index 返回列表中元素第一次出现的索引。

标签: python duplicates rename


【解决方案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,为(唯一值)返回 2,然后为下一个重复值返回 3,为下一个唯一或重复值返回 4,5,6。您已经回答了我的问题,并且代码运行良好。我可以在一个函数中做到这一点,而不是为唯一值编写另一个函数。谢谢
  • num >= 1 完成了所需的工作。
【解决方案2】:

快到了!我注释了附加代码:

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]

【讨论】:

    【解决方案3】:

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

    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, 3, 3]

    【讨论】:

    • 使用原始列表,否则看起来你有一个错误。
    【解决方案4】:

    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)
    

    【讨论】:

      【解决方案5】:

      你很亲密……

      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]
      

      【讨论】:

        【解决方案6】:

        你可以使用这个解决方案:

        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)
        

        【讨论】:

          猜你喜欢
          • 2017-05-26
          • 2013-05-14
          • 2018-03-04
          • 2019-06-27
          • 1970-01-01
          • 2021-07-09
          • 1970-01-01
          • 1970-01-01
          • 2021-04-17
          相关资源
          最近更新 更多