Problems that arise when compliance is converted into a general for statement

Asked 2 years ago, Updated 2 years ago, 347 views

# Create Dictionary 
loc_mapping = {val : index for index, val in enumerate(strings)} # It doesn't mean val = index, but it means to express it in the form of val : index.
loc_mapping

When you run , you get the following result values:

{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}

So I've just converted the code above that I've been compiling into a for statement.

for index, val in enumerate(strings):
    loc_mapping = {val:index} #val = index does not mean, but it means to express it in the form of val:index
    print(loc_mapping)
{'a': 0}
{'as': 1}
{'bat': 2}
{'car': 3}
{'dove': 4}
{'python': 5}

This is the result.

Like the result of the first code, I want to put all of it in one dictionary of

I'd appreciate it if you could tell me how to do it!

python for

2022-10-15 09:43

1 Answers

loc_mapping = dict()
for index, val in enumerate(strings):
    loc_mapping[val] = index #val = index, but it means to express it in the form of val: index.
print(loc_mapping)


2022-10-15 09:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.