繁体   English   中英

如何将列表中的特定元素大写(python)

[英]How to capitalize particular elements in list (python)

我希望接受用户输入并搜索二维列表; 如何使用二维列表并获取输入?

试试这个:

letters = [['l', 'd', 'l', 'o', 'h', 'p'],  
    ['i', 't', 'i', 'f', 'w', 'f'],   
    ['g', 'n', 'r', 'k', 'q', 'u'],  
    ['h', 'g', 'u', 'a', 'l', 'l'],  
    ['t', 'c', 'v', 'g', 't', 'l'],   
    ['d', 'r', 'a', 'w', 'c', 's']]

words = ['draw', 'full', 'hold', 'laugh', 'light', 'start', 'all', 'kid']

word = words[0]
splitted_word = list(word)
for i in range(len(letters)):
    if set(splitted_word).difference(set(letters[i])) == set():
        letters[i] = [x.upper() if  x in splitted_word else x for x in letters[i]]

print(letters)

只需将变量word替换为您要查找的变量即可。

也许你可以这样做:

for i in matrix:
    line = ''.join(i)
    x = line.find(word) #word is the word we need to search eg. draw
    if x != -1:
        i[x:len(word)].upper()

我不确定它是否有效我还没有测试过。 让我知道这是否有帮助。

暂无
暂无

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

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