Appending an id to a list if not already present in a string

前端 未结 8 844
醉话见心
醉话见心 2021-02-02 05:45

I am trying to check if id is in a list and append the id only if its not in the list using the below code..however I see that the id is getting appended even though id is alre

相关标签:
8条回答
  • 2021-02-02 06:03

    I agree with other answers that you are doing something weird here. You have a list containing a string with multiple entries that are themselves integers that you are comparing to an integer id.

    This is almost surely not what you should be doing. You probably should be taking input and converting it to integers before storing in your list. You could do that with:

    input = '350882 348521 350166\r\n'
    list.append([int(x) for x in input.split()])
    

    Then your test will pass. If you really are sure you don't want to do what you're currently doing, the following should do what you want, which is to not add the new id that already exists:

    list = ['350882 348521 350166\r\n']
    id = 348521
    if id not in [int(y) for x in list for y in x.split()]:
        list.append(id)
    print list
    
    0 讨论(0)
  • 2021-02-02 06:04

    Your id variable is a number where your list only has one element. It's a string that contains your other IDs. You either need to check if id is in that string, or pull the numbers out of the string and store them in the list separately

    list  = [350882, 348521, 350166]
    
    0 讨论(0)
  • 2021-02-02 06:05

    What you are trying to do can almost certainly be achieved with a set.

    >>> x = set([1,2,3])
    >>> x.add(2)
    >>> x
    set([1, 2, 3])
    >>> x.add(4)
    >>> x.add(4)
    >>> x
    set([1, 2, 3, 4])
    >>> 
    

    using a set's add method you can build your unique set of ids very quickly. Or if you already have a list

    unique_ids = set(id_list)
    

    as for getting your inputs in numeric form you can do something like

    >>> ids = [int(n) for n in '350882 348521 350166\r\n'.split()]
    >>> ids
    [350882, 348521, 350166]
    
    0 讨论(0)
  • 2021-02-02 06:05

    A more pythonic way, without using set is as follows:

    lst = [1, 2, 3, 4]
    lst.append(3) if 3 not in lst else lst
    
    0 讨论(0)
  • 2021-02-02 06:08

    7 years later, allow me to give a one-liner solution by building on a previous answer. You could do the followwing:

    numbers = [1, 2, 3]
    

    to Add [3, 4, 5] into numbers without repeating 3, do the following:

    numbers = list(set(numbers + [3, 4, 5]))
    

    This results in 4 and 5 being added to numbers as in [1, 2, 3, 4, 5]

    Explanation:

    Now let me explain what happens, starting from the inside of the set() instruction, we took numbers and added 3, 4, and 5 to it which makes numbers look like [1, 2, 3, 3, 4, 5]. Then, we took that ([1, 2, 3, 3, 4, 5]) and transformed it into a set which gets rid of duplicates, resulting in the following {1, 2, 3, 4, 5}. Now since we wanted a list and not a set, we used the function list() to make that set ({1, 2, 3, 4, 5}) into a list, resulting in [1, 2, 3, 4, 5] which we assigned to the variable numbers

    This, I believe, will work for all types of data in a list, and objects as well if done correctly.

    0 讨论(0)
  • 2021-02-02 06:12

    There are a couple things going on with your example. You have a list containing a string of numbers and newline characters:

    list = ['350882 348521 350166\r\n']
    

    And you are trying to find a number ID within this list:

    id = 348521
    if id not in list:
        ...
    

    Your first conditional is always going to pass, because it will be looking for integer 348521 in list which has one element at index list[0] with the string value of '350882 348521 350166\r\n', so integer 348521 will be added to that list, making it a list of two elements: a string and an integer, as your output shows.

    To reiterate: list is searched for id, not the string in list's first element.

    If you were trying to find if the string representation of '348521' was contained within the larger string contained within your list, you could do the following, noting that you would need to do this for each element in list:

    if str(id) not in list[0]: # list[0]: '350882 348521 350166\r\n'
        ...                    #                  ^^^^^^
    

    However be aware that you would need to wrap str(id) with whitespace for the search, otherwise it would also match:

    2348521999
     ^^^^^^
    

    It is unclear whether you want your "list" to be a "string of integers separated by whitespace" or if you really want a list of integers.

    If all you are trying to accomplish is to have a list of IDs, and to add IDs to that list only if they are not already contained, (and if the order of the elements in the list is not important,) then a set would be the best data structure to use.

    ids = set(
        [int(id) for id in '350882 348521 350166\r\n'.strip().split(' ')]
    )
    
    # Adding an ID already in the set has no effect
    ids.add(348521)
    

    If the ordering of the IDs in the string is important then I would keep your IDs in a standard list and use your conditional check:

    ids = [int(id) for id in '350882 348521 350166\r\n'.strip().split(' ')]
    
    if 348521 not in ids:
        ...
    
    0 讨论(0)
提交回复
热议问题