df[[y.lower() in x.lower() for x, y in zip(df['everything'], df['searching_for'])]]
上面的代码显示输出:
df[[y.lower() in x.lower()
for x, y in df[['everything', 'searching_for']].values.tolist()]]
file_names searching_for everything
0 a.txt where Dave Ran Away. Where is Dave?
1 a.txt candy mmmm, candy
3 b.txt where where the red fern grows
但是我希望它是:
import json
data = ['A + G', 'B + F', 'C + E', 'D + D']
print(json.dumps(data, indent=0))
我尝试更改Json的缩进,但这不起作用。我在哪里错了?
答案 0 :(得分:1)
如果仅将json
用于打印输出,则可以跳过它,并通过以下方式获得所需的输出:
data = ['A + G', 'B + F', 'C + E', 'D + D']
for i, d in enumerate(data):
if (i == 0):
print('["{}"'.format(d))
elif (i == len(data) - 1):
print(' "{}"]'.format(d))
else:
print(' "{}"'.format(d))
# OUTPUT
# ["A + G"
# "B + F"
# "C + E"
# "D + D"]
答案 1 :(得分:1)
print('['+json.dumps(data, indent=1)[3:-2]+']')
确实很丑陋,但是有效
答案 2 :(得分:0)
另一种较短但又更骇人听闻的方法:(如果您知道在开始和结束时总会有'[\ n'和'\ n]'),则将这些字符删除,而只需将'['和']'< / p>
import json
data = ['A + G', 'B + F', 'C + E', 'D + D']
raw = json.dumps(data, indent=0)
new = raw[2:-2]
print('['+new+']')
答案 3 :(得分:0)
又快又脏:
import json
def cut_ends(string):
string = string.replace('\n]',']')
string = string.replace('[\n','[')
return string
data = ['A + G', 'B + F', 'C + E', 'D + D']
print(cut_ends(json.dumps(data, indent=0)))