返回: “如何编写Python代码以返回特定的字母和数字组合?”

huangapple go评论164阅读模式
英文:

How can I write a Python code to return a certain combination of letters and numbers?

问题

我需要从数据集中提取特定的元数据。我只对键为字母'p'后跟一个整数,然后是另一个字母's'的特定字段感兴趣。

例如,我的第一个数据集具有以下字段,我需要提取:

  1. P7s
  2. P8s
  3. P9s
  4. p10s
  5. p12s

是否有办法编写代码来返回这些项?

这是我目前的代码,我在其中指定数据集中的内容与我加载文件的应用程序中所需的元数据形式匹配的内容。

问题出在 'Markers analysed',我想要指定如果 'Markers analysed' = p#s,然后返回该值。

  1. metadata_crosswalk={
  2. 'Date of Analysis': 'date',
  3. 'Creator':'export user name',
  4. 'Instrument':'cyt',
  5. 'Markers analysed': , # 这里有问题
  6. 'Number of Samples':'tot'
  7. }
  8. for coscine_key,fcs_key in metadata_crosswalk.items():
  9. print("looking at metadata key (coscine)", coscine_key)
  10. print("looking to store fcs metadata key", fcs_key, "| value:", fcs_metadata[fcs_key])
  11. metadata[coscine_key]=fcs_metadata[fcs_key]
英文:

I have to pull certain metadata from a data set. I'm only interested in a certain field whose key is the letter 'p' followed by an integer and then another letter 's'.

For example, my first dataset has the following fields that I need to extract:

  1. P7s
  2. P8s
  3. P9s
  4. p10s
  5. p12s

Is there anyway to write a code to return those items?

This is my code right now where I specify what in the data set will match with the required metadata form in the application where I'm loading the files.

The issue is with 'Markers analysed' where I would like to specify if 'Markers analysed'= p # s then return that value.

  1. metadata_crosswalk={
  2. 'Date of Analysis': 'date',
  3. 'Creator':'export user name',
  4. 'Instrument':'cyt',
  5. ** 'Markers analysed': ,**
  6. 'Number of Samples':'tot'
  7. }
  8. for coscine_key,fcs_key in metadata_crosswalk.items():
  9. print("looking at metadata key (coscine)", coscine_key)
  10. print("looking to store fcs metadata key", fcs_key, "| value:", fcs_metadata[fcs_key])
  11. metadata[coscine_key]=fcs_metadata[fcs_key]

答案1

得分: 2

  1. import re
  2. pattern = re.compile(r'p\d+s')
  3. for coscine_key, fcs_key in metadata_crosswalk.items():
  4. print("查找元数据键(coscine):", coscine_key)
  5. if fcs_key is not None:
  6. print("查找要存储的fcs元数据键:", fcs_key, "| 值:", fcs_metadata[fcs_key])
  7. metadata[coscine_key] = fcs_metadata[fcs_key]
  8. elif coscine_key == 'Markers analysed':
  9. matching_fields = [field for field in fcs_metadata.keys() if pattern.match(field)]
  10. print("匹配的字段:", matching_fields)
  11. metadata[coscine_key] = matching_fields
英文:

You can use regular expressions to extract the fields.

  1. import re
  2. pattern = re.compile(r'p\d+s')
  3. for coscine_key,fcs_key in metadata_crosswalk.items():
  4. print("looking at metadata key (coscine)", coscine_key)
  5. if fcs_key is not None:
  6. print("looking to store fcs metadata key", fcs_key, "| value:", fcs_metadata[fcs_key])
  7. metadata[coscine_key]=fcs_metadata[fcs_key]
  8. elif coscine_key == 'Markers analysed':
  9. matching_fields = [field for field in fcs_metadata.keys() if pattern.match(field)]
  10. print("matching fields:", matching_fields)
  11. metadata[coscine_key] = matching_fields

huangapple
  • 本文由 发表于 2023年3月9日 15:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定