【问题标题】:Using Python need to search a string使用 Python 需要搜索一个字符串
【发布时间】:2020-03-04 08:12:19
【问题描述】:

使用 Python 我想搜索一个字符串,输入数组列表包含整数和字符串,搜索字符串也有数字。下面是我的例子。如何获得该输出?

sr = pd.Series(['New_York12', 'Isbon212', 'Tokyo31', 'Paris031', 'Munich012']) 
result = sr.str.contains('is|12.0', regex=True,case=False) 

Expected Output:
True
True
False
True
True 

【问题讨论】:

  • 您是否尝试打印result
  • print("Expected Output:") for row in result: print(row)
  • 对不起。一个小改动'is|12.0'。我想在这种情况下。
  • 这个真的需要熊猫系列吗?!
  • 当我将我的 pandas 数据帧转换为字符串时,Model_Str='|'.join(map(str, (Model_data[model].head(Model_data[model].count())))) 整数数据转换为字符串并使用 .0 Ex is|12.0。在这种情况下,它不起作用。

标签: python arrays python-3.x string list


【解决方案1】:

输出结果的简单方法如下:

import pandas as pd


sr = pd.Series(['New_York12', 'Isbon212', 'Tokyo31', 'Paris031', 'Munich012']) 
results = sr.str.contains('is|12.0|12', regex=True,case=False)

for result in results:
    print (result)

返回

True
True
False
True
True

【讨论】:

  • 对不起。一个小变化'是|12.0'。我想在这种情况下。
  • 你能不能运行我检查一下。我的输出低于:City 1 False City 2 True City 3 False City 4 True City 5 False
  • 我得到的和你一样 - 你想要什么输出?
  • 我在问题中提到过。 预期输出 Expected Output: True True False True True
  • 我再次更新了它 - 这将创建您想要的输出。我认为这回答了如何打印输出的原始问题。如果您需要过滤器方面的帮助,这听起来像是另一个问题。
猜你喜欢
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多