什么是Pythonic方法来实现以下目标?
# Original lists:
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
# List of tuples from 'list_a' and 'list_b':
list_c = [(1,5), (2,6), (3,7), (4,8)]
Run Code Online (Sandbox Code Playgroud)
每个成员list_c
都是一个元组,其第一个成员来自list_a
,而第二个来自list_b
.
我有以下两个清单:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
现在我想将两个列表的项目添加到新列表中.
输出应该是
third = [7,9,11,13,15]
Run Code Online (Sandbox Code Playgroud) 我试图像这样导入izip模块:
from itertools import izip
Run Code Online (Sandbox Code Playgroud)
然而,在最近从Python 2.7转换到3之后 - 它似乎不起作用.
我想写一个csv文件:
writer.writerows(izip(variable1,2))
Run Code Online (Sandbox Code Playgroud)
但我没有运气.仍然会遇到错误.
我经常添加Python列表的向量.
示例:我有两个这样的列表:
a = [0.0, 1.0, 2.0]
b = [3.0, 4.0, 5.0]
Run Code Online (Sandbox Code Playgroud)
我现在想要将a添加到a以获得结果a = [3.0, 5.0, 7.0]
.
通常我最终这样做:
a[0] += b[0]
a[1] += b[1]
a[2] += b[2]
Run Code Online (Sandbox Code Playgroud)
是否有一些有效,标准的方法来减少打字?
更新:可以假设列表长度为3并包含浮点数.
我正在尝试创建一个水平堆叠条形图,matplotlib
但是我看不到如何使条形实际堆叠而不是从y轴开始.
这是我的测试代码.
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plot_chart(df, fig, ax)
ind = arange(df.shape[0])
ax.barh(ind, df['EndUse_91_1.0'], color='#FFFF00')
ax.barh(ind, df['EndUse_91_nan'], color='#FFFF00')
ax.barh(ind, df['EndUse_80_1.0'], color='#0070C0')
ax.barh(ind, df['EndUse_80_nan'], color='#0070C0')
plt.show()
Run Code Online (Sandbox Code Playgroud)
left
在看到tcaswell的评论后编辑使用kwarg.
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plot_chart(df, fig, ax)
ind = arange(df.shape[0])
ax.barh(ind, df['EndUse_91_1.0'], color='#FFFF00')
lefts = df['EndUse_91_1.0']
ax.barh(ind, df['EndUse_91_nan'], color='#FFFF00', left=lefts)
lefts = lefts + df['EndUse_91_1.0']
ax.barh(ind, df['EndUse_80_1.0'], color='#0070C0', left=lefts)
lefts = lefts + df['EndUse_91_1.0']
ax.barh(ind, df['EndUse_80_nan'], color='#0070C0', left=lefts)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这似乎是正确的方法,但如果没有特定条形的数据,它会失败,因为它试图添加nan
一个然后返回的值nan
.
说我有两个清单:
a=[1,2,3,4,5]
b=[5,4,3,2,1]
Run Code Online (Sandbox Code Playgroud)
我想创建第三个,它将是两个线性和:
c[i]==a[i]+b[i]
c==[6,6,6,6,6]
Run Code Online (Sandbox Code Playgroud)
是否可以使用'for'构造函数?喜欢:
c = [aa+bb for aa in a for bb in b]
Run Code Online (Sandbox Code Playgroud)
(这显然不是我想要的)
假设有两个列表:
['a', 'b', 'c'], ['d', 'e', 'f']
Run Code Online (Sandbox Code Playgroud)
我想要的是:
'ad','ae','af','bd','be','bf','cd','ce','cf'
Run Code Online (Sandbox Code Playgroud)
我怎样才能在没有递归或列表理解的情况下得到这个?我的意思是只使用循环,使用 python?
我想通过元素,而无需使用numpy的列表元素上进行操作,例如,我想add([1,2,3], [2,3,4]) = [3,5,7]
和mult([1,1,1],[9,9,9]) = [9,9,9]
,但我不知道它在做的方式是,它认为"正确"的风格.
我提出的两个解决方案是
def add(list1,list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]+list2[x])
return list3
def mult(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]*list2[x])
return list3
def div(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]/list2[x])
return list3
def sub(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]-list2[x])
return list3
Run Code Online (Sandbox Code Playgroud)
每个操作员都有一个单独的功能
和
def add(a,b)
return a+b
def mult(a,b)
return a*b
def div(a,b)
return a/b
def sub(a,b)
return a-b …
Run Code Online (Sandbox Code Playgroud) python functional-programming coding-style list higher-order-functions
我知道python中不支持减去列表,但是有一些方法可以省略两个列表之间的公共元素.但我想要做的是用一个列表中的相应元素分别减去一个列表中的每个元素,并将结果作为输出列表返回.我怎样才能做到这一点?
A = [3, 4, 6, 7]
B = [1, 3, 6, 3]
print A - B #Should print [2, 1, 0, 4]
Run Code Online (Sandbox Code Playgroud) 我想通过数组中的数字重复我的字符串中的每个字符,即if
rep = [1, 0, 1, 1, 3, 0, 0, 1, 0]
seq = 'AATCGGGAA'
Run Code Online (Sandbox Code Playgroud)
我想要类似的东西
seq*rep
Run Code Online (Sandbox Code Playgroud)
输出
ATCGGGA
Run Code Online (Sandbox Code Playgroud) python ×10
list ×6
coding-style ×1
izip ×1
loops ×1
matplotlib ×1
merge ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
string ×1
subtraction ×1
sum ×1
tuples ×1