Home > Net >  Use sets and lists in python to create a new list
Use sets and lists in python to create a new list

Time:11-05

I have the following data that is dynamic, but below is a sample of what it might be:

# An incoming List of lists with only ints
# For exampe the incoming_list could be:
incoming_list = [[1,2]. [3]]

# The indexes are like so:
0: [1,2]
1: [3]

I then have a check_list that has some example data (will be dynamic) like so:

# A List[int] for check_list
check_list= [3]

I then need to get the first int on the incoming_list if any of the incoming lists data is in it's indexes:

# If the following were input:
incoming_list = [[1,2]. [3]]
check_list= [3]

# Then a new_list would be:
new_list = [3] because incoming_list has a list with 3 in it, and the 
first element of that list is 3

##############################################################

# Another example ff the following were input:
incoming_list = [[1,2]. [3]]
check_list= [2,3]

# Then a new_list would be:
new_list = [1,3] because incoming_list has a 2 in the first index and 
its first value is 1 and because incoming list has a 3 in the second index 
and its first and only value is 3

I was try to do this with a set list combo but I think the List of List part is messing it up:

new_list = list(set(v for k, v in incoming_lists if int(k) in check_list))

Any ideas how to make these clean and elegant?

CodePudding user response:

In the general case it might make sense to preprocess the list into an O(1) lookup structure which can be done in linear time:

lookup = {}
for lst in reversed(incoming_list):
    for x in lst:
        lookup[x] = lst[0]

Then you can simply use this:

result = [lookup[x] for x in check_list]

CodePudding user response:

One simple way to achieve the desired result for the given inputs in questions. In case if you're looking for 1 line statement.

[l[0] for v in check_list for l in incoming_list if v in l]

In [33]: # EXAMPLE 1                                                                                                                                                                                        

In [34]: incoming_list = [[1,2], [3]]                                                                                                                                                                       

In [35]: check_list= [3]                                                                                                                                                                                    

In [36]: [l[0] for v in check_list for l in incoming_list if v in l]                                                                                                                                        
Out[36]: [3]

In [37]: # EXAMPLE 2                                                                                                                                                                                        

In [38]: incoming_list = [[1,2], [3]]                                                                                                                                                                       

In [39]: check_list= [2,3]                                                                                                                                                                                  

In [40]: [l[0] for v in check_list for l in incoming_list if v in l]                                                                                                                                        
Out[40]: [1, 3]

In [41]:  

You can update the single line statement for the cases where we have the changes of duplicates in final result.

CodePudding user response:

Although it's not very clear what you mean, I think this should be able to work.

new_list = []
check = set(check_list)

for sublist in incoming_list:
    for i in sublist:
        if i in check and sublist[0] not in new_list:
            new_list.append(sublist[0])
            continue

print(new_list)

Output

Let's try out different values of incoming_list and check_list.

incoming_list = [[1, 2], [3]]
check_list = [2]

# Result: [1]
incoming_list = [[1, 2], [3], [2, 3, 5], [7, 8], [4, 3]]
check_list = [3]

# Result: [3, 2, 4]
  • Related