亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號