提问人:Shehzad Ahmad 提问时间:10/14/2023 更新时间:10/15/2023 访问量:71
我被困在调用字典中的键值中,该键位于列表内,但具有随机变量。其输出与列表中的输出不同
I am stuck in calling a value of a key inside of dictionary which is inside of list but with random variable. its output is not the same as in list
问:
我试图制作一个随机数,然后该随机数将用于从列表数据内的字典中调用follower_count,但我无法为随机生成的数字获得相同的关注者,甚至没有接近,关注者也是随机选择的
import random
data = [
{
'name': 'Instagram',
'follower_count': 346,
'description': 'Social media platform',
'country': 'United States'
},
{
'name': 'Cristiano Ronaldo',
'follower_count': 215,
'description': 'Footballer',
'country': 'Portugal'
},
{
'name': 'Ariana Grande',
'follower_count': 183,
'description': 'Musician and actress',
'country': 'United States'
},
{
'name': 'Dwayne Johnson',
'follower_count': 181,
'description': 'Actor and professional wrestler',
'country': 'United States'
},
{
'name': 'Selena Gomez',
'follower_count': 174,
'description': 'Musician and actress',
'country': 'United States'
},
{
'name': 'Kylie Jenner',
'follower_count': 172,
'description': 'Reality TV personality and Self-Made Billionaire',
'country': 'United States'
}
]
def comparison(followers_A,followers_B):
if followers_A>followers_B:
return followers_A
else:
return followers_B
def number():
'''Generates Random numbers'''
return random.randint(0,length)
def follower(number):
'''Calls the follower in the list'''
return data[number-1]['follower_count']
length=len(data)
print(f"{length} its the length of list")
print(f"{number()} its random number")
number=number()
follower = follower(number)
print(f"{follower} its followers")
print(data[11]['follower_count'])
答:
1赞
Marcin Orlowski
10/14/2023
#1
您的代码存在多个问题:
- 您对函数和变量都使用相同的名称,这可能会导致意外行为。
number
follower
- 在 中,如果随机数是从 开始生成的,则可能会有问题。
data[number-1]['follower_count']
-1
0
- 您正在尝试访问 ,但您提供的列表较短(6 个元素),因此没有第 11 个元素会导致 。
data[11]['follower_count']
data
IndexError
- 数字函数不一致;它生成一个从 到 的随机整数,但列表索引从 开始,一直到 。
0
length
0
length - 1
- 函数未使用(死代码)。
comparison()
下面是已解决上述问题的重构代码:
import random
data = [
# ... (same as your data)
]
def generate_random_number(length):
'''Generates Random numbers within range of list length'''
return random.randint(0, length - 1)
def get_follower_count(index):
'''Calls the follower in the list by index'''
return data[index]['follower_count']
list_length = len(data)
random_index = generate_random_number(list_length)
follower_count = get_follower_count(random_index)
print(f"{list_length} is the length of list")
print(f"{random_index} is the random number")
print(f"{follower_count} is the follower count")
1赞
Codist
10/14/2023
#2
如果您使用 random.choice() 从列表中选择 [random] 字典,则可以使事情变得更容易。然后,您可以对所选词典执行任何您想做的事情:
# pseudo-randomly select a dictionary from the list
e = random.choice(data)
# print the follower_count
print(e['follower_count'])
0赞
Suramuthu R
10/14/2023
#3
我理解你的编码如下。 您有一个包含少量字典的列表。在列表中的字典总数中生成一个随机数。您的程序应该获取生成的列表随机数的数据。从选定的选定词典中,它应该返回关注者的名称和数量(除此之外,它还应该打印列表的长度和选定的随机数。 希望我的理解是正确的。
import random
data = [
{
'name': 'Instagram',
'follower_count': 346,
'description': 'Social media platform',
'country': 'United States'
},
{
'name': 'Cristiano Ronaldo',
'follower_count': 215,
'description': 'Footballer',
'country': 'Portugal'
},
{
'name': 'Ariana Grande',
'follower_count': 183,
'description': 'Musician and actress',
'country': 'United States'
},
{
'name': 'Dwayne Johnson',
'follower_count': 181,
'description': 'Actor and professional wrestler',
'country': 'United States'
},
{
'name': 'Selena Gomez',
'follower_count': 174,
'description': 'Musician and actress',
'country': 'United States'
},
{
'name': 'Kylie Jenner',
'follower_count': 172,
'description': 'Reality TV personality and Self-Made Billionaire',
'country': 'United States'
}
]
length=len(data)
number = random.randint(0,length)
dct = data[number-1]
Name = dct['name']
follower_count = dct['follower_count']
print(f"{length} its the length of list")
print(f"{number} its random number")
print(f"{Name} is his/her name")
print(f"{follower_count} are number of followers")
希望这会有用。
评论
number()