我如何移动此代码以列出理解
prev = 0
data = [1, 20, 40, 50, 100]
newlist = []
for x in data:
value = x - prev
prev = x
newlist.append(value)
Run Code Online (Sandbox Code Playgroud) 我有一个清单清单:
W = [[5.0, 0, 0, 0, 0], [5.0, 0, 0, 0], [0, 0, 0], [5.0, 0, 0, 0], [5.0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
W我要从中的每个列表中删除零个条目。
我尝试了以下方法:
for idx in range(len(W)):
W[idx].remove(0)
print(W)
Run Code Online (Sandbox Code Playgroud)
但它总是会回来
[[5.0, 0, 0, 0, 0], [5.0, 0, 0, 0], [0, 0, 0], [5.0, 0, 0, 0], [5.0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
这里什么都没有改变。我知道以下事实:在遍历对象时无法更改对象,但是由于我没有遍历W而是遍历len(W),所以我不明白为什么我的代码无法正常工作。
我究竟做错了什么?
编写一个 python 函数,它对给定的字符串执行运行长度编码并返回运行长度编码的字符串。
我尝试使用循环但无法获得预期的输出。
def encode(message):
#Remove pass and write your logic here
count=0
encoded_message=[]
for char in range(0,len(message)-1,1):
count=1
while(message[char]==message[char+1]):
count=count+1;
char=char+1
encoded_message.append(str(count)+message[char])
return encoded_message
encoded_message=encode("ABBBBCCCCCCCCAB")
print(' '.join(encoded_message))
Run Code Online (Sandbox Code Playgroud)
预期输出为1A4B8C1A1B。我得到的是1A 4B 3B 2B 1B 8C 7C 6C 5C 4C 3C 2C 1C 1A
我有一个 pandas df,我需要压平系列列
data = pd.DataFrame([['TRAN',[{'Name':'Ben','Age':'24'}],'T','Good'],
['LMI',[{'Name':'Zoe','Age':'32'}],'U','Better'],
['ARN',[{'Name':'Jack','Age':'28'}],'V','Best']
],
columns=['Type', 'Applicant', 'Decision', 'Action'])
Run Code Online (Sandbox Code Playgroud)
数据是 pandas 数据框,申请人列是一个系列,
#data
type(data)
Out[25]: pandas.core.frame.DataFrame
#Applicant Column
type(data.Applicant)
Out[26]: pandas.core.series.Series
Run Code Online (Sandbox Code Playgroud)
我需要展平该系列并将列名称转换为'Type', 'Applicant.Name', 'Applicant.Age', 'Decision',的数据框'Action'。
def different(s):
x = len(s)
for i in range(1, 1 << x):
u.append([s[j] for j in range(x) if (i & (1 << j))])
Run Code Online (Sandbox Code Playgroud)
它需要一个列表并进行不同的组合(a,b,c)=((a,b,c),(a,b),(a,c)等.)但是这个范围是做什么的?从1到什么.我不明白"<<"
而且,如果(i&(1 << j))这是做什么的?它会检查i和2是否具有j的功率?对我没有任何意义:/ /
这些是我的 csv 文件中的一些示例行:
10/10/1949 20:30,san marcos,tx,us,cylinder,2700,45 minutes,"This event took place in early fall around 1949-50. It occurred after a Boy Scout meeting in the Baptist Church. The Baptist Church sit",4/27/2004,29.8830556,-97.9411111
10/10/1949 21:00,lackland afb,tx,,light,7200,1-2 hrs,"1949 Lackland AFB, TX. Lights racing across the sky & making 90 degree turns on a dime.",12/16/2005,29.38421,-98.581082
10/10/1955 17:00,chester (uk/england),,gb,circle,20,20 seconds,"Green/Orange circular disc over Chester, England",1/21/2008,53.2,-2.916667
10/10/1956 21:00,edna,tx,us,circle,20,1/2 hour,"My older brother and twin sister were leaving the only Edna theater at about 9 PM,...we had our bikes …Run Code Online (Sandbox Code Playgroud) 在views.py我通过'key_tp_list': tb_list,'key_tb_list': tb_list。它们的类型是字典列表。然后在我的html中,我写道:
{% for i in range(key_tp_length) %}
<li>{{ key_tp_list[i].eid }} | {{ dict.user }} | {{ dict.start_time }} | {{ dict.note }}</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
结果发现有2个错误:我不能使用range(key_tp_length)in {% %},也不能使用key_tp_list[i]in {{ }}。
我怎样才能解决这个问题?
在工作中,我们最近升级到了熊猫0.20,我有一个使用sort排序的数字列表(但是不再支持,尝试时会收到上述消息sort_values)。
numbers = [1, 3, 4, 2]
numbers.sort(reverse = True)
print numbers
[4, 3, 2, 1]
numbers.sort_values(reverse = True)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
追溯(最近一次通话):
文件“”,第1行,位于
AttributeError:“列表”对象没有属性“ sort_values”
我打算在python中做这样的事情:
plt.plot(xval_a_target, q_prof_target, label=r"target", color=target_color, ls=target_style, linewidth=lwidth)
Run Code Online (Sandbox Code Playgroud)
我正在以这种方式创建许多不同的图,并希望将后一部分分配给变量:
target_plot_style = """label=r"target", color=target_color, ls=target_style, linewidth=lwidth"""
Run Code Online (Sandbox Code Playgroud)
为了将绘图线缩短为:
plt.plot(xval_a_target, q_prof_target, eval(target_plot_style),我尝试使用eval和exec进行了尝试,但是它不起作用。有没有简单的方法可以做到这一点?