简体   繁体   English

删除或更改列表的特定重复元素(并非所有重复元素)

[英]Remove or change SPECIFIC repeated elements of a list (not all repeated elements)

I'm trying to make something that can read through a list of character elements and tell you exactly how many times each element was repeated.我正在尝试制作一些可以通读字符元素列表并准确告诉您每个元素重复多少次的东西。 My idea was to go through the list using a for loop and printing statements that tell me the info I want.我的想法是使用 for 循环和打印语句来浏览列表,这些语句告诉我我想要的信息。

This was my first idea:这是我的第一个想法:

list = ["code", "this", "code"]

for i in range(len(list)):
    list.count(list[i])
    print("{} is repeated ".format(list[i]) + str(list.count(list[i])) + " times")

When I ran this code it printed:当我运行此代码时,它打印出:

code is repeated 2 times代码重复2次

this is repeated 1 times这是重复1次

code is repeated 2 times代码重复2次

Now, the next step towards my goal would be to to stop "code is repeated 2 times" from printing twice.现在,实现我的目标的下一步是停止“代码重复 2 次”打印两次。 This is were my troubles begin.这是我的麻烦开始。 I've searched for ways to remove specific duplicates from lists but all I've found is way to remove ALL duplicates (which I don't want since that would make the code useless after it goes past the first element of the list).我已经搜索了从列表中删除特定重复项的方法,但我发现的只是删除所有重复项的方法(我不想要这样,因为这会使代码在经过列表的第一个元素后变得无用)。 Therefore, my questions are the following:因此,我的问题如下:

Would it be possible to remove specific repeated elements from the list once the statement of repetitions is printed?一旦打印重复语句,是否可以从列表中删除特定的重复元素? This would mean, once it prints "code is repeated 2 times", the list changes to ["this"] only.这意味着,一旦它打印“代码重复 2 次”,列表就会更改为仅 ["this"]。

Would it instead be possible to change the "values" of a specific repeated element?是否可以更改特定重复元素的“值”? This would mean, once it prints "code is repeated 2 times", the list would change (for example) to [0, "this", 0] so that I could use an if statement to not print anything if the element is == 0.这意味着,一旦它打印“代码重复 2 次”,列表将更改(例如)为 [0, "this", 0] 以便我可以使用 if 语句在元素为 = 时不打印任何内容= 0。

To be clear, I just want to know:说清楚,我只想知道:

-if it is possible: how could I change my coding to have that happen. - 如果可能的话:我怎么能改变我的编码来实现这种情况。

-if it is not possible: other things I might be able to do to reach my goals. -如果这是不可能的:我可以做的其他事情来实现我的目标。

Useset :使用set

lst = ["code", "this", "code"]
for elem in sorted(set(lst), key = lambda x:lst.index(x)):
    print(f"{elem} is repeated {lst.count(elem)} times")

Or, dict.fromkeys :或者, dict.fromkeys

for elem in dict.fromkeys(lst):
    print(f"{elem} is repeated {lst.count(elem)} times")

Output:输出:

code is repeated 2 times
this is repeated 1 times

You can also look at collections.Counter() :您还可以查看collections.Counter()

from collections import Counter
frequency = Counter(lst)
for word, freq in frequency.items():
    print(f"{word} is repeated {freq} times")

Same output.相同的输出。

What you are saying is also possible in some sense, but it does not look pythonic, neither do I find it necessary, however here is the code.你所说的在某种意义上也是可能的,但它看起来不像 Pythonic,我也觉得没有必要,但是这里是代码。

CAUTION : Modifying the list while iterating over it is a bad idea.注意:在迭代列表时修改列表是一个坏主意。

lst = ['code', 'this', 'code']
i = 0
while any(lst):
    if lst[i] == None:
        i += 1
        continue
    print(f"{lst[i]} is repeated {lst.count(lst[i])} times")
    lst = [None if j == lst[i] else j for j in lst]
    i += 1

Output same.输出相同。

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

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