Py学习  »  Ch3steR  »  全部回复
回复总数  5
6 年前
回复了 Ch3steR 创建的主题 » Python:“list”对象不能解释为整数

你可以用 itertools.product

list(itertools.product([points1[0]],points1[1]))

[(1219.5537056928035, 1318.8861439312564),
 (1219.5537056928035, 1214.6746106337268),
 (1219.5537056928035, 1110.4630773361973),
 (1219.5537056928035, 1006.2515440386678),
 (1219.5537056928035, 902.0400107411383)]

itertools.repeat 是

itertools.repeat(object[, times])

times 以整数作为参数,然后给它一个列表。

6 年前
回复了 Ch3steR 创建的主题 » 如何将以下Python字符串拆分为字符串列表?

你可以用 re 在这里。

import re
text='Predicate(big,small)'
parsed=re.findall(r'\w+|[^a-zA-Z,\s])
# ['Predicate', '(', 'big', 'small', ')']
  1. \w+ [a-zA-Z0-9_] ).
  2. [^a-zA-Z,\s] 匹配列表中不存在的单个字符。
  3. \s
6 年前
回复了 Ch3steR 创建的主题 » 如何用python将乘法表的结果存储在列表中?

table(n) 不退还任何东西。你可以使用 return 或 yield .

使用 返回

def table(n,lst=[]):
    for i in range(1,11):
        lst=lst+[n*i]
    return lst
n = int(input("Enter the Number\n"))
a=table(n)

使用 产量 .

def table(n):
    for i in range(1,11):
        yield n*i
n=int(input())
a=list(table(table(n))
6 年前
回复了 Ch3steR 创建的主题 » 在Python中将带有值的文本文件加载到元组中
with open('file.txt','r') as f:
    my_tuple=tuple(line.strip('\n') for line in f)

print(my_tuple)
# ('word1','word2','word3','word4','word5')
6 年前
回复了 Ch3steR 创建的主题 » 在列表中运行多个参数的Python

你可以用 zip

zip()的目的是映射多个容器的类似索引,以便它们可以作为单个实体使用。

返回元组的迭代器,其中第i个元组包含每个参数序列或iterable中的第i个元素。当最短输入iterable用尽时,迭代器停止。

zip() 参考链接。

ID = ["user-19533", "user-19532", "user-19531"]
names = ["test1", "test2", "test3"]
section = ["abc","pqr","xyz"]
zipped=zip(ID,names,section)
for i,name,sec in zipped:
    print(f'{i} would have names = {name} , section = {sec}')