首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python IF IN语句with list

Python IF IN语句with list
EN

Stack Overflow用户
提问于 2018-06-10 03:58:42
回答 3查看 162关注 0票数 2

我确信这是一个简单的解决方案,我能得到一点帮助吗?我在这里和网上都找过了,也没能解决这个问题。我刚开始学习还不到一周,基本上已经给自己设置了一个小任务来学习一些基础知识。

import pandas as pd

df1 = pd.read_csv('booking.csv', names=['snum','booked','name'])
df1.drop(['booked', 'name'], axis=1, inplace=True)

df2 = df1.values.tolist()

print('The following tickets are available; %s' % df2)
tic = input('Which ticket would you like to buy? ')

if tic in df2:
    print('Ok')
else:
    print('Ticket Unavilable')

我遇到的问题是If语句。无论我使用什么值作为输入,我总是得到消息'Ticket Unavailable‘。我相信错误一定出在从数据帧转换过来的列表上。

到目前为止,我已经做到了;

  • 测试IF语句时使用的是一个尚未转换或导入的列表,该列表作为df2变量类型工作,以确认它是一个列表

  • df2变量出现在打印的问题中,因此我知道它们已在ok

  • 副本中导入和转换,并粘贴到另一个Python文件中,结果与

相同

这些变量是基本座位号A1、A2、A3、A4、A5、B1、B2、B3、B4、B5。我知道输入'A‘'B’ect也会返回'OK',实用性不如功能性重要。

EN

回答 3

Stack Overflow用户

发布于 2018-06-10 04:03:08

看起来pandas.DataFrame.values.tolist并没有产生您认为它所做的事情:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame.from_records([dict(a=1), dict(a=2)])

In [3]: df
Out[3]: 
   a
0  1
1  2

In [4]: df.values.tolist()
Out[4]: [[1], [2]]

在本例中,tic是一个字符串,而df.values.tolist()是一个嵌套列表。我想,你想要的是:

df2 = set(df1['snum'])

我使用了一个集合,因为哈希表更适合查找。

票数 2
EN

Stack Overflow用户

发布于 2018-06-10 04:14:52

看起来您没有使用正确的DataFrame方法来获取票证列表。使用DataFrame.to_dict('records')简化票证名称的提取。

booking.csv

92747,true,Harry Potter
28479,false,Sherlock Holmes

Python代码

import pandas as pd

# Load ticket list from CSV file
booking_df = pd.read_csv('booking.csv', names=['snum','booked','name'])

# Convert ticket list to a list of built-in Python dictionaries
ticket_list = booking_df.to_dict('records')

# Extract the ticket names from the list of tickets
ticket_name_set = {ticket["name"] for ticket in ticket_list}

print('The following tickets are available; %s' % ticket_name_set)
wanted_ticket_name = input('Which ticket would you like to buy? ')

if wanted_ticket_name in ticket_name_set:
    print('Ok')
else:
    print('Ticket Unavilable')

输出:

➜ python tickets.py
The following tickets are available; ['Harry Potter', 'Sherlock Holmes']
Which ticket would you like to buy? Harry Potter
Ok
➜ python tickets.py
The following tickets are available; ['Harry Potter', 'Sherlock Holmes']
Which ticket would you like to buy? Hamlet
Ticket Unavilable
票数 0
EN

Stack Overflow用户

发布于 2018-06-10 04:22:10

变化

if tic in df2:

if any(tic in s for s in df2):

您有一个列表,但是您试图像访问Panda Dataframe一样访问它。

booking.csv

snum    booked  name
a1      no  
a2      no  
a3      no  
a4      no  
a5      no  
a6      no  
a7      no  
a8      no  
a9      no  
a10     no  
a11     no
a12     no
a13     no
b1      no
b2      no
b3      no
b4      no
b5      no
b6      no
b7      no
b8      no
b9      no
b10     no
b11     no
b12     no
b13     no

修改后的示例

import pandas as pd

df1 = pd.read_csv('booking.csv', names=['snum','booked','name'])
df1.drop(['booked', 'name'], axis=1, inplace=True)

df2 = df1.values.tolist()

print('The following tickets are available; %s' % df2)
tic = input('Which ticket would you like to buy? ')

if any(tic in s for s in df2):#df2.str.contains(tic):
    print('Ok')
else:
    print('Ticket Unavilable')

给出以下输出

The following tickets are available; [['snum'], ['a1'], ['a2'], ['a3'], ['a4'], ['a5'], ['a6'], ['a7'], ['a8'], ['a9'], ['a10'], ['a11'], ['a12'], ['a13'], ['b1'], ['b2'], ['b3'], ['b4'], ['b5'], ['b6'], ['b7'], ['b8'], ['b9'], ['b10'], ['b11'], ['b12'], ['b13']]

Which ticket would you like to buy? a1
Ok

The following tickets are available; [['snum'], ['a1'], ['a2'], ['a3'], ['a4'], ['a5'], ['a6'], ['a7'], ['a8'], ['a9'], ['a10'], ['a11'], ['a12'], ['a13'], ['b1'], ['b2'], ['b3'], ['b4'], ['b5'], ['b6'], ['b7'], ['b8'], ['b9'], ['b10'], ['b11'], ['b12'], ['b13']]

Which ticket would you like to buy? c2
Ticket Unavilable
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50778026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档