Reputation: 169
Say I have a list of words with repeats:
["Apple", "Orange", "Grape", "Orange", "Watermelon", "Apple", "Grape"]
I need to put them into a dictionary so that each word has its own key in an ascending order:
{1 : "Apple", 2 : "Orange", 3 : "Grape", 4: "Watermelon"}
The order is based on which word appears first. If "Apple" appears first, it will have the key 1
and if "Apple" appears in the future, it will be ignored, since "Apple" is already included in 1. If "Orange" appears after "Apple", it will have the key 2
. Thus, the key-value pairs are added in an ascending order.
I don't have a problem with adding and checking repeats, but I'm struggling with making the keys in an ascending order. Any clues?
Upvotes: 0
Views: 233
Reputation: 2912
fruits = ["Apple", "Orange", "Grape", "Orange", "Watermelon", "Apple", "Grape"]
basket = []
for i in range(len(fruits)):
if fruit[i] not in basket:
basket.append(fruit)
This doesn't get you a dictionary, but it shouldn't matter, because the elements are accessed the same way: basket[2] == "Grape"
either way, assuming you correct for arrays being 0-indexed while your desired dict layout is 1-indexed.
Upvotes: 0
Reputation: 2281
Edited after the discussion in the comments:
A pythonic way to check if a key exists in a dictionary:
fruits = ['Apple', 'Orange', 'Grape', 'Orange', 'Watermelon', 'Apple', 'Grape']
index = 1
basket = {}
for fruit in fruits:
if fruit not in basket:
basket[fruit] = index
index = index + 1
Upvotes: 0
Reputation: 369
You can convert your list
into a set
which eliminates redundant entries but you lose your initial list order:
fruit = set(["Apple", "Orange", "Grape", "Orange", "Watermelon", "Apple", "Grape"])
The object fruit
becomes:
{'Apple', 'Grape', 'Orange', 'Watermelon'}
I find it easier to have the iteration index starting at 1 for cases like these. If you would like to sort your fruits before assigning them to numerical keys in a dictionary you will need to to apply the sorted()
function to your fruit set for an alphabetical sort (you can also setup a parameter to sort in other ways using the sorted()
function).
basket = {}
for i, item in enumerate(sorted(fruit), start=1):
basket[i] = item
print basket
Printed result:
{1: 'Apple', 2: 'Grape', 3: 'Orange', 4: 'Watermelon'}
Upvotes: 1
Reputation: 353059
If you don't mind an import:
>>> from collections import OrderedDict
>>> s = ["Apple", "Orange", "Grape", "Orange", "Watermelon", "Apple", "Grape"]
>>> dict(enumerate(OrderedDict.fromkeys(s), 1))
{1: 'Apple', 2: 'Orange', 3: 'Grape', 4: 'Watermelon'}
This works because OrderedDict.fromkeys
makes an ordered dictionary where the order is insertion order:
>>> OrderedDict.fromkeys(s)
OrderedDict([('Apple', None), ('Orange', None), ('Grape', None), ('Watermelon', None)])
Upvotes: 5
Reputation: 27792
Just maintain an index and check each fruit against the values of the dictionary.
fruits = ["Apple", "Orange", "Grape", "Orange", "Watermelon", "Apple", "Grape"]
d = {}
index = 1
for fruit in fruits:
if fruit not in d.values():
d[index] = fruit
index += 1
Will give you:
>>> d
{1: 'Apple', 2: 'Orange', 3: 'Grape', 4: 'Watermelon'}
Upvotes: 3