简体   繁体   English

当两个列表组合在一起时如何从列表项中删除括号、逗号和引号

[英]How to remove parenthesis, commas, and quotation marks from list items when two lists are combined together

I have been trying to create a simple word list with 260 combinations.我一直在尝试创建一个包含 260 种组合的简单单词列表。 I created two lists and combined them to get all combinations我创建了两个列表并将它们组合起来以获得所有组合

letter = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

c = [(x,y) for x in letter for y in number]
print(c)

The result is结果是

[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('a', 6), ('a', 7), ('a', 8), ('a', 9), ('b', 0), ('b', 1), ('b', 2), ('b', 3), ('b', 4), ('b', 5), ('b', 6), ('b', 7), ('b', 8), ('b', 9), ('c', 0), ('c', 1), ('c', 2), ('c', 3), ('c', 4), ('c', 5), ('c', 6), ('c', 7), ('c', 8), ('c', 9), ('d', 0), ('d', 1), ('d', 2), ('d', 3), ('d', 4), ('d', 5), ('d', 6), ('d', 7), ('d', 8), ('d', 9), ('e', 0), ('e', 1), ('e', 2), ('e', 3), ('e', 4), ('e', 5), ('e', 6), ('e', 7), ('e', 8), ('e', 9), ('f', 0), ('f', 1), ('f', 2), ('f', 3), ('f', 4), ('f', 5), ('f', 6), ('f', 7), ('f', 8), ('f', 9), ('g', 0), ('g', 1), ('g', 2), ('g', 3), ('g', 4), ('g', 5), ('g', 6), ('g', 7), ('g', 8), ('g', 9), ('h', 0), ('h', 1), ('h', 2), ('h', 3), ('h', 4), ('h', 5), ('h', 6), ('h', 7), ('h', 8), ('h', 9), ('i', 0), ('i', 1), ('i', 2), ('i', 3), ('i', 4), ('i', 5), ('i', 6), ('i', 7), ('i', 8), ('i', 9), ('j', 0), ('j', 1), ('j', 2), ('j', 3), ('j', 4), ('j', 5), ('j', 6), ('j', 7), ('j', 8), ('j', 9), ('k', 0), ('k', 1), ('k', 2), ('k', 3), ('k', 4), ('k', 5), ('k', 6), ('k', 7), ('k', 8), ('k', 9), ('l', 0), ('l', 1), ('l', 2), ('l', 3), ('l', 4), ('l', 5), ('l', 6), ('l', 7), ('l', 8), ('l', 9), ('m', 0), ('m', 1), ('m', 2), ('m', 3), ('m', 4), ('m', 5), ('m', 6), ('m', 7), ('m', 8), ('m', 9), ('n', 0), ('n', 1), ('n', 2), ('n', 3), ('n', 4), ('n', 5), ('n', 6), ('n', 7), ('n', 8), ('n', 9), ('o', 0), ('o', 1), ('o', 2), ('o', 3), ('o', 4), ('o', 5), ('o', 6), ('o', 7), ('o', 8), ('o', 9), ('p', 0), ('p', 1), ('p', 2), ('p', 3), ('p', 4), ('p', 5), ('p', 6), ('p', 7), ('p', 8), ('p', 9), ('q', 0), ('q', 1), ('q', 2), ('q', 3), ('q', 4), ('q', 5), ('q', 6), ('q', 7), ('q', 8), ('q', 9), ('r', 0), ('r', 1), ('r', 2), ('r', 3), ('r', 4), ('r', 5), ('r', 6), ('r', 7), ('r', 8), ('r', 9), ('s', 0), ('s', 1), ('s', 2), ('s', 3), ('s', 4), ('s', 5), ('s', 6), ('s', 7), ('s', 8), ('s', 9), ('t', 0), ('t', 1), ('t', 2), ('t', 3), ('t', 4), ('t', 5), ('t', 6), ('t', 7), ('t', 8), ('t', 9), ('u', 0), ('u', 1), ('u', 2), ('u', 3), ('u', 4), ('u', 5), ('u', 6), ('u', 7), ('u', 8), ('u', 9), ('v', 0), ('v', 1), ('v', 2), ('v', 3), ('v', 4), ('v', 5), ('v', 6), ('v', 7), ('v', 8), ('v', 9), ('w', 0), ('w', 1), ('w', 2), ('w', 3), ('w', 4), ('w', 5), ('w', 6), ('w', 7), ('w', 8), ('w', 9), ('x', 0), ('x', 1), ('x', 2), ('x', 3), ('x', 4), ('x', 5), ('x', 6), ('x', 7), ('x', 8), ('x', 9), ('y', 0), ('y', 1), ('y', 2), ('y', 3), ('y', 4), ('y', 5), ('y', 6), ('y', 7), ('y', 8), ('y', 9), ('z', 0), ('z', 1), ('z', 2), ('z', 3), ('z', 4), ('z', 5), ('z', 6), ('z', 7), ('z', 8), ('z', 9)]

Now I want to print these items without any spaces and in different lines so that it can be used as a word list;现在我想打印这些项目,没有任何空格和不同的行,以便它可以用作单词列表; for example the required result would look like:例如,所需的结果如下所示:

a0
a1
a2
a3
a4
.
.
.
.
z9

Pro answer:专业回答:

print('\n'.join(x + str(y) for x, y in c))

Use a loop:使用循环:

c = [(x,y) for x in letter for y in number]
for _c in c:
    print(str(_c[0])+ str(_c[1]))

The easiest way to do this is probably a for loop.最简单的方法可能是for循环。 The "tricky" part is converting the values so that they can be joined together (concatenated). “棘手”的部分是转换值,以便它们可以连接在一起(连接)。

There are several techniques available to you:有几种技术可供您使用:

  • convert the values up front预先转换值
  • convert the values when you use them使用它们时转换值

From your code:从您的代码:

letter = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
          "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", 
          "u", "v", "w", "x", "y", "z"]
number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Conversion up front预先转换

Since each of the letters are already strings, you don't need to convert them to the str datatype, but you do need to convert the numbers (which are of the int datatype) into the the str datatype.由于每个字母都已经是字符串,因此您不需要将它们转换为str数据类型,但您需要将数字(属于int数据类型)转换为str数据类型。

As you parse the y values in the number list, you can convert them to str .在解析数字列表中的 y 值时,可以将它们转换为str

c = [(x, str(y)) for x in letter for y in number]

for pair in c:
    print(pair[0] + pair[1])

Conversion when you use them使用时的转换

Conversely, if you want to hold off on converting the int to str until the last minute, you can do the conversion when you print :相反,如果您想推迟将int转换为str直到最后一分钟,您可以在print时进行转换:

c = [(x, y) for x in letter for y in number]

for pair in c:
    print(pair[0] + str(pair[1]))

Tuple unpacking technique:元组解包技术:

If you would rather keep the print statement simple and clean and use variable names that make more sense, this is an option, as well: tuple unpacking in the for loop target variable.如果您希望print语句简单明了并使用更有意义的变量名,这也是一个选项:在for循环目标变量中解包元组。 Meaning we replace the target variable ( pair ) with two target variables (one to accept each value in the tuple >>> ltr and num ):这意味着我们用两个目标变量替换目标变量( pair )(一个接受tuple中的每个值 >>> ltrnum ):

c = [(x, str(y)) for x in letter for y in number]

for ltr, num in c:
    print(ltr + num)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM