相关疑难解决方法(0)

如何在Python中连接元素两个列表?

我有两个列表,我想以元素方式连接它们.其中一个列表在连接之前经过字符串格式化.

例如 :

a = [0, 1, 5, 6, 10, 11] 
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']
Run Code Online (Sandbox Code Playgroud)

在这种情况下,a将进行字符串格式化.也就是说,新的aaa应该是:

aa = [00, 01, 05, 06, 10, 11]
Run Code Online (Sandbox Code Playgroud)

最终输出应该是:

c = ['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我该怎么做?

python list

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

嵌套列表理解有两个列表

我理解简单列表理解是如何工作的,例如:

[x*2 for x in range(5)] # returns [0,2,4,6,8]
Run Code Online (Sandbox Code Playgroud)

而且我也理解嵌套列表comprehesion的工作原理:

w_list = ["i_have_a_doubt", "with_the","nested_lists_comprehensions"]

# returns the list of strings without underscore and capitalized
print [replaced.title() for replaced in [el.replace("_"," ")for el in w_list]]
Run Code Online (Sandbox Code Playgroud)

所以,当我尝试这样做的时候

l1 = [100,200,300]
l2 = [0,1,2]
[x + y for x in l2 for y in l1 ]
Run Code Online (Sandbox Code Playgroud)

我期待这个:

[101,202,303]
Run Code Online (Sandbox Code Playgroud)

但我得到了这个:

[100,200,300,101,201,301,102,202,302]
Run Code Online (Sandbox Code Playgroud)

所以我有一个更好的方法解决问题,这给了我想要的东西

[x + y for x,y in zip(l1,l2)]
Run Code Online (Sandbox Code Playgroud)

但我不理解第一个代码上9个元素的返回

python nested list-comprehension

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

使用for循环填充字典(python)

我正在尝试使用for循环创建一个字典.这是我的代码:

dicts = {}
keys = range(4)
values = ["Hi", "I", "am", "John"]
for i in keys:
    for x in values:
        dicts[i] = x
print(dicts)
Run Code Online (Sandbox Code Playgroud)

这输出:

{0: 'John', 1: 'John', 2: 'John', 3: 'John'}
Run Code Online (Sandbox Code Playgroud)

为什么?

我打算把它输出:

{0: 'Hi', 1: 'I', 2: 'am', 3: 'John'}
Run Code Online (Sandbox Code Playgroud)

为什么不以这种方式输出,我们如何正确输出?

python dictionary

34
推荐指数
2
解决办法
11万
查看次数

带有两个列表中的参数的函数

我有两个列表xscat和yscat.我希望列表理解能分别在xscat和yscat中选取x和y.结果列表应包含peaks([x[0], y[0]]), peaks([x[1], y[1]])

xscat=yscat=[-1, -1.5,5]
[peaks([x,y]) for x,y in xscat,yscat]
Run Code Online (Sandbox Code Playgroud)

你能找到任何使用理解的解决方案吗?或其他方式(地图)?

python dictionary list-comprehension

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

Python同时迭代两个列表

有没有办法在python中同时覆盖两个或多个列表?

就像是

a = [1,2,3]
b = [4,5,6]
for x,y in a,b:
    print x,y
Run Code Online (Sandbox Code Playgroud)

输出

1 4
2 5
3 6
Run Code Online (Sandbox Code Playgroud)

我知道我可以用元组这样做

l = [(1,4), (2,5), (3,6)]
for x,y in l:
    print x,y
Run Code Online (Sandbox Code Playgroud)

python iteration foreach list python-2.7

21
推荐指数
1
解决办法
5万
查看次数

Python循环有多个列表?

<edit> 感谢所有回答到目前为止的人.zip和os.path.join非常有用.关于如何在前面列出计数器的任何建议,而不做这样的事情:

zip(range(len(files)), files, directories)
Run Code Online (Sandbox Code Playgroud)

</编辑>

嗨,

我正在学习Python,但我来自以下伪代码是典型的背景:

directories = ['directory_0', 'directory_1', 'directory_2']
files = ['file_a', 'file_b', 'file_c']

for(i = 0; i < directories.length; i++) {
    print (i + 1) + '. ' + directories[i] + '/' + files[i] + '\n'
}

# Output:
# 1. directory_0/file_a
# 2. directory_1/file_b
# 3. directory_2/file_c
Run Code Online (Sandbox Code Playgroud)

在Python中,我现在写上面的方式是这样的:

directories = ['directory_0', 'directory_1', 'directory_2']
files = ['file_a', 'file_b', 'file_c']

for i in range(len(directories)):
    print '%s. %s/%s' % ((i + 1), directories[i], files[i]

# Output:
# …
Run Code Online (Sandbox Code Playgroud)

python arrays loops list

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

Python双重迭代

什么是在两个列表上同时迭代的pythonic方式?

假设我想逐行比较两个文件(比较i一个文件中的每一行和另一个文件的第一i行),我想做这样的事情:

file1 = csv.reader(open(filename1),...)
file2 = csv.reader(open(filename2),...)

for line1 in file1 and line2 in file2: #pseudo-code!
    if line1 != line2:
        print "files are not identical"
        break
Run Code Online (Sandbox Code Playgroud)

实现这个目标的pythonic方法是什么?


编辑:我没有使用文件处理程序,而是使用CSV阅读器(csv.reader(open(file),...)),zip()似乎不能使用它...


最终编辑:像@Alex M.建议的那样,zip()在第一次迭代时将文件加载到内存中,因此在大文件上这是一个问题.在Python 2上,使用itertools解决了这个问题.

python

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

如何在Python中迭代两个或多个列表项的元组?

具体来说,我有两个字符串列表,我想将它们组合成一个字符串,其中每一行是列表中的下两个字符串,用空格分隔:

a = ['foo1', 'foo2', 'foo3']
b = ['bar1', 'bar2', 'bar3']
Run Code Online (Sandbox Code Playgroud)

我想要一个函数combine_to_lines(),它将返回:

"""foo1 bar1
foo2 bar2
foo3 bar3"""
Run Code Online (Sandbox Code Playgroud)

我承认我已经解决了这个问题,所以我要发布答案.但也许其他人有一个更好的或看到我的缺陷.

更新:我过度简化了上面的例子.在我的实际问题中,行以更复杂的方式格式化,需要从zip()返回的元组被解压缩.但是,为了达到这个例子最简单的解决方案,我感到非常荣幸.

python loops

12
推荐指数
3
解决办法
4805
查看次数

numpy数组上的操作包含不同大小的行

我有两个列表,看起来像这样:

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

我想逐个元素地逐个减去输出,如下所示:

a-b= [[-4,-4,-4,-4],[7,2,2,2],[-1,-1,-1,-1,-1]]
Run Code Online (Sandbox Code Playgroud)

为了做到这一点,我将每个a和转换b为数组并减去我使用的:

np.array(a)-np.array(b)
Run Code Online (Sandbox Code Playgroud)

输出只是给我错误:

不支持的操作数类型 - :'list'和'list'

我究竟做错了什么?np.array命令不应该确保转换到数组吗?

python arrays numpy

8
推荐指数
2
解决办法
629
查看次数

查找两个列表的索引最大值

假设我有 2 个列表:

L1 = [2,4,1,6]
L2 = [1,5,2,3]
Run Code Online (Sandbox Code Playgroud)

输出应该是一个新列表,其中包含根据 L1 或 L2 的位置找到的最大数字。

示例输出:

L3 = [2, 5, 2, 6]
Run Code Online (Sandbox Code Playgroud)

怎么做 ?

python list

6
推荐指数
2
解决办法
5463
查看次数