小编Sil*_*ost的帖子

按列表len排序列表

我正在尝试按len大小排序列表.但我得到一个奇怪的语法错误.我是python的新手,所以我可能做错了什么

代码的相关部分:

 orderedpaths=[]
 if(len(paths)==1):
  orderedpaths=paths
 else: 
  c=0
  while(len(paths)!=0):

   if(c==0):
    smallest=(len(paths[c])
    c+=1

   else:
    if(len[paths[c])<smallest):
     smallest=(len(paths[c]))
     orderedpaths.append(paths[c])
     del paths[c]
     c+=1    

 return orderedpaths
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 153
    c+=1
    ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我不知道为什么.

python sorting

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

正则表达式非连续字符

目前我有:

[A-Za-z0-9._%+-]
Run Code Online (Sandbox Code Playgroud)

这匹配包含字母,数字和某些特殊字符的任何字符串(._%+-)

如何更改此值以使其不会与包含特殊字符的字符串连续匹配?

举例来说,我希望它匹配: foo.bar+testfoo.+bar+foo.

但不是: foo..bar+testfoo.bar++testfoo.bar++

regex

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

从具有特定参数的列表中获取特定对象

我有一个Account对象列表self.accounts,我知道其中只有一个会有一个type等于'equity' 的属性.从列表中仅获取该对象的最佳(最pythonic)方法是什么?

目前我有以下内容,但我想知道[0]最后是否是多余的.有没有更简洁的方法来做到这一点?

return [account for account in self.accounts if account.type == 'equity'][0]

python list

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

Python参考

我遇到过一种情况,我认为这种情况在理解引用如何在Python中工作方面存在差距.

假设我们有两个类:

class A:
    def __init__(self):
        self.x = [1,2,3]

    def modify(self):
        self.x.append(4)

    def reset(self):
        self.x = []

class B:
    def __init__(self, x):
        self._x = x

    def say(self):
        print self._x




a = A()
b = B(a.x)

b.say()
a.modify()
b.say()
a.reset()
b.say()
Run Code Online (Sandbox Code Playgroud)

我预期的输出是:

[1, 2, 3]
[1, 2, 3, 4]
[]
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

似乎当我打电话reset()并设置self.x为新列表时,所持有的引用B变得独立并继续存在,从而成为副本而不是引用.这是正确的理解吗?

python list

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

列表索引超出范围

这怎么可能,我有一个名为"temp"的列表.这就是我在python解释器中所拥有的.

In [150]: len(temp)
Out[150]: 773942

In [151]: temp[773942]
Run Code Online (Sandbox Code Playgroud)

我得到一个IndexError:列表索引超出范围.

我很困惑,因为它只是告诉我列表中有773942个值,现在它告诉我索引超出范围?

python indexing list

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

替换除第一个和空格之外的所有字符

嗨,我尝试做这样的事情:

我有一些字符串 - 'Hello World!'例如.我想要替换除了第一个和白色空格之外的所有字符.

所以......结果将是:"H.... ......"; 我不想删除它,只是替换"."或其他字符.

我尝试过这样做,preg_replace但没有结果.

php preg-replace str-replace

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

Python noob无法获得类方法

Customer班上有一个叫做的方法save_from_row().它看起来像这样:

@classmethod
def save_from_row(row):
    c = Customer()
    c.name = row.value('customer', 'name')
    c.customer_number = row.value('customer', 'number')
    c.social_security_number = row.value('customer', 'social_security_number')
    c.phone = row.value('customer', 'phone')
    c.save()
    return c
Run Code Online (Sandbox Code Playgroud)

当我尝试运行我的脚本时,我得到了这个:

Traceback (most recent call last):
  File "./import.py", line 16, in <module>
    Customer.save_from_row(row)
TypeError: save_from_row() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)

我不明白参数数量的不匹配.这是怎么回事?

python class-method

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

Python正则表达式没有给出预期的结果

尝试在python 2.5.4中的列表上进行正则表达式搜索 - 示例代码:

import re

list_in = ['heti_abcd_xyz_1234', 'heti_abcd_xyz', 'heti_abcd']

en = re.compile('abcd_xyz_1234$')

for item in list_in:
    if en.search(item) is None:
        list_in.remove(item)
print list_in
Run Code Online (Sandbox Code Playgroud)

结果然而我得到:

['heti_abcd_xyz_1234', 'heti_abcd']
Run Code Online (Sandbox Code Playgroud)

当我只期待第一个元素.

任何建议都非常感谢.

python for-loop list

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

如何匹配标签而忽略其属性

假设我有一些如下所示的 HTML

<a href="#">This text</a>
<a href="#" target="">This Text</a>
<a href="#" oncick="" target="">This Text</a>
Run Code Online (Sandbox Code Playgroud)

什么正则表达式会抓取“此文本”?我对正则表达式的概念很陌生,所以尝试过“(.*)”之类的东西,但并没有取得多大成功。有人可以提供建议吗?:D

regex

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

嵌套for循环的缩进

我想知道为什么这是正确的:

for heading in soup.find_all("td", class_="paraheading"):
    key = " ".join(heading.text.split()).rstrip(":")
    if key in columns:
        print key
        next_td = heading.find_next_sibling("td", class_="bodytext")
        value = " ".join(next_td.text.split())
        print value
    if key == "Industry Categories":
        print key
        ic_next_td = heading.find_next_sibling("td", class_="bodytext")
        for value in ic_next_td.strings:
                print value
Run Code Online (Sandbox Code Playgroud)

这不是:

for heading in soup.find_all("td", class_="paraheading"):
    key = " ".join(heading.text.split()).rstrip(":")
    if key in columns:
        print key
        next_td = heading.find_next_sibling("td", class_="bodytext")
        value = " ".join(next_td.text.split())
        print value
    if key == "Industry Categories":
        print key
        ic_next_td = heading.find_next_sibling("td", class_="bodytext") …
Run Code Online (Sandbox Code Playgroud)

python indentation

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