简体   繁体   English

如何从for循环创建列表

[英]how to create a list from for loop

everyone, recently I am trying to create a list from my data大家,最近我正在尝试从我的数据创建一个列表

Here is my data:这是我的数据:

!wget --no-check-certificate 'https://drive.google.com/uc?export=download&id=1VfkX_asEXo1HpdSE68Cl_HEdyrKPDxsw' -O /content/API_UAT2.txt

I want to find whether these items exist in my data我想查找这些项目是否存在于我的数据中

tunnel_fares = {

            'Aberdeen Tunnel':5

                 , 'Lion Rock Tunnel':8

                 , 'Shing Mun Tunnels':5

                 , 'Tseung Kwan O Tunnel':3

                 , 'Tsing Sha Highway':8

                 , 'Cross Harbour Tunnel':20

                 , 'Eastern Harbour Crossing':40

                 , 'Western Harbour Crossing':85

            , 'Tate\'s Cairn Tunnel':20

            , 'Tai Lam Tunnel':48

            , 'Lantau Link':30

            }

Here is my code这是我的代码

with open ('API_UAT2.txt') as f:  #open the txt
    js = json.load(f) #turns it to json format
    data = js['routes'][0]['legs'][0]['steps']
    for item in data:
      data_name = item['name']
      toll = []
      for name,fare in tunnel_fares.items():
        if name in data_name:
          toll = name
          print(toll)

It finally returns它终于回来了

Western Harbour Crossing
Tsing Sha Highway
Tsing Sha Highway

However, I want to turn it into a list, and remove duplicated object ie:但是,我想把它变成一个列表,并删除重复的 object 即:

['Western Harbour Crossing', 'Tsing Sha Highway']

I tried to use我试着用

new = list(map(lambda x:x, toll))

But it turns out a super weird thing.但事实证明这是一件非常奇怪的事情。 So how can I do this?那么我该怎么做呢? Thank you very much.非常感谢。

To add an item to a list you would use:要将项目添加到列表中,您将使用:

toll.append(name)

instead of:代替:

toll = name

Therefore your code will be:因此,您的代码将是:

with open ('API_UAT2.txt') as f:  #open the txt
    js = json.load(f) #turns it to json format
    data = js['routes'][0]['legs'][0]['steps']
    for item in data:
      data_name = item['name']
      toll = []
      for name,fare in tunnel_fares.items():
        if name in data_name:
          toll.append(name)
          print(toll)

Append to a list toll and remove duplicates using dict.fromkeys : Append 到列表toll并使用dict.fromkeys删除重复项:

with open ('API_UAT2.txt') as f:  #open the txt
    js = json.load(f) #turns it to json format
    data = js['routes'][0]['legs'][0]['steps']
    for item in data:
      data_name = item['name']
      toll = []
      for name,fare in tunnel_fares.items():
        if name in data_name:
          toll.append(name)

toll = list(dict.fromkeys(toll))

You could use a set instead:您可以改用一个集合:

with open ('API_UAT2.txt') as f:  #open the txt
    js = json.load(f) #turns it to json format
    data = js['routes'][0]['legs'][0]['steps']
    tolls = set()
    for item in data:
      data_name = item['name']
      toll = []
      for name, fare in tunnel_fares.items():
        if name in data_name:
          tolls.add(name)

for removing duplicate items from list named l, you can use this code:要从名为 l 的列表中删除重复项,您可以使用以下代码:

l = list(set(l))

Try this:尝试这个:

Code Syntax代码语法

with open ('API_UAT2.txt') as f:  #open the txt
    js = json.load(f) #turns it to json format
    data = js['routes'][0]['legs'][0]['steps']
    for item in data:
         data_name = item['name']
         toll = []
         for name,fare in tunnel_fares.items():
              if name in data_name:
                  toll.append(name)
         toll= list(set(toll))

         print(toll)

Output Syntax Output 语法

['Western Harbour Crossing', 'Tsing Sha Highway']

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

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