为了账号安全,请及时绑定邮箱和手机立即绑定

Str 转换为 Dict,每个 str 的 len 为 k,len 的单词列表为 v

Str 转换为 Dict,每个 str 的 len 为 k,len 的单词列表为 v

陪伴而非守候 2023-09-19 17:12:44
我这里有一个字符串:str_files_txt = "A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operating systems such as CP/M and MS-DOS, where the operating system does not keep track of the file size in bytes, the end of a text file is denoted by placing one or more special characters, known as an end-of-file marker, as padding after the last line in a text file. On modern operating systems such as Microsoft Windows and Unix-like systems, text files do not contain any special EOF character, because file systems on those operating systems keep track of the file size in bytes. There are for most text files a need to have end-of-line delimiters, which are done in a few different ways depending on operating system. Some operating systems with record-orientated file systems may not use new line delimiters and will primarily store text files with lines separated as fixed or variable length records. 'Text file' refers to a type of container, while plain text refers to a type of content. At a generic level of description, there are two kinds of computer files: text files and binary files"我应该创建一个字典,其中键是单词的长度,值是所有具有相同长度的单词。并使用一个列表来存储所有这些单词。这是我尝试过的,它有效,但我不确定如何有效地使用循环来做到这一点,任何人都可以分享答案。files_dict_values = {}files_list = list(set(str_file_txt.split()))values_1=[]values_2=[]values_3=[]values_4=[]values_5=[]values_6=[]values_7=[]values_8=[]values_9=[]values_10=[]values_11=[]for ele in files_list:  if len(ele) == 1:    values_1.append(ele)    files_dict_values.update({len(ele):values_1})  elif len(ele) == 2:    values_2.append(ele)    files_dict_values.update({len(ele):values_2})  elif len(ele) == 3:    values_3.append(ele)    files_dict_values.update({len(ele):values_3})  elif len(ele) == 4:    values_4.append(ele)    files_dict_values.update({len(ele):values_4})  elif len(ele) == 5:    values_5.append(ele)
查看完整描述

5 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

您遇到两个问题:清理数据和创建字典。

在清除不属于单词的字符后,使用 defaultdict(list)

from collections import defaultdict



d = defaultdict(list)


text = """A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operating systems such as CP/M and MS-DOS, where the operating system does not keep track of the file size in bytes, the end of a text file is denoted by placing one or more special characters, known as an end-of-file marker, as padding after the last line in a text file. On modern operating systems such as Microsoft Windows and Unix-like systems, text files do not contain any special EOF character, because file systems on those operating systems keep track of the file size in bytes. There are for most text files a need to have end-of-line delimiters, which are done in a few different ways depending on operating system. Some operating systems with record-orientated file systems may not use new line delimiters and will primarily store text files with lines separated as fixed or variable length records.

'Text file' refers to a type of container, while plain text refers to a type of content.

At a generic level of description, there are two kinds of computer files: text files and binary files"

"""


# remove the characters ,.!;:-"' from begin/end of all space splitted words

words = [w.strip(",.!;:- \"'") for w in text.split()]


# add words to list in dict, automatically creates list if needed

# your code uses a set as well

for w in set(words):

    d[len(w)].append(w)


# output 

for k in sorted(d):

        print(k,d[k])

输出:


1 ['A', 'a']

2 ['to', 'an', 'At', 'do', 'on', 'In', 'On', 'as', 'by', 'or', 'of', 'in', 'is']

3 ['use', 'the', 'one', 'and', 'few', 'not', 'EOF', 'may', 'any', 'for', 'are', 'two', 'end', 'new', 'old']

4 ['have', 'that', 'such', 'type', 'need', 'text', 'more', 'done', 'kind', 'Some', 'does', 'most', 'file', 'with', 'line', 'ways', 'keep', 'CP/M', 'name', 'will', 'Text', 'data', 'last', 'size']

5 ['track', 'those', 'bytes', 'fixed', 'known', 'where', 'which', 'there', 'while', 'There', 'lines', 'kinds', 'store', 'files', 'plain', 'after', 'level']

6 ['exists', 'modern', 'MS-DOS', 'system', 'within', 'refers', 'length', 'marker', 'stored', 'binary']

7 ['because', 'placing', 'content', 'Windows', 'padding', 'systems', 'records', 'contain', 'special', 'generic', 'denoted', 'spelled']

8 ['computer', 'sequence', 'textfile', 'variable']

9 ['Microsoft', 'depending', 'different', 'Unix-like', 'flatfile)', 'primarily', 'container', 'character', 'separated', 'operating']

10 ['delimiters', 'characters', 'electronic', '(sometimes', 'structured']

11 ['end-of-file', 'alternative', 'end-of-line', 'description']

17 ['record-orientated']


查看完整回答
反对 回复 2023-09-19
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

str_files_txt = "A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operating systems such as CP/M and MS-DOS, where the operating system does not keep track of the file size in bytes, the end of a text file is denoted by placing one or more special characters, known as an end-of-file marker, as padding after the last line in a text file. On modern operating systems such as Microsoft Windows and Unix-like systems, text files do not contain any special EOF character, because file systems on those operating systems keep track of the file size in bytes. There are for most text files a need to have end-of-line delimiters, which are done in a few different ways depending on operating system. Some operating systems with record-orientated file systems may not use new line delimiters and will primarily store text files with lines separated as fixed or variable length records. 'Text file' refers to a type of container, while plain text refers to a type of content. At a generic level of description, there are two kinds of computer files: text files and binary files"


