社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

tobias_k

tobias_k 最近创建的主题
tobias_k 最近回复了

zip 需要多个iterables,因此如果通过 单一的 列表列表作为参数,子列表仅包装成元组,每个元组有一个元素。

* 当你把清单交给 拉链 . 这样,你就有效地通过了 列表,而不是 列表列表:

>>> a = [[1,2], [3,4]]
>>> zip(*a)
[(1, 3), (2, 4)]

您的代码有以下问题:

for line in contents:
    if 'module' in line:

在这里, contents 是包含文件全部内容的单个字符串,而不是可以逐行循环的字符串(行)列表或文件句柄。因此,你的 line 实际上不是一行,而是字符串中的一个字符,显然可以 从未 包含子字符串 "module" .

因为你从来没有 使用 这个 线 在循环中,您只需删除循环和条件,代码就可以正常工作。(如果您将代码更改为实际循环行,并且 find 在这些行中,它不会工作,因为 ( ) 不在同一行。)


或者,可以使用正则表达式:

>>> content = """module traffic(green_main, yellow_main, red_main, green_first, yellow_first, 
...                red_first, clk, rst, waiting_main, waiting_first);"""
...
>>> re.search("module \w+\((.*?)\);", content, re.DOTALL).group(1)
'green_main, yellow_main, red_main, green_first, yellow_first, \n               red_first, clk, rst, waiting_main, waiting_first'

在这里, module \w+\((.*?)\); 方法

  • 单词 module 后面是空格和一些单词类型 \w 文字
  • 字面上的开头 (
  • 抓捕组 (...) 带着任何东西 . ,包括换行符( re.DOTALL ),非贪婪 *?
  • 字面上的结束语 ) ;

group(1) 在(非逃逸的)对 (…)

如果你想把这些列在清单上:

>>> list(map(str.strip, _.split(",")))
['green_main', 'yellow_main', 'red_main', 'green_first', 'yellow_first', 'red_first', 'clk', 'rst', 'waiting_main', 'waiting_first']
6 年前
回复了 tobias_k 创建的主题 » 在python中如何根据索引和长度来无限字符串?

只是用模 % 要将索引“规范化”为字符串长度:

def create_sequence(string, start, length):
    return ''.join(string[i % len(string)] for i in range(start, start + length))

>>> create_sequence("book", -3, 9)
'ookbookbo'
>>> create_sequence("book", -5, 15)
'kbookbookbookbo'
6 年前
回复了 tobias_k 创建的主题 » 在嵌套字典上循环并在不满足条件时删除(python)

问题是 for i in range(len(mydict[j]["c"])): 您正在迭代dict中的列表,同时从这些列表中删除。相反,您可以用列表理解替换内部循环:

for d in mydict:
    d['c'] = [d2 for d2 in d['c']
                 if all(k in d2 for k in ("key1", "key2", "key3", "key4"))]
6 年前
回复了 tobias_k 创建的主题 » 如果返回true,则使用相同的字典值重复python函数

可以定义包装函数,应用给定函数 f 在某些情况下 cond 保留,然后将该函数应用于列表中的每个元素。

def repeat(func, cond, x):
    while cond(x):
        x = func(x)
    return x

>>> f = lambda x: x + 5
>>> [repeat(f, lambda x: x < 18, item) for item in [1, 3, 2]]
[21, 18, 22]

或者使用 functools.partial 要创建要应用的新函数,例如, map :

>>> import functools
>>> g = functools.partial(repeat, f, lambda x: x < 18)
>>> list(map(g, [1, 3, 2]))
[21, 18, 22]
6 年前
回复了 tobias_k 创建的主题 » 跳过python中的索引

你想做的事情对于一条巨蟒来说是不可能的。 for 循环,它小于Java/C风格 for (initializer; step; condition) 循环,但更像是 foreach (iterable) 循环,其中iterable恰好是 range 就你而言。

因此,无论何时 i = ... 在你的循环中( i 是来自 对于 循环) 将在循环的下一次迭代中用新值覆盖(未修改)。

相反,您可以使用稍长一点的 while 循环:

i = 0
while i < len(li):
    print(i)
    if i == 3: #along with other condition
        def g(li):
            global i
            i = li[9]
        g(li)
    else:
        i += 1

还要注意嵌套函数 g 尽管实际代码中的情况可能不同,但显然不起任何作用,可以将其删除。

i = 0
while i < len(li):
    print(i)
    if i == 3: #along with other condition
        i = li[9]
    else:
        i += 1