Python中带有列表的案例数

气味
import random
result = []
for i in list_data:
    for e in list_data:
        result += str(i,e)
# ======
return result


import gowithflow as gwf
a = ['a', 'b', 'c']
gwf.number_of_cases(a)
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

a = ['a', 'a']
gwf.number_of_cases(a)
['aa']

a = [1, 2, 3, 'a']
gwf.number_of_cases(a)
['11', '12', '13', '1a', '21', '22', '23', '2a', '31', '32', '33', '3a', 'a1', 'a2', 'a3', 'aa']
# '''

我想要一个包含原始列表中每对值的新列表。每对值都以“ str”格式返回。如果存在一对相同的值,则将删除这两个值中的任何一个。我的代码是上半部分,下半部分是一个示例。

出现Typeerror:

不支持解码str

用户名

如果给出的示例是所需的输出,那么以下解决方案就足够了:

list(set([str(c) + str(d) for c in a for d in a]))

代码的内部部分创建所有对的列表,但不会删除重复项。

[str(c) + str(d) for c in a for d in a]

通过转换为集合并返回列表,可以删除重复项。

a = ['a', 'a']

输出:

['aa']

第二个例子:

a = [1, 2, 3, 'a']

输出:

['33', '2a', 'aa', '1a', '31', '23', '3a', '13', 'a2', 'a3', '32', '11', '21', 'a1', '12', '22']

编辑:

如果顺序对您很重要,并且您想从示例中获得顺序,则可以执行以下操作:

def uniq(input):
  output = []
  for x in input:
    if x not in output:
      output.append(x)
  return output

a = [1, 2, 3, 'a'] 
ordered_with_duplicates = [str(c) + str(d) for c in a for d in a]
ordered_no_duplicates = uniq(ordered_with_duplicates)

输出:

['11', '12', '13', '1a', '21', '22', '23', '2a', '31', '32', '33', '3a', 'a1', 'a2', 'a3', 'aa']

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章