Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

python - Selecting a random list and then pulling information from that list

I know how to append and access random data from a list. I'm trying to randomize one of the Lists (A, B, or C), then use data from those lists.

Is it as simple as changing random_list to match the actual List_A (or b/c)? Or am I going about it all wrong?

import random

All_Lists = ["List_A", "List_B", "List_C"]
List_A = ["Albert", "Apple", "3", "Alpaca", "4"]
List_B = ["Barbara", "Banana", "6", "Baboon", "3"]
List_C = ["Calvin", "Carrot", "1", "Cat", "0"]

rand_idy = int(random.random() * len(All_Lists))
random_list = All_Lists[rand_idy]
print(random_list)
question from:https://stackoverflow.com/questions/66052980/selecting-a-random-list-and-then-pulling-information-from-that-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change All_Lists as follows, and move it to after the other lists:

All_Lists = [List_A, List_B, List_C]

Note the lack of quotation marks.

In general, you don't reference Python variables by strings that name them. See also: How do I create variable variables?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...