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

Python:通过采取可能的覆盖,使用渐进式索引重命名列表中的重复项

Python:通过采取可能的覆盖,使用渐进式索引重命名列表中的重复项,python,regex,indexing,duplicates,rename,Python,Regex,Indexing,Duplicates,Rename,我需要为字符串列表编制索引,因为我不希望在重命名文件时出现重复项以避免覆盖 假设我有这样一个列表: list = ['name', 'city', 'city', 'name1', 'town', 'city2', 'town', 'name2'] list = ['name3', 'city1', 'city3', 'name1', 'town1', 'city2', 'town2', 'name2'] 我想得到这样一份清单: list = ['name', 'city', 'city',

我需要为字符串列表编制索引,因为我不希望在重命名文件时出现重复项以避免覆盖

假设我有这样一个列表:

list = ['name', 'city', 'city', 'name1', 'town', 'city2', 'town', 'name2']
list = ['name3', 'city1', 'city3', 'name1', 'town1', 'city2', 'town2', 'name2']
我想得到这样一份清单:

list = ['name', 'city', 'city', 'name1', 'town', 'city2', 'town', 'name2']
list = ['name3', 'city1', 'city3', 'name1', 'town1', 'city2', 'town2', 'name2']
为此,我受到了这篇文章的启发:

而我写的这段代码却没有!谁能找到它遗漏了什么,或者告诉我它是否完全错了

def unique(mylist):
newlist = []

for i, v in enumerate(mylist):
    totalcount = mylist.count(v)
    print('Step 1, totalcount : '+ str(totalcount))
    count = mylist[:i].count(v)
    print('Step 2, count : '+ str(count))

    if totalcount > 1 :
        print('Logic test :' + str(bool((v + str(count + 1)) in mylist)))
        while (v + str(count + 1)) in mylist :
            count += 1 
        list[i] = v + str(count+1)
        
    else : 
        list[i] = v

    newlist.append(list[i])
    print('Step 3 : '+str(newlist))

return newlist
我的代码的结果是:

['name', 'city1', 'city', 'name1', 'town1', 'city2', 'town', 'name2']
因为我可以找到代码,所以我想应用一个函数,只删除字符串的最后几位,就像下面的函数一样,然后使用链接中的一个代码进行索引。然而,我会发现直接做更优雅。你觉得怎么样

def delete_digit_end(string):

name_parts=re.findall(r'[^\d_]+|[^\D]+|[^\W_]+|[\W_]+', string) # this part creates a list by splitting the digits,
# letters and '-_'

lenght=len(name_parts)-1 #we want to analize the last element of the list, if it contains digits or '_-'

# We do a loop while to test if the parts have digits or '-_', if true we execute the loop until it is false 
while name_parts[lenght].isdigit() : 
    # if it is true it will remove them
    name_parts[lenght]=''# it will remove them
    lenght -= 1 # if the condition was true, we continue with one inferior part

new_string = ''.join(map(str,name_parts))# now that we have cleaned if it was necessary we concatenate them

return new_string

以下是您可以进行的工作:

l = ['name', 'city', 'city', 'name1', 'town', 'city2', 'town', 'name2']

new_list = []

for item in l:
    i = 1
    if item[-1].isnumeric():
        new_list.append(item)
    else:
        while item + str(i) in l or item + str(i) in new_list:
            i += 1
        new_list.append(item + str(i))

print(new_list)
输出:

['name3', 'city1', 'city3', 'name1', 'town1', 'city2', 'town2', 'name2']

在预期产出中,顺序真的很重要吗?这取决于你的意思;如果不是真正的索引,这就是为什么我认为只要删除所有现有的索引就有了基础。但是,我有两个其他列表,一个用于路径,另一个具有文件扩展名,因此我必须保持列表中字符串的位置相同。原则是避免过度写入(当我移动文件并重命名它们时出现错误窗口。我的意思是
['name1','name2','name3','city1',等]
ahh不,但它可以是'['name1','city1','city2',name2,…],所以为什么我想删除输入端的所有数字。它错过了正确的缩进。