我正在编写一个小型NLP算法,我需要执行以下操作:
对于列表中的每个字符串x ["this", "this", "and", "that"],如果字符串x和下一个字符串相同,我想打印字符串.
可能重复:
在Python中将列表迭代为对(当前,下一个)
我有一个这样的列表:
list = [A, B, C, D, E, F, G]
Run Code Online (Sandbox Code Playgroud)
如何对此进行分组以获得以下Python输出
[(A,B), (B,C), (C, D), (D,E), (E,F), (F,G)]
Run Code Online (Sandbox Code Playgroud)
所以这些值按secound值分组但订单仍然保留......
我有一个有点随机的列表.
list = [1,2,4,2,25,3,2,4,2,1,1,32,3,3,2,2,23,4,2,2,2,34,234,33,2,4,3,2,3,4,2,1,3,4,3]
Run Code Online (Sandbox Code Playgroud)
我想迭代它并做这样的事情:
for item in list:
while item >=5:
if item = item_5_indexes_ago
print "its the same as it was 5 entries ago!"
Run Code Online (Sandbox Code Playgroud)
显然,item_5_indexes_ago是无效的python.我应该在这里替代什么?我想检查list [5] == list [1],如果list [6] == list [2],.....对于列表中的每个项目.
我有一个如下所示的循环:
with open(file, 'r') as f:
next(f)
for line in f:
print line
Run Code Online (Sandbox Code Playgroud)
但我不想在每次迭代时只打印当前行,我还想打印前一行,如下所示,但代码没有给我我正在寻找的内容:
with open(file, 'r') as f:
next(f)
for line in f:
previous(f) #this does not go to the previous line
print line
next(f)
print line
next(f)
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的:
输入:
line1
line2
line3
Run Code Online (Sandbox Code Playgroud)
输出:
line1
line2
line2
line3
Run Code Online (Sandbox Code Playgroud) 我正在尝试用 python3 解决这个 python 问题,我的代码如下所示。
class Solution:
def romanToInt(self, s: str) -> int:
# Define integer value to each roman
rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000}
# A list of integer values
value = list(map(rom_val.get, s))
# The subtracted new number
new = 0
# The converted integer number
integer = 0
# List to keep the checked roman
checked = []
for i, j in enumerate(value):
if j > …Run Code Online (Sandbox Code Playgroud) 是否可以在for循环中获取下一个迭代
for x,y dict.iteritems()
我对x的下一项感兴趣,而没有向前移动迭代器。
在Python 2.7.x中
考虑到python列表,我如何将当前项目与下一个项目进行比较呢?例如:
mylist = [1, 2, 2, 4, 2, 3]
for i in mylist:
if i == next item:
replace (both i and next item) with "same value"
>>> [1, "same value", "same value", 4, 2, 3]
Run Code Online (Sandbox Code Playgroud) 假设我l在python中有一个列表定义为:
l = [x1, x2, x3,...,xn-1, xn],
Run Code Online (Sandbox Code Playgroud)
有一种简单的方法来创建一个列表l1:
l1 = [(x1, x2), (x2, x3),...,(xn-1, xn)]?
我想访问生成器中相邻值的重叠对。
如果它是一个列表,我可以使用
a = [5, 7, 11, 4, 5]
for v, w in zip(a[:-1], a[1:]):
print [v, w]
Run Code Online (Sandbox Code Playgroud)
这是来自这个问题。
但是当我尝试对生成器执行相同操作时,出现错误
TypeError: 'generator' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
有没有办法为发电机做到这一点?
我想编写一个函数来显示给定的树是否是 BinarySearch。
这是我到目前为止所写的:
class Node:
def _isBinary(self):
L=[]
if self.left is not None and self.right is not None:
if self.left.data>self.data or self.right.data<self.data:
L.append(1)
else:
L+=self.left._isBinary()
L+=self.right._isBinary()
else:
if self.left is not None:
if self.left.data>self.datat:
L.append(1)
else:
self.left._isBinary()
if self.right is not None:
if self.right.data<self.data:
L.append(1)
else:
self.right._isBinary()
return L
class tree:
def isBinary(self):
if self.root is None:
return
else:
return not 1 in self.root._isBinary(self.root.data)
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,我刚刚报告了代码中感兴趣的部分)这段代码运行良好,但是当例如一个数字(大于根)在树的左侧,但它是较低的数字:
99
/ \
8 888
\
100
Run Code Online (Sandbox Code Playgroud)
它应该给我 False,而不是它返回 True。我能做什么?(如果可能,不完全改变我的原始代码?)
x = [1, 3, 2, 5, 7]
Run Code Online (Sandbox Code Playgroud)
从列表的第一个值开始:
如果下一个值更大,则打印 "\nthe value x is greater than y"
如果下一个值相等,则打印 "\nthe value x is equal to y"
如果下一个值较小,则打印 "\nthe value x is smaller than y"
如何将其转换为精确的Python代码?我实际上正在使用pandas数据框,我只是通过使用列表作为示例来简化它.
x = [1, 3, 2, 5, 7]
Run Code Online (Sandbox Code Playgroud)
根据上面给出的,输出应该是这样的:
the value 3 is greater than 1
the value 2 is smaller than 3
the value 5 is greater than 2
the value 7 is greater than 5
Run Code Online (Sandbox Code Playgroud) c = [1,2,3,4,5,6,7,8,9,10]
for a,b in func(c):
doSomething()
Run Code Online (Sandbox Code Playgroud)
所以func()必须返回 (1,2) (2,3) (3,4) ... (8,9) (9,10)
在python 2.7中是否有一个优雅的方法来实现这一目标?
什么是一种简单的使用方法zip:
Input: (1,2,3,4,5)
Output: ((1,2),(2,3),(3,4),(4,5))
Run Code Online (Sandbox Code Playgroud)
编辑:是的,一般的ngram解决方案类似,但对于这样一个简单的任务来说太冗长了.请参阅下面的答案,了解原因.
python ×12
list ×5
python-2.7 ×2
python-3.x ×2
tuples ×2
binary-tree ×1
compare ×1
dictionary ×1
generator ×1
grouping ×1
iterator ×1
loops ×1
next ×1
recursion ×1
string ×1
tree ×1