我有一个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)
但是,仍然没有"感觉"正确.: - /
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) 我想要的只是从网上商店加载chrome扩展程序.我做了很多搜索来弄明白,但只知道我们可以从本地机器加载扩展.我真的很想知道selenium是否具有从网上商店或网址加载扩展程序的功能.
请让我知道我正在尝试使用硒?
selenium google-chrome-extension google-chrome-devtools selenium-webdriver
我想要做的很简单,但我找不到怎么做.
从:
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)
换句话说,我需要知道如何:
我正在使用并发.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)
t = time.time()
with concurrent.futures.ProcessPoolExecutor() as executor:
my_results = …Run Code Online (Sandbox Code Playgroud) 我想知道是否有更好的方法在列表中一次迭代两个项目.我经常使用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)
这并不是非常重要,因为我已经有办法做我想做的事情.这似乎就是应该有更好的方法之一.
我想转换这样的列表
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) 我不知道如何解决这个问题.我试过重新输入程序.
我的最后一个主函数出现了意外的缩进错误.
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) 假设我的字符串看起来如下,长度不一,但"字"的数量总是等于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 62并32 3b 3a fe
我可以使用正则表达式来匹配确切的格式,但我想知道是否有更直接的方法来做到这一点,因为正则表达式看起来像是一个简单的问题.