Python - 删除列表中包含多个空格的项目

时间:2016-11-25 08:14:11

标签: python

假设我有一个包含多个字符串的列表。 举个例子:

['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']

有没有办法删除或弹出字符串中多个空格的所有项目?这样输出就是:

['dirty room', 'dormitory']

6 个答案:

答案 0 :(得分:5)

使用带有过滤的列表理解:

old = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
new = [item for item in old if item.count(' ') < 2]

答案 1 :(得分:4)

您可以使用过滤器:

randNumber=$((($RANDOM$RANDOM % $upperBorder) + $lowerBorder))

答案 2 :(得分:0)

result = [l for l in testList if l.count(' ') < 2] 

答案 3 :(得分:0)

TL; DR

使用列表推导

列表理解的替代

如果我必须采取面值OP&#39; &#34; ...删除或弹出所有项目 与...&#34; 我会像这样继续

# INITIALIZATION
n = len(strings)
i_del = 0 # how many items have I deleted till now?

for i in range(n):
    if condition(strings[i-i_del]):
        strings.pop(i-i_del)
        i_del = i_del+1

或者可能是这样的,向后计数从我们的 (简单)预订

n = len(strings)
for i in range(1,n+1):
    if condition(strings[n-i]) : strings.pop(n-i)

关于循环列表的谦卑考虑

  1. 当您只想使用时,可以使用for el in my_list: … 您已存储到列表中的值。

  2. 你必须在列表的索引上使用一个简单的循环, for i in range(len(my_list)): …当你想要修改时 存储在列表中的值,因为简单值是不可变的。

  3. 你必须循环索引并采取额外措施(继续 当你要去时,使用辅助计数器等) 通过删除值或插入新值来修改列表本身 价值观。

  4. 速度

    仅仅是为了获取信息,速度与速度相同 其他地方提出的列表理解

    In [27]: from random import randint as r
    
    In [28]: test = [''.join(chr(r(65,90)) for _ in range(r(11,20))) for count in range(100)]
    
    In [29]: %timeit new = [i for i in test if i.count('A')>1]
    10000 loops, best of 3: 31.3 µs per loop
    
    In [30]: %timeit t2 = test[:]
    The slowest run took 5.44 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 542 ns per loop
    
    In [31]: %%timeit
        ...: t2 = test[:]
        ...: for i in range(1,101):
        ...:     if t2[100-i].count('A')>1 : t2.pop(100-i)
        ...: 
    10000 loops, best of 3: 41 µs per loop
    

    鉴于复制test的费用可以忽略不计,我说.pop() 比列表理解慢30%。

    小心速度测试

    当然,如果我选择了更多pop s偏向的测试 列表理解会更好,但我选择要测试的内容。 道德:谨防速度测试......

答案 4 :(得分:0)

使用regexlist comprehension的简单解决方案:

given = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
result = [i for i in given if len(re.findall(" ", i)) < 2]

不要先忘记import re

答案 5 :(得分:0)

实现这一目标的简单方法:

(ice/WARNING) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): relay only option results in no host candidate for IP4:10.16.241.33:0/UDP

(ice/WARNING) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): relay only option results in no host candidate for IP4:10.32.4.48:0/UDP

(ice/WARNING) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): relay only option results in ICE TCP being disabled

(ice/ERR) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) specified too many components

(ice/WARNING) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) specified bogus candidate

(ice/WARNING) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) has no stream matching stream 0-1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts) aLevel=1

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) no streams with non-empty check lists

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) no streams with pre-answer requests

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) no checks to start

(stun/INFO) STUN-CLIENT(relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:3478/UDP)::TURN): Received response; processing

(stun/WARNING) STUN-CLIENT(relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:3478/UDP)::TURN): Error processing response: Retry may be possible, stun error code 401.

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): Message does not correspond to any registered stun ctx

(stun/INFO) STUN-CLIENT(relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:3478/UDP)::TURN): Received response; processing

(turn/INFO) TURN(relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:3478/UDP)): Succesfully allocated addr IP4:52.9.27.168:49256/UDP lifetime=3600

(ice/ERR) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) pairing local trickle ICE candidate turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): setting pair to state FROZEN: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(ice/INFO) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts))/CAND-PAIR(l0AQ): Pairing candidate IP4:52.9.27.168:49256/UDP (57e1fff):IP4:52.9.27.168:49963/UDP (57c1fff) priority=395226047563186174 (57c1fff0afc3ffe)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/ICE-STREAM(0-1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts) aLevel=0): Starting check timer for stream.

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): setting pair to state WAITING: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): setting pair to state IN_PROGRESS: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(turn/INFO) TURN(relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:3478/UDP)): Creating permission for IP4:52.9.27.168:49963/UDP

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) is now checking

(ice/WARNING) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default): no pairs for 0-1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts) aLevel=0

(stun/WARNING) Inconsistent message method: 103 expected 008

(stun/WARNING) Inconsistent message method: 103 expected 001

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): Message does not correspond to any registered stun ctx

(stun/INFO) STUN-CLIENT(relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:3478/UDP)::TURN): Received response; processing

(stun/WARNING) Inconsistent message method: 108 expected 001

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): Message does not correspond to any registered stun ctx

(ice/INFO) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): peer (PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default) Trickle grace period is over; marking every component with only failed pairs as failed.

(stun/INFO) STUN-CLIENT(relay(IP4:10.16.241.33:63522/UDP|IP4:52.9.27.168:3478/UDP)::TURN): Timed out

(turn/WARNING) TURN(relay(IP4:10.16.241.33:63522/UDP|IP4:52.9.27.168:3478/UDP)): mode 20, nr_turn_client_error_cb

(turn/WARNING) TURN(relay(IP4:10.16.241.33:63522/UDP|IP4:52.9.27.168:3478/UDP)) failed

(turn/INFO) TURN(relay(IP4:10.16.241.33:63522/UDP|IP4:52.9.27.168:3478/UDP)): cancelling

(turn/WARNING) ICE-CANDIDATE(relay(IP4:10.16.241.33:63522/UDP|IP4:52.9.27.168:3478/UDP)): nr_turn_allocated_cb called with state 4

(turn/WARNING) ICE-CANDIDATE(relay(IP4:10.16.241.33:63522/UDP|IP4:52.9.27.168:3478/UDP)): nr_turn_allocated_cb failed

(stun/INFO) STUN-CLIENT(l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)): Timed out

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): setting pair to state FAILED: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/STREAM(0-1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts) aLevel=0)/COMP(1): All pairs are failed, and grace period has elapsed. Marking component as failed.

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default): all checks completed success=0 fail=1

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): triggered check on l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): received STUN check on failed pair, resurrecting: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): setting pair to state WAITING: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(ice/INFO) ICE-PEER(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts):default)/CAND-PAIR(l0AQ): Inserting pair to trigger check queue: l0AQ|IP4:52.9.27.168:49256/UDP|IP4:52.9.27.168:49963/UDP(turn-relay(IP4:10.32.4.48:63523/UDP|IP4:52.9.27.168:49256/UDP)|candidate:3 1 UDP 92020735 52.9.27.168 49963 typ relay raddr 52.9.27.168 rport 49963)

(stun/INFO) Responding with error 400: ICE Failure

(ice/NOTICE) ICE(PC:1480060743115000 (id=119 url=https://172.26.203.112:8889/amit_new/index-rui-dev.jsp#app/contacts)): Message does not correspond to any registered stun ctx

+++++++ END ++++++++

<强>输出:

my_list = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
res = [item for item in my_list if len(item.split(' ')) < 3]