lengthWordDict = {}

for word in str_files_txt.split(' '):

    wordWithoutSpecialChars = ''.join([char for char in word if char.isalpha()])

    wordWithoutSpecialCharsLength = len(wordWithoutSpecialChars)

    if(wordWithoutSpecialCharsLength in lengthWordDict.keys()):

        lengthWordDict[wordWithoutSpecialCharsLength].append(word)

    else:

        lengthWordDict[wordWithoutSpecialCharsLength] = [word]

print(lengthWordDict)

这是我的解决方案,它获取单词的长度(没有特殊字符,例如标点符号)


要获取单词的绝对长度(带标点符号)替换wordWithoutSpecialChars 为word


输出:


{1: ['A', 'a', 'a', 'A', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], 4: ['text', 'file', 'name', 'kind', 'file', 'that', 'text.', 'text', 'file', 'data', 'file', 'such', 'does', 'keep', 'file', 'size', 'text', 'file', 'more', 'last', 'line', 'text', 'file.', 'such', 'text', 'file', 'keep', 'file', 'size', 'most', 'text', 'need', 'have', 'done', 'ways', 'Some', 'with', 'file', 'line', 'will', 'text', 'with', "'Text", "file'", 'type', 'text', 'type', 'text'], 9: ['(sometimes', 'operating', 'operating', 'end-of-file', 'operating', 'Microsoft', 'character,', 'operating', 'end-of-line', 'different', 'depending', 'operating', 'operating', 'primarily', 'separated', 'container,'], 7: ['spelled', 'systems', 'denoted', 'placing', 'special', 'padding', 'systems', 'Windows', 'systems,', 'contain', 'special', 'because', 'systems', 'systems', 'systems', 'systems', 'records.', 'content.', 'generic'], 8: ['textfile;', 'flatfile)', 'computer', 'sequence', 'computer', 'Unix-like', 'variable', 'computer'], 2: ['an', 'is', 'is', 'of', 'is', 'as', 'of', 'of', 'as', 'In', 'as', 'of', 'in', 'of', 'is', 'by', 'or', 'as', 'an', 'as', 'in', 'On', 'as', 'do', 'on', 'of', 'in', 'to', 'in', 'on', 'as', 'or', 'to', 'of', 'to', 'of', 'At', 'of', 'of'], 3: ['old', 'CP/M', 'and', 'the', 'not', 'the', 'the', 'end', 'one', 'the', 'and', 'not', 'any', 'EOF', 'the', 'are', 'for', 'are', 'few', 'may', 'not', 'use', 'new', 'and', 'are', 'two', 'and'], 11: ['alternative', 'description,'], 10: ['structured', 'electronic', 'characters,', 'delimiters,', 'delimiters'], 5: ['lines', 'MS-DOS,', 'where', 'track', 'bytes,', 'known', 'after', 'files', 'those', 'track', 'bytes.', 'There', 'files', 'which', 'store', 'files', 'lines', 'fixed', 'while', 'plain', 'level', 'there', 'kinds', 'files:', 'files', 'files'], 6: ['exists', 'stored', 'within', 'system.', 'system', 'marker,', 'modern', 'system.', 'length', 'refers', 'refers', 'binary'], 16: ['record-orientated']}



查看完整回答
反对 回复 2023-09-19
?
千万里不及你

TA贡献1784条经验 获得超9个赞

answer = {}

for word in str_files_text.split():  # loop over all the words

    # use setdefault to create an empty set if the key doesn't exist

    answer.setdefault(len(word), set()).add(word)  # add the word to the set

    # the set will handle deduping


# turn those sets into lists

for k,v in answer.items():

    answer[k] = list(v)


查看完整回答
反对 回复 2023-09-19
?
繁花如伊

TA贡献2012条经验 获得超12个赞

您可以直接将字符串添加到字典的正确位置,如下所示:


res = {}

for ele in list(set(str_files_txt.split())):

  if len(ele) in res:

    res[len(ele)].append(ele)

  else:

    res[len(ele)] = [ele]

print(res)


查看完整回答
反对 回复 2023-09-19
?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

如何使用循环并让 json 自己创建键


str_files_txt = "A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operating systems such as CP/M and MS-DOS, where the operating system does not keep track of the file size in bytes, the end of a text file is denoted by placing one or more special characters, known as an end-of-file marker, as padding after the last line in a text file. On modern operating systems such as Microsoft Windows and Unix-like systems, text files do not contain any special EOF character, because file systems on those operating systems keep track of the file size in bytes. There are for most text files a need to have end-of-line delimiters, which are done in a few different ways depending on operating system. Some operating systems with record-orientated file systems may not use new line delimiters and will primarily store text files with lines separated as fixed or variable length records. 'Text file' refers to a type of container, while plain text refers to a type of content. At a generic level of description, there are two kinds of computer files: text files and binary files"

op={}

for items in str_files_txt.split():

    if len(items) not in op:

        op[len(items)]=[]

    op[len(items)].append(items)

for items in op:

    op[items]=list(set(op[items]))


查看完整回答
反对 回复 2023-09-19
  • 5 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信