小编pk.*_*pk.的帖子

从单词列表中创建字谜列表

我想从单词列表中找到字谜的创建列表.我应该在我的代码或递归中使用另一个循环吗?

some_list = ['bad', 'app', 'sad', 'mad', 'dab','pge', 'bda', 'ppa', 'das', 'dba']

new_list = [some_list[0]]
i = 0
while i+1 < len(some_list):
    if (''.join(sorted(some_list[0]))) == (''.join(sorted(some_list[i+1]))):
        new_list.append(some_list[i+1])
        i = i+1
    else:
        i = i+1

print(new_list)
Run Code Online (Sandbox Code Playgroud)
  • 我的输出是['bad', 'dab', 'bda', 'dba'].但我还想要更多其他字谜的列表some_list.

我希望输出为: - ['app', 'ppa'] - ['bad', 'dab', 'bda', 'dba'] -['sad', 'das']

python list anagram

3
推荐指数
1
解决办法
1956
查看次数

如果我使用变量,如何在print语句中包含引号

word = input("Enter a word: ")
word_length = len(word)

print("The length of", word,"is ", word_length)
Run Code Online (Sandbox Code Playgroud)

如果是'back',则输出为:

The length of back is 4
Run Code Online (Sandbox Code Playgroud)

我希望输出为:

The length of 'back' is 4
Run Code Online (Sandbox Code Playgroud)

python printing variables

1
推荐指数
1
解决办法
129
查看次数

标签 统计

python ×2

anagram ×1

list ×1

printing ×1

variables ×1