Comparing list with a list of lists

2024/11/14 18:25:31

I have a list string_array = ['1', '2', '3', '4', '5', '6'] and a list of lists multi_list = [['1', '2'], ['2', '3'], ['2', '4'], ['4', '5'], ['5', '6']]

The first element of each sub-list in multi_list will have an associated entry in string_array.

(Sublists will not have duplicate elements)

How do I map these associated elements into a dictionary like:

{
'1': ['2'],
'2': ['3', '4'],
'3': [],
'4': ['5'],
'5': ['6'],
'6': []
}
Answer

Here's a few concepts that will help you, but I'm not going to give you the complete solution. You should do so on your own to sink in the concepts.

To make an empty dictionary of lists

{'1': [],'2': [],'3': [],'4': [],'5': [],'6': [],
}

you can use a for loop:

list_one = ['1', '2', '3', '4', '5', '6']my_dict = {}
for value in list_one:my_dict[value] = []

You could also get fancy and use a dictionary comprehension:

my_dict = {value: [] for value in list_one}

Now you'll need to loop over the second list and append to the current list. e.g. to append to a list you can do so in a few ways:

a = [1,2]
b = [3,4]
c = 5# add a list to a list
a += b
# now a = [1,2,3,4]# add a list to a list
b.append(c)
# now b = [3,4,5]

To chop up lists you can use slice notation:

a = [1,2,3,4]
b = a[:2]
c = a[2:]
# now b = [1,2], and c = [3,4]

And to access an item in a dictionary, you can do so like this:

a = { '1': [1,2,3] }
a['1'] += [4,5,6]
# now a = { '1': [1,2,3,4,5,6] }
https://en.xdnf.cn/q/119099.html

Related Q&A

Cannot save data to database Python

I have a table called category TABLES["category"] = ("""CREATE TABLE category (category_id INTEGER NOT NULL AUTO_INCREMENT,category_name VARCHAR(120) NOT NULL,PRIMARY KEY (cate…

How to generate a permutation of list of lists in python

I have a list of lists say[[2, 4, 6], [2, 6, 10], [2, 12, 22], [4, 6, 8], [4, 8, 12], [6, 8, 10], [8, 10, 12], [8, 15, 22], [10, 11, 12]]How do I generate a combination of the lists for a given length?…

Issue sending file via Discord bot (Python)

if message.content.upper().startswith("!HEADPATS"):time.sleep(1)with open(tenor.gif, rb) as picture:await client.send_file(channel, picture)Ive got my discord bot up and running (everythings …

Matplotlib installation on Mavericks

Im having problem while installing matplotlib. Im using Mavericks and it complains about a deprecated NumPy API both installing via pip and installing from source (following the instructions here https…

Exact string search in XML files?

I need to search into some XML files (all of them have the same name, pom.xml) for the following text sequence exactly (also in subfolders), so in case somebody write some text or even a blank, I must …

Integrate a function by the trapezoidal rule- Python

Here is the homework assignment Im trying to solve:A further improvement of the approximate integration method from the last question is to divide the area under the f(x) curve into n equally-spaced tr…

Kivy module not found in vscode (Mac)

I have installed Kivy and when I used the IDLE app that came with Python I can import it and it runs perfectly. However, when I try to import it in vscode I get the error: ModuleNotFoundError: No modul…

How to get latest unique entries from sqlite db with the counter of entries via Django ORM

I have a SQLite db which looks like this:|ID|DateTime|Lang|Details| |1 |16 Oct | GB | GB1 | |2 |15 Oct | GB | GB2 | |3 |17 Oct | ES | ES1 | |4 |13 Oct | ES | ES2 | |5 |15 Oct | ES | ES3 …

What does this code %.8f% do in python? [duplicate]

This question already has answers here:What does % do to strings in Python? [duplicate](4 answers)Closed 6 years ago.I am editing a code line to pass the rate in quotes:OO000OO00O0O0O000 [rate]=O0O0OO…

How to append a selection of a numpy array to an empty numpy array

I have a three .txt files to which I have successfully made into a numpy array. If you are curious these files are Level 2 data from the Advanced Composition Experiment (ACE). The particular files are …