简体   繁体   English

Python 编号列表中元素的出现次数

[英]Python number the occurrences of elements in list

Having a problem when working on lists在处理列表时遇到问题

trying to figure out how to number elements if they appear more than once, from the second appearance i want to add the number near the '@':试图弄清楚如果元素出现多次时如何编号,从第二次出现我想在“@”附近添加数字:

for example:例如:

['example@company.com', 'example@company.com', 'none@comapny.com','example@company.com']

wanted output :想要的输出:

['example@company.com', 'example2@company.com', 'none@comapny.com','example3@company.com']

Code so far :到目前为止的代码:

count_apper=Counter(mails_list)
    for values in count_apper.items():
        for mail in mails_list:
            if values[0]==mail:
                number+=1
                temp_var=mail.split("@") 
                temp_var[0]=temp_var[0]+f"{number}"
                temp_var="@".join(temp_var)
                print(temp_var)
            number=1

Output :输出 :

example1@company.com
example2@company.com
example2@company.com
none2@company.com

I would base my answer off of a collections.Counter() I think.我认为我的答案基于collections.Counter() It will do some of the work for you.它会为你做一些工作。

import collections

addresses = ['example@company.com', 'example@company.com', 'none@comapny.com', 'example@company.com']

results = []

for address, count in collections.Counter(addresses).items():
    # add a "first" address as is
    results.append(address)

    # If there were other occurrences add them
    for i in range(1, count):
        results.append(f"{i+1}@".join(address.split("@")))

print(results)

This should give you:这应该给你:

['example@company.com', 'example2@company.com', 'example3@company.com', 'none@comapny.com']

You can iterate the list and use a dict to keep track of the number of occurences of a specific address.您可以迭代列表并使用dict来跟踪特定地址的出现次数。 To add text before the @ sign, you can use the .split method of str .要在@符号前添加文本,您可以使用str.split方法。 A possible implementation looks as follows.一种可能的实现如下所示。

addresses = ['example@company.com', 'example@company.com', 'none@comapny.com', 'example@company.com']
occurence_count = {}
transformed = []
for a in addresses:
    count = occurence_count.get(a, 0) + 1
    occurence_count[a] = count

    name, domain = a.split('@')
    if count > 1:
        transformed.append(f'{name}{count}@{domain}')
    else:
        transformed.append(a)

print(transformed) 

Try this试试这个


j=['example@company.com', 'example@company.com', 'none@comapny.com','example@company.com']
count=1
k=j.copy()
for i in range(len(j)):
    
    if k.count(k[i])>1:
        m=[char for char in j[i]]
        m.insert(j[i].index('@'),str(count)
        )
        count+=1
        j[i]=''.join(m)
print (j)

I dont know about python but i guess this logic will work我不知道 python 但我想这个逻辑会起作用

read the string in an array format findif s[int x]=@ then you can do s[int x-1]=1 and for next thing you can do as ++s[x-1]以数组格式读取字符串 findif s[int x]=@ 然后你可以做 s[int x-1]=1 并且接下来你可以做 ++s[x-1]

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

相关问题 如何在python中的列表列表中查找元素的出现 - how to find occurrences of elements in a list of list in python Python 从字典中拆分列表中的字符串后,分别计算出现次数以及出现次数最多的元素 - Python count the number of occurrences and also the elements with maximum occurences separately after splitting the string in list from dictionary 我如何让 python 在列表中找到不同的元素及其在其中的出现次数 - How do i make python find differing elements in a list and their number of occurrences within it 计算python列表中标记之间的出现次数 - Count the number of occurrences between markers in a python list 在Python中查找“-”登录列表组的出现次数 - Find number of occurrences of groups of '-' sign in list in Python 计算值字典列表python的出现次数 - Count the number of occurrences for values dictionary list python Python-如何计算列表中的出现次数 - Python - How to count the number of occurrences in a list Python:计算字符串中列表项的出现次数 - Python: Count number of occurrences of list items in a string Python-计算列表中日期的出现次数 - Python- Count number of occurrences of a date in a list 使用 Python 查找字符串中列表的出现次数 - Find number of occurrences of a list in a string using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM