Reputation: 45
I want to create a dictionary from a list I created beforehand.
For example:
import random
my_randoms = random.sample(range(1, 1001), 1000)
print(my_randoms)
#is this the correct thought? I would need to use .append but where? on the random.sample, right?
def my_randoms():
new_dictionary = {}
import random
random.sample(range(1,1001),1000)
return new_dictionary
print(my_randoms())
Then, the dictionary should have been created and now I need to implement the code so that with each a figure becomes smaller that the figure before.
I know how to do something similar with increasing figures.
For example:
def stars(n):
result = ""
# Count line I am on
line = 1 # If I start with 10. It starts with 10 stars, so if I have a dictionary with 1000 numbers, I should start at 1000 right?
while line <= n:
star_count = line
gathered = 0 # As I start with 1000 this must be 1000 as well, right?
# Gather as many stars as the line number we are on
while gathered < star_count:
result = result + "*" # this code gathers more. Does it decrease with "-"? I tried it, but it didn't work..
gathered = gathered + 1
# New line
result += "\n" # this must stay the same, as we intend to create new lines, right?
line = line + 1
return result
print(stars(4))
Can someone help me here?
Upvotes: 1
Views: 66
Reputation: 7988
what you might want is to first build the data structure as a list of tuples:
start_data = [("A", 1), ("B", 2), ("X", 99), ("G", 33), ("D", -5)]
then sort it and convert to a dictionary:
as_dict = dict(sorted(start_data))
resulting in:
{'A': 1, 'B': 2, 'D': -5, 'G': 33, 'X': 99}
It's true that as of python 3.7 dictionaries will reflect the insert order of new elements, but dictionaries are still inherently unordered objects, so trying to use native dictionaries and sort the elements is going to be kludgy. Another option is to use OrderedDict:
from collections import OrderedDict
od = OrderedDict(start_data)
od_sorted = sorted(od.items())
which gives the same result:
[('A', 1), ('B', 2), ('D', -5), ('G', 33), ('X', 99)]
Which option you choose really is only a matter of preference. I never end up using OrderedDict
and whenever I try I end up going back to normal dictionaries. I think it adds an unnecessary layer of complexity that is no longer needed as of Python 3.7. Some people will probably disagree, and that's fine; as I said, at this point it's mostly just a personal preference.
For the problem you described, I think you could do it in one line, like this1:
s = [*map(lambda x: ("*" * x) + "\n", range(1000, 0, -1))]
s
will contain strings of *
of random length between 1 and 1000, sorted in decreasing order of length, and terminated with a newline2.
or, as a single string:
s = "".join(map(lambda x: ("*" * x) + "\n", range(1000, 0, -1)))
1: Using random.sample(range(1, 1001), 1000)
is really pointless. sample(population, k)
will choose k
unique index positions (not necessarily unique elements) from population
. This means that when len(population) == k
(as is the case here) the result is just the the original set.
2: I'm using python's [*iterable]
syntax here to expand the map, but this is for illustrative reasons only. In practice, always leave a map
as a map
(or, more broadly, leave a generator as a generator) until you actually need to use it.
Upvotes: 1