如何找出一个lis中有多少个“name”

2024-09-22 16:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我为telegram创建了一个bot,遇到了计算要显示内联键盘的值的数目的问题。我有一个列表和数据,可以改变,我需要在键盘上所有namesaddresses在内联,只有我想这个键盘调整到值的数量(我知道如何创建内联按钮,显示所有的名称和地址)。但我需要每个按钮都有自己的回调)

text=[{'id': 'fd65865b-0f43-468b-80a5-04d5c9f90086', 'vendorId': '232231', 'name': 'магазин-кафетерій', 'location': {'settlement': 'Київ', 'addressLine': 'м.Київ вул.А.Ахматової 35'}, 'freeVisit': False}, {'id': '2a2d898c-853c-453b-a8c3-0acd0e1ebc8a', 'vendorId': '232830', 'name': 'магазин', 'location': {'settlement': 'Київ', 'addressLine': 'м.Київ вул.Радунська,13-А'}, 'freeVisit': False}.....]

selectKeyboard = telebot.types.InlineKeyboardMarkup( row_width=1)
lenname=[i.split("name")[0] for i in text]
    print(lenname)

if 0<lenname<2:
    for i in range(len(text)):
        one=types.InlineKeyboardButton(text=str(text[0]['name']),callback_data="first")

    selectKeyboard.add(one)

if 1<lenname<3:
    for i in range(len(text)):
        one=types.InlineKeyboardButton(text=str(text[0]['name'])+"  ",callback_data="first")
        two=types.InlineKeyboardButton(text=str(text[1]['name'])+"  ",callback_data="second")

    selectKeyboard.add(one,two)

if 2<lenname<4:
    for i in range(len(text)):
        one=types.InlineKeyboardButton(text=str(text[0]['name'])+"  ",callback_data="first")
        two=types.InlineKeyboardButton(text=str(text[1]['name'])+"  ",callback_data="second")
        three = types.InlineKeyboardButton(text=str(text[2]['name']) + "  " ,callback_data="three")

    selectKeyboard.add(one,two,three)


And ....

我期望动态键盘的输出,但实际输出是1个内联按钮


Tags: textnameinfordatacallback键盘按钮
1条回答
网友
1楼 · 发布于 2024-09-22 16:36:57

我知道怎么做。我总结了所有的i

for i in range(len(text)):
    name = text[i]['name']
    vendor = text[i]['vendorId']
    address=text[i]['location']['addressLine']
    pprint(name)

    sumaI+=i

print(sumaI)


selectKeyboard = telebot.types.InlineKeyboardMarkup( row_width=1)

if sumaI==0:
    for i in range(len(text)):
        one=types.InlineKeyboardButton(text=str(text[0]['name'])+"  "+str(text[0]['location']['addressLine']),callback_data="first")

    selectKeyboard.add(one)

elif  sumaI==1:
    for i in range(len(text)):
        one=types.InlineKeyboardButton(text=str(text[0]['name'])+"  "+str(text[0]['location']['addressLine']),callback_data="first")
        two=types.InlineKeyboardButton(text=str(text[1]['name'])+"  "+str(text[1]['location']['addressLine']),callback_data="second")

    selectKeyboard.add(one,two)

elif sumaI==3:
    for i in range(len(text)):
        one=types.InlineKeyboardButton(text=str(text[0]['name'])+"  "+str(text[0]['location']['addressLine']),callback_data="first")
        two=types.InlineKeyboardButton(text=str(text[1]['name'])+"  "+str(text[1]['location']['addressLine']),callback_data="second")
        three = types.InlineKeyboardButton(text=str(text[2]['name']) + "  " + str(text[2]['location']['addressLine']),
                                         callback_data="three")

    selectKeyboard.add(one,two,three)

相关问题 更多 >