社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

PacketLoss

PacketLoss 最近创建的主题
PacketLoss 最近回复了
5 年前
回复了 PacketLoss 创建的主题 » 如何在Python中对列表中的字符串执行方法

你可以用 for loops list comprehension 在你的例子中实现这个结果。

all_lists = [['list one'],['list two'],['list...'],['list n']]

作为一个函数,我们可以执行 upper 对于列表中的每个元素,使用 for loop .

def upper_list(data):
    result = []
    for nested in data:
        changes = []
        for element in nested:
            changes.append(element.upper())
        result.append(changes)
    return result

upper_list(all_lists)
#[['LIST ONE'], ['LIST TWO'], ['LIST...'], ['LIST N']]

此外,您可以使用列表理解将上述内容压缩成一行代码。

all_lists = [[element.upper() for element in nested] for nested in all_lists]

这两种方法都适用于包含多个元素的嵌套列表,例如;

all_lists = [['list one', 'test'],['list two','two'],['list...'],['list n']]

>>>[[element.upper() for element in nested] for nested in all_lists]
#[['LIST ONE', 'TEST'], ['LIST TWO', 'TWO'], ['LIST...'], ['LIST N']]
5 年前
回复了 PacketLoss 创建的主题 » 如何让python只在你说“嗨”的时候回复
  1. 你从来不叫你的函数
  2. 你不需要检查 if True 在检查值是否匹配之后,这是多余的。

以下内容将帮助您开始。

def hiOrHey(hello):
    if hello.lower() == 'hi': # Check if hello in lowercase matches hi
        print("Hey there!")
    else:
        print("SAY HI")

hello = input('Say Hi: ')

hiOrHey(hello) # Call your function, passing variable hello