简体   繁体   English

将字符串中的每一行转换为字典键

[英]Convert every line in the string into dictionary key

Hi I am new to python and dont know whether can I ask this basic question in this site or not嗨,我是 python 的新手,不知道我是否可以在这个网站上问这个基本问题

I want to convert every line in the string into a key and assign 0 as an value我想将字符串中的每一行转换为一个键并将 0 分配为一个值

MY string is:我的字符串是:

s = '''
sarika

santha

#

akash


nice
'''

I had tried this https://www.geeksforgeeks.org/ways-to-convert-string-to-dictionary/ ways but thought not useful for my requirement我曾尝试过这种https://www.geeksforgeeks.org/ways-to-convert-string-to-dictionary/方法,但认为对我的要求没有用

Pls help anyone Thanks in advance请帮助任何人提前谢谢

Edit:编辑:

Actually I had asked for basic string but I am literally for followed string实际上我要求的是基本字符串,但我实际上是要跟随的字符串

s="""
san
francisco

Santha

Kumari



this one
"""

 Here it should take {sanfrancisco:0 , santha kumari:0 , this one: 0 }

This is the challenge I am facing这是我面临的挑战

Here in my string if having more than 1 new line gap it should take the nextline string as one word and convert into key在我的字符串中,如果有超过 1 个新行间隙,它应该将下一行字符串作为一个单词并转换为键

You can do it in the below way:您可以通过以下方式进行操作:

>>> s="""
... hello
... #
... world
... 
... vk
... """
>>> words = s.split("\n")
>>> words
['', 'hello', '#', 'world', '', 'vk', '']
>>> words = words[1:len(words)-1]
>>> words
['hello', '#', 'world', '', 'vk']
>>> word_dic = {}
>>> for word in words:
...     if word not in word_dic:
...             word_dic[word]=0
... 
>>> word_dic
{'': 0, 'world': 0, '#': 0, 'vk': 0, 'hello': 0}
>>> 

Please let me know if you have any question.如果您有任何问题,请告诉我。

You could continuously match either all lines followed by 2 newlines, or match all lines followed by a single newline.您可以连续匹配所有行后跟 2 个换行符,或者匹配所有行后跟一个换行符。

^(?:\S.*(?:\n\n\S.*)+|\S.*(?:\n\S.*)*)

The pattern matches模式匹配

  • ^ Start of string ^字符串开头
  • (?: Non capture group (?:非捕获组
    • \S.* Match a non whitespace char and the rest of the line \S.*匹配非空白字符和该行的 rest
    • (?:\n\n\S.*)+ Repeat matching 1+ times 2 newlines, a non whitespace char and the rest of the line (?:\n\n\S.*)+重复匹配 1+ 次 2 个换行符、一个非空白字符和该行的 rest
    • | Or或者
    • \S.* Match a single non whitespace char and the rest of the line \S.*匹配单个非空白字符和该行的 rest
    • (?:\n\S.*)* Optionally match a newline, a non whitespace char and the rest of the line (?:\n\S.*)*可选匹配换行符、非空白字符和行的 rest
  • ) Close non capture group )关闭非捕获组

Regex demo |正则表达式演示| Python demo Python 演示

For those matches, replace 2 newlines with a space and replace a single newline with an empty string.对于这些匹配项,用空格替换 2 个换行符并用空字符串替换一个换行符。

Then from the values, create a dictionary and initialize all values with 0.然后从这些值中,创建一个字典并用 0 初始化所有值。

Example例子

import re

s="""
san
francisco

Santha

Kumari



this one
"""
pattern = r"^(?:\S.*(?:\n\n\S.*)+|\S.*(?:\n\S.*)*)"
my_dict = dict.fromkeys(
    [
        re.sub(
            r"(\n\n)|\n",
               lambda n: " " if n.group(1) else "", s.lower()
        ) for s in re.findall(pattern, s, re.MULTILINE)
    ],
    0
)
print(my_dict)

Output Output

{'sanfrancisco': 0, 'santha kumari': 0, 'this one': 0}

You could do it like this:你可以这样做:

# Split the string into a list
l = s.split()
dictionary = {}

# iterate through every element of the list and assign a value of 0 to it

n = 0


for word in l:
   while n < len(l) - 1:
       if word == "#":
           continue
       w = l[n] + l[n+1]
       dictionary.__setitem__(w, 0)
       n+=2
print(dictionary)

steps -脚步 -

  1. Remove punctuations from a string via translate.通过翻译从字符串中删除标点符号。
  2. split words if they're separated by 2 \n character如果单词被 2 \n 字符分隔,则拆分单词
  3. remove the spaces from the list从列表中删除空格
  4. remove \n character and use dict comprehension to generate the required dict删除 \n 字符并使用字典理解生成所需的字典
import string
s = '''
sarika
santha

#

akash




nice
'''

s = s.translate(str.maketrans('', '', string.punctuation))
word_list = s.split('\n\n')
while '' in word_list:
    word_list.remove('')
result = {word.replace('\n', ''): 0 for word in word_list}
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM