python for循环矩阵来分配值(python for loop in matrix to assign values)
我正在创建一个程序,它将创建一个网格,程序将通过您在输入处分配的矩阵数组来确定其位置。
码:
def onbekende_naam(hoogtes): print(hoogtes) i = 0 j = 0 pos1 = set() for hoogtes_subs in hoogtes: j = 0 for hoogtes in hoogtes: print("i = " + str(i)) print("j = " + str(j)) pos1.add((i, j)) print pos1 j += 1 i += 1 #pos1.add((i, j)) return pos1 #verwerking print (onbekende_naam(hoogtes))
输入:
4 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 12 1
输出:
[['1', '2', '3', '4'], ['5', '6', '7', '8'], ['9', '1', '2', '3'], ['4', '5', '6', '7']] i = 0 j = 0 set([(0, 0)]) i = 0 j = 1 set([(0, 1), (0, 0)]) i = 0 j = 2 set([(0, 1), (0, 0), (0, 2)]) i = 0 j = 3 set([(0, 1), (0, 3), (0, 0), (0, 2)]) i = 1 j = 0 set([(0, 1), (0, 3), (0, 0), (0, 2), (1, 0)]) i = 1 j = 1 set([(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (1, 1)]) i = 1 j = 2 set([(0, 1), (1, 2), (0, 0), (0, 2), (1, 0), (0, 3), (1, 1)]) i = 1 j = 3 set([(0, 1), (1, 2), (0, 0), (0, 2), (1, 3), (1, 0), (0, 3), (1, 1)]) i = 2 j = 0 set([(0, 1), (1, 2), (0, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) i = 3 j = 0 set([(0, 1), (1, 2), (0, 0), (3, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) set([(0, 1), (1, 2), (0, 0), (3, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)])
正如你所看到的,当i值大于2时,它会停止递增j
我很新,所以感谢您的帮助
I was creating a program that will create a grid and the program will deside its position by a matrix array you assign at the input.
code:
def onbekende_naam(hoogtes): print(hoogtes) i = 0 j = 0 pos1 = set() for hoogtes_subs in hoogtes: j = 0 for hoogtes in hoogtes: print("i = " + str(i)) print("j = " + str(j)) pos1.add((i, j)) print pos1 j += 1 i += 1 #pos1.add((i, j)) return pos1 #verwerking print (onbekende_naam(hoogtes))
input:
4 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 12 1
output:
[['1', '2', '3', '4'], ['5', '6', '7', '8'], ['9', '1', '2', '3'], ['4', '5', '6', '7']] i = 0 j = 0 set([(0, 0)]) i = 0 j = 1 set([(0, 1), (0, 0)]) i = 0 j = 2 set([(0, 1), (0, 0), (0, 2)]) i = 0 j = 3 set([(0, 1), (0, 3), (0, 0), (0, 2)]) i = 1 j = 0 set([(0, 1), (0, 3), (0, 0), (0, 2), (1, 0)]) i = 1 j = 1 set([(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (1, 1)]) i = 1 j = 2 set([(0, 1), (1, 2), (0, 0), (0, 2), (1, 0), (0, 3), (1, 1)]) i = 1 j = 3 set([(0, 1), (1, 2), (0, 0), (0, 2), (1, 3), (1, 0), (0, 3), (1, 1)]) i = 2 j = 0 set([(0, 1), (1, 2), (0, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) i = 3 j = 0 set([(0, 1), (1, 2), (0, 0), (3, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) set([(0, 1), (1, 2), (0, 0), (3, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)])
as you can see it stops incrementing j when the i value is bigger than 2
I'm fairly new at this so thanks for helping
原文:https://stackoverflow.com/questions/42537691
更新时间:2022-02-10 09:02
最满意答案
import keyword print(keyword.kwlist)
列出所有关键字。
为了获得所有的内置功能,
import builtins print(dir(builtins))
给你所有的内建插件:
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
import keyword print(keyword.kwlist)
lists all the keywords.
In order to get all the built-ins,
import builtins print(dir(builtins))
gives you all the built-ins:
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
相关问答
更多-
您最好使用关键字模块 >>> import keyword >>> keyword.kwlist ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', ' ...
-
这个给你: import re BAD_WORDS = ["spam", "ham"] BAD_WORDS_RE = re.compile(r"\b(%s)\b" % "|".join(BAD_WORDS)) for i, line in enumerate(open("file.txt").readlines()): words = set(BAD_WORDS_RE.findall(line)) if words: print "Found the following ...
-
如何使用正则表达式在python中提取关键字列表后的单词?(how to extract words following a list of keywords in python using regex?)[2022-10-05]
这里的主要问题是您需要将{keywords}放在非捕获组中: (?:{keywords}) 。 下面是一个示意图: a|b|c\s+\w+匹配a或b或c ++ . When you put the alternation list into a group, . When you put the alternation list into a group, (a | b | c)\ s + \ w +时, it matches either a , or b or c`,然 ... -
你可以在一个正则表达式中完成: >>> reobj = re.compile("(?:auto|allow-|iface)(?:(?!(?:auto|allow-|iface)).)*(?>> result = reobj.findall(subject) >>> result ['auto lo eth0', 'allow-hotplug eth1', 'iface eth0-home inet static\n address 192.168.1.1\n ...
-
DbLookup关键字(DbLookup Keywords)[2022-05-21]
在作为Notes客户机安装(以及部分Domino服务器安装)的一部分的Lotus Domino Designer帮助数据库中查找@DbLookup的帮助主题。 以下内容摘自关于Keyword参数的文档: Keyword. Optional. Keywords can be concatenated. [FAILSILENT] returns "" (null string) instead of an error if the key cannot be found. [PARTIALMATCH] retu ... -
Python完整的关键字列表(Python full keywords list)[2024-03-20]
import keyword print(keyword.kwlist) 列出所有关键字。 为了获得所有的内置功能, import builtins print(dir(builtins)) 给你所有的内建插件: ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'Chil ... -
Delphi SynEdit无法识别列表中的所有关键字(Python)(Delphi SynEdit does not recognize all Keywords from the list (Python))[2023-08-22]
我能够让Syntax Highlighting工作。 似乎在SynHighligterPython.pas中填充的单词列表在一个列表中包含关键字和非关键字。 然后它假定组合列表已排序。 因此问题出现在函数IdentKind(可能:PWideChar)中,它使用FKeywords.Find(s,i)来搜索匹配的关键字。 根据定义,“查找”仅适用于排序列表。 我的解决方法是更改此行以使用indexof函数进行搜索,如下所示。 I:= FKeywords.IndexOf(s); 搜索功能记录在此链接中 I was ... -
在完整的Hibernate源代码下载中有一个grammar\hql.g文件,它是ANTLR语言定义。 您可以在这里从官方的GitHub源代码库查看该文件的最新版本。 在tokens部分,您可以找到所有令牌,包括关键字(它们是定义为字符串的关键字,例如ALL="all" )。 In the full Hibernate source download there's a grammar\hql.g file, which is the ANTLR language definition. You can vi ...
-
如何使用Postgres全文搜索来获取最常用的关键字?(How do I use Postgres full text search to get most frequent keywords?)[2023-12-10]
如果有人有同样的问题。 我必须首先使用葡萄牙语停止词( http://www.postgresql.org/docs/9.1/static/textsearch-dictionaries.html#TEXTSEARCH-STOPWORDS )创建基于简单的dicitionary: CREATE TEXT SEARCH DICTIONARY public.simple_dict ( TEMPLATE = pg_catalog.simple, STOPWORDS = english ); 然后 ... -
如果你想匹配完整的单词,你可以使用set交集: >>> my_string = 'this is my complex description' >>> keywords = ['my', 'desc', 'complex'] >>> set(my_string.split()) & set(keywords) {'complex', 'my'} If you want to match complete words, you could use set intersection: >>> my_stri ...