-3

I want to generate 4 random numbers from 1 to 50. And I want to define a function of this random code generator n times. And save each random 4 number in a successive list names.

def eko():
    print(random.sample(range(1, 50), 4))

def eko_times(times):
    for i in range(times):eko()

for example, when I type eko_times(2) I want to see this result:

eko_1:[1,43,23,3]
eko_2:[8,32,34,2]

how can I save this outputs as new list names as eko_i?

3 Answers3

0

Comments inline

def eko_times(times, low, high, n):
    out = [] # list for collecting results
    for _ in range(times): # you don't need the value of i
        out.append(random.sample(range(low, high+1), n))
    return out

Now you can access the individual results as

>>> results = eko_times(3, 1, 50, 4)    
>>> results[0]
[10, 40, 11, 25]
RichieV
  • 5,103
  • 2
  • 11
  • 24
  • `eko` returns `None`, so you'll need to fix that first. BTW, it'd be better to use a list comprehension than an explicit loop. – wjandrea Sep 14 '20 at 00:21
  • @wjandrea you caught me on an edit, and yes a list comprehension is just a bit more performant in this case, but I try not to change the code too much with new users – RichieV Sep 14 '20 at 00:23
  • I misstyped first code I had updated the new. I'm sorry. – Erkan AĞASLAN Sep 14 '20 at 00:27
  • I mean a list comp would be more natural, since the loop doesn't do anything but build the list. I don't worry about performance on newbie questions. (For example, `eko_times` should be a generator for top performance, but that's too advanced. Also you can move the `range` out of the loop.) – wjandrea Sep 14 '20 at 00:28
  • @Erkan let me know when you test it – RichieV Sep 14 '20 at 00:38
  • Yes I want to achieve this result. But I am really newbie. I just wanted to save this results as list names. In the page you have suggested has a lot of answers and a lot of responses. I am not good at understand the codes. I wanted to create a list name with this function and wanted to store results in this list names. I am trying to learn Python by udemy lessons. But its very difficult when you are beginning of the road. Thanks for your answers. @wjandrea – Erkan AĞASLAN Sep 14 '20 at 00:47
  • can u help me? @wjandrea – Erkan AĞASLAN Sep 14 '20 at 01:28
  • @Erkan With what, sorry? – wjandrea Sep 14 '20 at 01:31
  • I just wanted to save this results as list names. I need to update my codes with your suggestion. I want to save this results in each list with variable names. The page everyone suggested is very hard for me to understand. I need a code saving my each 4 number to list named "eko_i" @wjandrea – Erkan AĞASLAN Sep 14 '20 at 01:40
  • @Erkan See the [top answer](https://stackoverflow.com/a/1373185/4518341) on that question. In short, you should use a list instead, just like Richie is suggesting here. – wjandrea Sep 14 '20 at 02:00
-1

It's been answered here.

However, I wouldn't really recommend it since it wouldn't be space efficient. Just create a list with each element being the result of eko() function.

  • *What* wouldn't be space efficient? The [top answer](https://stackoverflow.com/a/1373185/4518341) on that question says *"For cases where you're thinking of doing something like `var1 = 'foo'; var2 = 'bar'; var3 = 'baz'; ...` a list may be more appropriate than a dict"*. Also, `eko` returns `None`, so you'll need to fix that first. – wjandrea Sep 14 '20 at 00:22
  • Yes I want to achieve this result. But I am really newbie. I just wanted to save this results as list names. – Erkan AĞASLAN Sep 14 '20 at 00:42
-1

You can return a list of dicts specifying the names of your dicts based off the iteration range you provide.

This uses a few approaches and as a beginner you may want to do some reading on the following.

F-Strings (Python 3+), List comprehension, Dictionaries

def eko(times):
    return [{f"eko_{i + 1}": random.sample(range(1, 50), 4)} for i in range(times)]

eko_list = eko(2)
#[{'eko_1': [33, 29, 6, 17]}, {'eko_2': [43, 12, 34, 35]}]

You can then access individual response via eko_list[0]['eko_1']

[33, 29, 6, 17]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
PacketLoss
  • 5,561
  • 1
  • 9
  • 27
  • Don't use a list of dicts for this. It would make a bit more sense to use just a dict, but where the keys are ordered, just use a list: `[random.sample(range(1, 50), 4) for i in range(times)]` – wjandrea Sep 14 '20 at 00:37