相关疑难解决方法(0)

在块中迭代列表的最"pythonic"方法是什么?

我有一个Python脚本,它将整数列表作为输入,我需要一次使用四个整数.不幸的是,我无法控制输入,或者我将它作为四元素元组列表传入.目前,我正在以这种方式迭代它:

for i in xrange(0, len(ints), 4):
    # dummy op for example code
    foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]
Run Code Online (Sandbox Code Playgroud)

它看起来很像"C-think",这让我怀疑有更多的pythonic方式来处理这种情况.迭代后会丢弃该列表,因此无需保留.也许这样的事情会更好吗?

while ints:
    foo += ints[0] * ints[1] + ints[2] * ints[3]
    ints[0:4] = []
Run Code Online (Sandbox Code Playgroud)

但是,仍然没有"感觉"正确.: - /

相关问题:如何在Python中将列表拆分为大小均匀的块?

python optimization loops list chunks

449
推荐指数
15
解决办法
12万
查看次数

如何在Python中使用空分隔符拆分字符串

some_string.split('')在python中有什么好办法?此语法给出错误:

a = '1111'
a.split('')

ValueError: empty separator
Run Code Online (Sandbox Code Playgroud)

我想获得:

['1', '1', '1', '1']
Run Code Online (Sandbox Code Playgroud)

python string split

25
推荐指数
3
解决办法
3万
查看次数

使用硒加载铬延伸

我想要的只是从网上商店加载chrome扩展程序.我做了很多搜索来弄明白,但只知道我们可以从本地机器加载扩展.我真的很想知道selenium是否具有从网上商店或网址加载扩展程序的功能.

请让我知道我正在尝试使用硒?

selenium google-chrome-extension google-chrome-devtools selenium-webdriver

13
推荐指数
4
解决办法
4万
查看次数

如何使用PIL库找到子图像?

我想使用PIL库从大图像中找到子图像.我还想知道找到它的坐标?

python python-imaging-library

10
推荐指数
2
解决办法
6982
查看次数

如何拆分每个第N个元素的Python列表

我想要做的很简单,但我找不到怎么做.

  • 从1st元素开始,将每个第4个元素放入新列表中.
  • 重复第2,第3和第4个元素.

从:

list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b']
Run Code Online (Sandbox Code Playgroud)

至:

list1 = ['1', '5', '9']
list2 = ['2', '6', 'a']
list3 = ['3', '7', 'b']
list4 = ['4', '9']
Run Code Online (Sandbox Code Playgroud)

换句话说,我需要知道如何:

  • 从列表中获取第N个元素(在循环中)
  • 将其存储在新阵列中
  • 重复

python arrays split loops list

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

Python 中的多处理中的多线程

我正在使用并发.futures 模块来进行多处理和多线程处理。我在具有 16GB RAM、英特尔 i7 第八代处理器的 8 核机器上运行它。我在 Python 3.7.2 甚至 Python 3.8.2 上尝试过这个

import concurrent.futures
import time
Run Code Online (Sandbox Code Playgroud) 获取列表并将每个元素乘以 2
def double_value(x):
  y = []
  for elem in x:
    y.append(2 *elem)
  return y
Run Code Online (Sandbox Code Playgroud) 将 elem 乘以 2
def double_single_value(x):
  return 2* x
Run Code Online (Sandbox Code Playgroud) 定义一个
import numpy as np
a = np.arange(100000000).reshape(100, 1000000)
Run Code Online (Sandbox Code Playgroud) 运行多个线程并将每个 elem 乘以 2 的函数
 def get_double_value(x):
  with concurrent.futures.ThreadPoolExecutor() as executor:
    results = executor.map(double_single_value, x)
  return list(results)
Run Code Online (Sandbox Code Playgroud)

下面显示的代码运行时间为 115 秒。这仅使用多处理。这段代码的CPU利用率是100%

t = time.time()

with concurrent.futures.ProcessPoolExecutor() as executor:
  my_results = …
Run Code Online (Sandbox Code Playgroud)

python multithreading multiprocessing concurrent.futures

9
推荐指数
2
解决办法
8660
查看次数

在列表中一次迭代两个项目的方法?

我想知道是否有更好的方法在列表中一次迭代两个项目.我经常使用Maya,其中一个命令(listConnections)返回一个交替值列表.该列表将类似于[connectionDestination,connectionSource,connectionDestination,connectionSource].要对此列表执行任何操作,我最好还是要做类似的事情:

for destination, source in cmds.listConnections():
    print source, destination
Run Code Online (Sandbox Code Playgroud)

你当然可以使用[:: 2]迭代列表中的每个其他项目,枚举和source将是索引+ 1,但是你必须为奇数列表和东西添加额外的检查.

到目前为止,我最接近的是:

from itertools import izip
connections = cmds.listConnections()
for destination, source in izip(connections[::2], connections[1::2]):
    print source, destination
Run Code Online (Sandbox Code Playgroud)

这并不是非常重要,因为我已经有办法做我想做的事情.这似乎就是应该有更好的方法之一.

python maya

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

如何将int列表转换为元组列表

我想转换这样的列表

l1 = [1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)

l2 = [(1,2),(3,4),(5,6),(7,8)]
Run Code Online (Sandbox Code Playgroud)

因为想要循环

for x,y in l2:
    draw_thing(x,y)
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
3
解决办法
322
查看次数

Python意外的indentaton错误main()

我不知道如何解决这个问题.我试过重新输入程序.

我的最后一个主函数出现了意外的缩进错误.

resident = 81
nonresident = 162


def main():

    # initialize counters and total tuition
    resident_counter = 0
    nonresident_counter = 0
    total_tuition = 0

    print("Name \tCode\tCredits\tTuition")
    print

    try:
        # open the data file
        infile = open('enroll.txt', 'r')

        # read the first value from the file
        student_name = infile.readline()

        # continue reading from file until the end
        while student_name != '':

            # strip the new line character and print the student's name
            student_name = student_name.rstrip('\n')
            print(student_name, end='\t')

            # read the …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

3
推荐指数
2
解决办法
783
查看次数

Pythonic将一条线分成四个单词组的方式?

假设我的字符串看起来如下,长度不一,但"字"的数量总是等于4的倍数.

9c 75 5a 62 32 3b 3a fe 40 14 46 1c 6e d5 24 de
c6 11 17 cc 3d d7 99 f4 a1 3f 7f 4c
Run Code Online (Sandbox Code Playgroud)

我想,切成像串 9c 75 5a 6232 3b 3a fe

我可以使用正则表达式来匹配确切的格式,但我想知道是否有更直接的方法来做到这一点,因为正则表达式看起来像是一个简单的问题.

python string

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