私信  •  关注

BernardL

BernardL 最近创建的主题
BernardL 最近回复了
7 年前
回复了 BernardL 创建的主题 » 如何在python程序中检测“space”?

空间的python表示是 u"\u0020" ,参考 here .

并测试:

if input('enter:') == u"\u0020":
    print('pass')
else:
    print('fail')
7 年前
回复了 BernardL 创建的主题 » python-检查列是否包含列表中的值,返回值

可以创建lambda函数来检查列表中的每个项:

d = {'id': [1,2,3,4,5,6,7,8,9,10],
      'text': ['bill did this', 'jim did something', 'phil', 'bill did nothing',
               'carl was here', 'this is random', 'other name',
               'other bill', 'bill and carl', 'last one']}
df = pd.DataFrame(data=d)

l = ['bill','carl']

df['contains'] = df['text'].apply(lambda x: ','.join([i for i in l if i in x]))

如果需要列表,可以删除join,否则它将只连接由逗号分隔的值。

产量

>>df['contains']

0         bill
1             
2             
3         bill
4         carl
5             
6             
7         bill
8    bill,carl
9             
Name: contains, dtype: object
7 年前
回复了 BernardL 创建的主题 » python:如何将csv数据转换为数组

读取文件并从中获取项目列表:

import csv

results = []

with open('some_array.csv','r') as f:
    lines = csv.reader(f)
    for line in lines:
        results.append([[int(i)] for i in line])

>>results
[[['11'], ['10'], ['8'], ['12'], ['13'], ['11']],
 [['0'], ['1'], ['0'], ['2'], ['3'], ['0']],
 [['5'], ['15'], ['13'], ['11'], ['18'], ['18']]]