簡體   English   中英

如何從for循環創建列表

[英]how to create a list from for loop

大家,最近我正在嘗試從我的數據創建一個列表

這是我的數據:

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

我想查找這些項目是否存在於我的數據中

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

            }

這是我的代碼

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)

它終於回來了

Western Harbour Crossing
Tsing Sha Highway
Tsing Sha Highway

但是,我想把它變成一個列表,並刪除重復的 object 即:

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

我試着用

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

但事實證明這是一件非常奇怪的事情。 那么我該怎么做呢? 非常感謝。

要將項目添加到列表中,您將使用:

toll.append(name)

代替:

toll = name

因此,您的代碼將是:

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 到列表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))

您可以改用一個集合:

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)

要從名為 l 的列表中刪除重復項,您可以使用以下代碼:

l = list(set(l))

嘗試這個:

代碼語法

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 語法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM