如何打印列表中每个重复字符串的值?

huangapple go评论71阅读模式
英文:

How can I print the values of each duplicate string in a list?

问题

I can provide you with the translated code parts as requested:

在我的列表中有多个重复的单词

我想要一个函数它将打印包含任何类型的terracotta的每个部分以及其相应的数字我一直试图使用`sort()``sorted()`,但我不太确定如何操纵它们来执行这个任务

我希望输出看起来像这样

[('灰色陶瓦', 1880), ('粉色陶瓦', X), '黄色陶瓦', X)...


等等,对于包含“陶瓦”的所有内容。

以下是我尝试过的示例:

```python
terracotta_list = [('灰色陶瓦', 1880), ('条纹丛林木头', 774), ('混凝土灰色', 675), ('黑色混凝土粉末', 596), ('丛林木板', 535), ('闪长岩', 455), ('抛光花岗岩', 363), ('石头板(侧视图)', 296), ('花岗岩', 264), ('陶瓦', 242), ('骨块(顶视图)', 237), ('泥土', 235), ('陶瓦', 11), ('骨块(侧视图)', 10), ('丛林木头(截面)', 10), ('橙色混凝土粉末', 10), ('阿卡西亚木板', 9), ('沙子', 8), ('鹅卵石', 7), ('绿色陶瓦', 7), ('白色混凝土粉末', 7), ('深橡木木板', 6), ('红色陶瓦', 6), ('洋红陶瓦', 5), ('橙色羊毛', 5), ('黑橡木原木', 4), ('白色羊毛', 4), ('粉色混凝土', 4), ('粉色陶瓦', 2), ('粉色混凝土粉末', 2), ('剥去云杉原木', 1), ('镂空砂岩(侧视图)', 1)]

list1 = sorted(terracotta_list, key=lambda blocks: blocks[0])

print(list1)

以及:

terracotta_list = [('灰色陶瓦', 1880), ('条纹丛林木头', 774), ('混凝土灰色', 675), ('黑色混凝土粉末', 596), ('丛林木板', 535), ('闪长岩', 455), ('抛光花岗岩', 363), ('石头板(侧视图)', 296), ('花岗岩', 264), ('陶瓦', 242), ('骨块(顶视图)', 237), ('泥土', 235), ('陶瓦', 11), ('骨块(侧视图)', 10), ('丛林木头(截面)', 10), ('橙色混凝土粉末', 10), ('阿卡西亚木板', 9), ('沙子', 8), ('鹅卵石', 7), ('绿色陶瓦', 7), ('白色混凝土粉末', 7), ('深橡木木板', 6), ('红色陶瓦', 6), ('洋红陶瓦', 5), ('橙色羊毛', 5), ('黑橡木原木', 4), ('白色羊毛', 4), ('粉色混凝土', 4), ('粉色陶瓦', 2), ('粉色混凝土粉末', 2), ('剥去云杉原木', 1), ('镂空砂岩(侧视图)', 1)]

def sort_key(blocks):
    return blocks[1]

blocks.sort(key=sort_key, reverse=True)

print(blocks)

我可以帮助您操纵或创建一个键来查找整个列表中的“陶瓦”字符串并打印与其对应的第二个元素吗?


<details>
<summary>英文:</summary>

In my list there are multiple duplicates of words.

I want a function that will print each section that includes any type of terracotta as well as it&#39;s corresponding number following it. I have been attempting to use `sort()` and `sorted()` but I&#39;m not too sure on how I can manipulate them to do this task.

I&#39;m looking for an output to look like this:

[('Gray terracotta', 1880), ("pink terracotta", X), "yellow terracotta", X)...


etc. for everything that includes &quot;terracotta&quot;.

Here is an example of what I have tried:

terracotta_list = [('Gray terracotta', 1880), ('Stripped jungle log', 774), ('Concrete gray', 675), ('Black concrete powder', 596), ('Jungle planks', 535), ('Diorite', 455), ('Polished granite', 363), ('Stone slab (side view)', 296), ('Granite', 264), ('Terracotta', 242), ('Bone block (top view)', 237), ('Dirt', 235), ('terracotta', 11), ('Bone block (side view)', 10), ('Jungle log (crosscut face)', 10), ('Orange concrete powder', 10), ('Acacia planks', 9), ('Sand', 8), ('Cobblestone', 7), ('Green terracotta', 7), ('White concrete powder', 7), ('Dark oak planks', 6), ('Red terracotta', 6), ('Magenta terracotta', 5), ('Orange wool', 5), ('Black oak log', 4), ('White wool', 4), ('Pink concrete', 4), ('Pink terracotta', 2), ('Pink concrete powder', 2), ('Stripped spruce log', 1), ('Chiseled sandstone (side view)', 1)]

list1 = sorted(terracotta_list, key=lambda blocks: blocks[0])

print(list1)


As well as:

```python
terracotta_list = [(&#39;Gray terracotta&#39;, 1880), (&#39;Stripped jungle log&#39;, 774), (&#39;Concrete gray&#39;, 675), (&#39;Black concrete powder&#39;, 596), (&#39;Jungle planks&#39;, 535), (&#39;Diorite&#39;, 455), (&#39;Polished granite&#39;, 363), (&#39;Stone slab (side view)&#39;, 296), (&#39;Granite&#39;, 264), (&#39;Terracotta&#39;, 242), (&#39;Bone block (top view)&#39;, 237), (&#39;Dirt&#39;, 235), (&#39;terracotta&#39;, 11), (&#39;Bone block (side view)&#39;, 10), (&#39;Jungle log (crosscut face)&#39;, 10), (&#39;Orange concrete powder&#39;, 10), (&#39;Acacia planks&#39;, 9), (&#39;Sand&#39;, 8), (&#39;Cobblestone&#39;, 7), (&#39;Green terracotta&#39;, 7), (&#39;White concrete powder&#39;, 7), (&#39;Dark oak planks&#39;, 6), (&#39;Red terracotta&#39;, 6), (&#39;Magenta terracotta&#39;, 5), (&#39;Orange wool&#39;, 5), (&#39;Black oak log&#39;, 4), (&#39;White wool&#39;, 4), (&#39;Pink concrete&#39;, 4), (&#39;Pink terracotta&#39;, 2), (&#39;Pink concrete powder&#39;, 2), (&#39;Stripped spruce log&#39;, 1), (&#39;Chiseled sandstone (side view)&#39;, 1)]

def sort_key(blocks):
    return blocks[1]

blocks.sort(key=sort_key, reverse=True)

print(blocks)

How can I manipulate or create a key to look for the "terracotta" string in the whole list and print the second corresponding element with it?

答案1

得分: 2

以下是翻译好的代码部分:

lst = [('灰色陶瓷', 1880), ('粉色陶瓷', 5), ('黄色陶瓷', 8)]

def find_value(lst):
    for tup in lst:  # 遍历给定列表
        if '陶瓷' in tup[0]:  # 检查字符串是否包含子字符串
            print(tup)  # 或者你可以打印
            print(tup[1])

find_value(lst)
英文:

As far as I can understand this is what you can do.

lst = [(&#39;Gray terracotta&#39;, 1880), (&quot;pink terracotta&quot;, 5), (&quot;yellow terracotta&quot;, 8)]
def find_value(lst):
    for tup in lst: # Looping through given list
        if &#39;terracotta&#39; in tup[0]:  # Checking if string contains substring
            print(tup) # Or you can print
            print(tup[1])
            
find_value(lst)

答案2

得分: 1

你应该考虑在这里使用 列表推导,并带有一个筛选条件:

import re
print([
    (name, value)
    for name, value in terracotta_list
    if re.search(r"\bterracotta\b", name)
])

使用 re 包 来查找名称中的模式(单词 'terracotta')使用正则表达式。

英文:

You should consider using list comprehension here with a filtering if condition:

import re
print([
    (name, value)
    for name, value in terracotta_list
    if re.search(r&quot;\bterracotta\b&quot;, name)
])

The re package is used to look for a pattern (the word 'terracotta') using a regex in the name.

答案3

得分: 1

我会使用列表推导式来做类似这样的事情:

list1 = []
for k, v in terracotta_list:
    if 'terracotta' in k.lower():
        list1.append([k, v])

或者

list2 = [[k, v] for k, v in terracotta_list if 'terracotta' in k.lower()]

它们本质上是相同的,只是第二种方式更简洁。我认为这会得到你想要的结果。

英文:

I would do something like this with list comprehension:

list1 = []
for k, v in terracotta_list:
    if &#39;terracotta&#39; in k.lower():
        list1.append([k, v])

or

list2 = [[k, v] for k, v in terracotta_list if &#39;terracotta&#39; in k.lower()]

They are both the same thing essentially, one is just shorter. I think that will get you what you're after.

答案4

得分: 0

我猜你想要过滤列表中的关键词,并且不返回重复的项,因为没有重复项。

最简单的方法是遍历列表并检查是否包含关键词:

for item in terracotta_list:
 if "erracotta" in item[0]:
  print (item)

这是一个快速而简单的解决方案,不区分大小写。

英文:

I guess you want to filter the list for a keyword, not returning the duplicates in the list, as there are none.

The simplest way would be to go through the list and check if the word is contained:

for item in terracotta_list:
 if &quot;erracotta&quot; in item[0]:
  print (item)

This is a quick and dirty solution, being case insensitive.

huangapple
  • 本文由 发表于 2023年6月19日 04:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定