enumerate

Break is not activated inside a for loop in Python [closed]

烂漫一生 提交于 2020-01-26 04:00:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 months ago . I want to print the first 10 key-value pairs from the dictionary word_index and I wrote this code: for key, value in enumerate(word_index): count = 0 if count <10: print(f'key {key} : value: {value}') count = count + 1 else: break As you can see the break is not activated. Why is that? What I should change in

Stuck with loops in python - only returning first value

丶灬走出姿态 提交于 2020-01-22 02:04:40
问题 I am a beginner in Python trying to make a function that will capitalize all of the values with an even index, and make lowercase all of the values with an odd index. I have been struggling repeatedly with for loops only giving me the first value. I have also tried with while loops. However I am curious if there is a way to make it work with for loops (do I need a '+=1' somewhere?) def func1(x): for (a,b) in enumerate (x): if a%2 == 0: return b.upper() else: return b.lower() func1('Testing

How can I avoid nested tuple unpacking when enumerating zipped lists?

荒凉一梦 提交于 2020-01-14 05:46:06
问题 How can I avoid using nested tuple unpacking when enumerating a list of tuples like this? for i, (x, y) in enumerate(zip("1234", "ABCD")): # do stuff 回答1: Use itertools.count to avoid nested tuple unpacking: from itertools import count for i, x, y in zip(count(), "1234", "ABCD"): # do stuff 来源: https://stackoverflow.com/questions/11968729/how-can-i-avoid-nested-tuple-unpacking-when-enumerating-zipped-lists

Read csv, then enumerate

随声附和 提交于 2020-01-13 10:55:08
问题 At the moment, I have a list directly in my code and am using enumerate so I can use the list index numbers for the user to select one of the items. Looks like this (just header row included) fmpList = [['Date', 'ID', 'Plot No', 'Modified', 'Lat', 'Long']...... for item in enumerate(fmpList[]): print "[%d] %s" % item This works well but I don't want to put the list in the function but rather to read the csv from file. The alternative I used to do this is... import csv with open ('fmpList.csv'

Understanding pythons enumerate

扶醉桌前 提交于 2020-01-11 12:13:08
问题 I started to teach myself some c++ before moving to python and I am used to writing loops such as for( int i = 0; i < 20; i++ ) { cout << "value of i: " << i << endl; } moving to python I frequently find myself using something like this. i = 0 while i < len(myList): if myList[i] == something: do stuff i = i + 1 I have read that this isnt very "pythonic" at all , and I actually find myself using this type of code alot whenever I have to iterate over stuff , I found the enumerate function in

Problem with indexes in enumerate() - Python [closed]

徘徊边缘 提交于 2020-01-07 09:24:21
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 7 days ago . I have a dataset (a_list_of_sentences) in the form of a list of lists of lists, where the smaller list consist in a word and its syntactic dependency, and these lists are joined into sentences, like this: [[['mary', 'nsubj'], ['loves', 'ROOT'], ['every', 'det'], ['man', 'dobj']], [['mary', 'nsubj'],

Missing directory and file info

旧时模样 提交于 2020-01-06 21:09:47
问题 The following is not returning a full list of files and directories: IEnumerable<FileSystemInfo> files = new DirectoryInfo("C:\\Windows\\System32\\drivers").EnumerateFileSystemInfos("*", SearchOption.AllDirectories); The application is run as a administrator. There are no additional file filter drivers in place. For example if I run: foreach (FileSystemInfo file in files) { Console.WriteLine(file.Name); } I get: en-US gm.dls gmreadme.txt UMDF wimmount.sys bfe.dll.mui ndiscap.sys.mui pacer.sys

Missing directory and file info

☆樱花仙子☆ 提交于 2020-01-06 21:07:34
问题 The following is not returning a full list of files and directories: IEnumerable<FileSystemInfo> files = new DirectoryInfo("C:\\Windows\\System32\\drivers").EnumerateFileSystemInfos("*", SearchOption.AllDirectories); The application is run as a administrator. There are no additional file filter drivers in place. For example if I run: foreach (FileSystemInfo file in files) { Console.WriteLine(file.Name); } I get: en-US gm.dls gmreadme.txt UMDF wimmount.sys bfe.dll.mui ndiscap.sys.mui pacer.sys

Does Kotlin have an “enumerate” function like Python?

和自甴很熟 提交于 2019-12-29 06:38:27
问题 In Python I can write: for i, element in enumerate(my_list): print i # the index, starting from 0 print element # the list-element How can I write this in Kotlin? 回答1: There is a forEachIndexed function in the standard library: myList.forEachIndexed { i, element -> println(i) println(element) } See @s1m0nw1's answer as well, withIndex is also a really nice way to iterate through an Iterable . 回答2: Iterations in Kotlin: Some Alternatives Like already said, forEachIndexed is a good way to

Python: ValueError: Mixing iteration and read methods would lose data

这一生的挚爱 提交于 2019-12-24 02:15:24
问题 When executing the script below I get: ValueError: Mixing iteration and read methods would lose data I understand that this is because num is inside the first for loop and last_host is dependend of num but I have no idea how to work around this. #!/usr/bin/env python2 import datetime as dt import glob as glob import os as os import shutil as shutil import signal as signal import subprocess as sp import sys as sys # Open PDB file and read coordinates pdb_file = open('align_z.pdb', 'r') new_pdb