简体   繁体   English

您如何更改集合中的特定元素?

[英]How do you change a specific element of a set?

In this code, I am trying to compare the value of a set that has been looped each time to a value passed (in this case a) in a parameter. 在这段代码中,我试图将每次循环的集合的值与参数中传递的值(在本例中为a)进行比较。 What's interesting though is that it shows when I use a for each loop that each element is an integer. 但是有趣的是,它显示了当我为每个循环使用a时,每个元素都是整数。 How do I get a integer to integer comparison without a console error? 如何获得没有控制台错误的整数到整数比较?

def remove(s,a,b):
    c=set()
    c=s
    for element in c:
        element=int(element)
        if(element<a or element>b):
            c.discard(element)
    return c

def main():
    remove({3, 17, -1, 4, 9, 2, 14}, 1, 10)

main()

Output: 输出:

    if(element<=a or element>=b):
TypeError: '>=' not supported between instances of 'int' and 'set'

You reassign your local variable b : 您重新分配本地变量b

def remove(s,a,b):
    b=set()  # now b is no longer the b you pass in, but an empty set
    b=s  # now it is the set s that you passed as an argument
    # ...    
    if(... element > b): # so the comparison must fail: int > set ??

Short implementation using a set comprehension: 使用集合理解的简短实现:

def remove(s, a, b):
    return {x for x in s if a <= x <= b}

>>> remove({3, 17, -1, 4, 9, 2, 14}, 1, 10)
{9, 2, 3, 4}

if you want int to int compare then make b as list of s. 如果要int进行int比较,则将b作为s的列表。

def remove(s,a,b):
    b = list(s)
    for element in s:
        element=int(element)      
        if(element< a or element > b):
            b.remove(element)
    return b

def main():
    remove({3, 17, -1, 4, 9, 2, 14}, 1, 10)

main()

Come on, why don't we make the code shorter? 来吧,为什么我们不缩短代码呢?

Try this: 尝试这个:

def remove(s, a, b):
    return s.difference(filter(lambda x: not int(a) < int(x) < int(b), s))


def main():
    new_set = remove({3, 17, -1, 4, 9, 2, 14}, 1, 10)

    # {2, 3, 4, 9}
    print(new_set)


main()

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

相关问题 你如何为python中的特定输入元素收集数据? - How do you collect data for a specific input element in python? 如何在python中更改字符的特定数量 - how do you change a specific number of a character in python 如何在Pandas DF中将具有特定值的特定列设置为新值? - How do you set a specific column with a specific value to a new value in a Pandas DF? 如何在key:value对的values部分中定位特定元素? - How do you target a specific element inside a values portion of a key:value pair? 在 Graphene 中,如何同时提供一个列表和一个特定元素? - In Graphene, how do you provide both a list and a specific element from one? 如果在所有子列表中的特定位置找不到某个元素,如何删除子列表? - How do you remove a sublist if a certain element is not found at a specific position across all sublists? BeautifulSoup 如何获取包含特定文本的父元素标签? 试图抓取 email 但无法拾取父元素标签 - BeautifulSoup How do you get the parent element tag that contains a specific text? Trying to scrape email but unable to pick up the parent element tag 如何在特定点拆分字符串? - How do you split a string at a specific point? 如何更改矩阵中的一个特定元素? - How to change one specific element in a matrix? 你如何在python中定义一个特定的标签 - How do you define a specific label in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM