<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) 基本上我希望能够在循环迭代中告诉我何时在第N个项目上.有什么想法吗?
d = {1:2, 3:4, 5:6, 7:8, 9:0}
for x in d:
if last item: # <-- this line is psuedo code
print "last item :", x
else:
print x
Run Code Online (Sandbox Code Playgroud) 在Ruby中,如果我有一个数组并且我想在循环中使用索引和值,我会使用each_with_index.
a=['a','b','c']
a.each_with_index{|v,i| puts("#{i} : #{v}") }
Run Code Online (Sandbox Code Playgroud)
版画
0 : a
1 : b
2 : c
Run Code Online (Sandbox Code Playgroud)
什么是Pythonic做同样的事情?
我没有编写一段时间并尝试重新使用Python.我正在尝试编写一个简单的程序,通过将每个数组元素值添加到一个总和来对数组求和.这就是我所拥有的:
def sumAnArray(ar):
theSum = 0
for i in ar:
theSum = theSum + ar[i]
print(theSum)
return theSum
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
line 13, theSum = theSum + ar[i]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
我发现我正在尝试做的事情显然就像这样简单:
sum(ar)
Run Code Online (Sandbox Code Playgroud)
但显然我并没有正确地遍历数组,我认为这是我需要为其他目的正确学习的东西.谢谢!
我正在尝试使用ijson包解析一个大的(~100MB)json文件,它允许我以有效的方式与文件交互.但是,在编写了这样的代码之后,
with open(filename, 'r') as f:
parser = ijson.parse(f)
for prefix, event, value in parser:
if prefix == "name":
print(value)
Run Code Online (Sandbox Code Playgroud)
我发现代码只解析第一行,而不解析文件中的其余行!
以下是我的json文件的一部分:
{"name":"accelerator_pedal_position","value":0,"timestamp":1364323939.012000}
{"name":"engine_speed","value":772,"timestamp":1364323939.027000}
{"name":"vehicle_speed","value":0,"timestamp":1364323939.029000}
{"name":"accelerator_pedal_position","value":0,"timestamp":1364323939.035000}
Run Code Online (Sandbox Code Playgroud)
在我看来,我认为ijson只解析一个json对象.
有人可以建议如何解决这个问题?
我有一个类似的清单:
[[16777230, 0], [16777226, 1], [16777252, 2], [16777246, 0]]
Run Code Online (Sandbox Code Playgroud)
我想在 python 中为我的操作在循环(嵌套循环)中创建一个循环,这样内循环将始终从外循环的下一个元素开始。
例如,外循环将在每次迭代中遍历列表中从索引 0 到 3 的所有元素。但在外循环的第一次迭代中,内循环将从索引 1 开始并在索引 3 处结束。那么,在外循环的第二次迭代中,内循环应该遍历索引2到索引3。 依此类推... 外循环的最后一次迭代应该使内循环从索引n遍历到索引n,基本上只有最后一个元素,在本例中为索引 3 到索引 3。
问题是我在遍历时删除了列表的元素。因此,它在使用范围函数进行遍历时会产生列表索引超出范围等问题。
如何构建这些内循环和外循环?
我试过这个,但似乎不起作用:
for sub_list1 in yx:
index_sl1 = yx.index(sub_list1)
for sub_list2 in yx[index_sl1+1:]:
Operations...
Run Code Online (Sandbox Code Playgroud)
帮助将不胜感激。谢谢!!
嗨,大家好.
我有一个包含很多元素的列表,我使用for循环迭代列表.例如
li = [2,31,321,41,3423,4,234,24,32,42,3,24,,31,123]
for (i in li):
print i
Run Code Online (Sandbox Code Playgroud)
但是我想得到i的前一个元素,如何实现呢?
我不必使用for循环,所以请随意更改任何内容.谢谢!
python的新手,并试图学习文件i/o的绳索.
我正在使用以下格式从大型(200万行)文件中提取行:
56fr4
4543d
4343d
5irh3
Run Code Online (Sandbox Code Playgroud)
这是我用来返回代码的函数:
def getCode(i):
with open("test.txt") as f:
for index, line in enumerate(f):
if index == i:
code = # what does it equal?
break
return code
Run Code Online (Sandbox Code Playgroud)
一旦索引到达正确的位置(i),我使用什么语法来设置代码变量?
我正在参加这个在线Python课程,他们不喜欢使用单行解决方案的学生.该课程不接受此解决方案的括号.
我已经使用列表理解解决了问题,但课程拒绝了我的答案.
问题是:
使用
index和其他列表的方法,编写一个函数replace(list, X, Y)它取代所有出现X在list用Y.例如,如果L = [3, 1, 4, 1, 5, 9]那么replace(L, 1, 7)将更改Lto 的内容[3, 7, 4, 7, 5, 9].为了使这项练习成为一项挑战,您不能使用[].注意:您不需要使用return.
这是我到目前为止,但由于TypeError而中断:'int'对象不可迭代.
list = [3, 1, 4, 1, 5, 9]
def replace(list, X, Y):
while X in list:
for i,v in range(len(list)):
if v==1:
list.remove(1)
list.insert(i, 7)
replace(list, 1, 7)
Run Code Online (Sandbox Code Playgroud)
这是我原来的答案,但遭到拒绝.
list = [3, 1, 4, 1, 5, …Run Code Online (Sandbox Code Playgroud) 我在这里找到了答案,但它不适用于我在Python'for'循环中访问索引
我正试图从第二个索引循环
ints = [1, 3, 4, 5, 'hi']
for indx, val in enumerate(ints, start=1):
print indx, val
for i in range(len(ints)):
print i, ints[1]
Run Code Online (Sandbox Code Playgroud)
我的输出就是这个
1 1
2 3
3 4
4 5
5 hi
0 3
1 3
2 3
3 3
4 3
Run Code Online (Sandbox Code Playgroud)
我需要在索引1处打印出第一个整数并循环到结尾.
python ×10
list ×4
loops ×4
arrays ×2
for-loop ×2
dictionary ×1
enumerate ×1
equivalent ×1
file-io ×1
iterator ×1
json ×1
nested-loops ×1
python-3.x ×1
replace ×1
ruby ×1
sum ×1